Skip to content

Commit

Permalink
allow compile-time choice of map versus unordered_map
Browse files Browse the repository at this point in the history
map gives sorted output at performance penalty relative to unordered_map
  • Loading branch information
eddelbuettel committed Apr 22, 2015
1 parent 74e390d commit d99bff7
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions include/cpptoml.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <vector>

namespace cpptoml
{

class base; // forward declaration
#if defined(CPPTOML_USE_MAP)
// a std::map will ensure that entries a sorted, albeit at a slight
// performance penalty relative to the (default) unordered_map
using mapStringToBase = std::map<std::string, std::shared_ptr<base>>;
#else
// by default an unordered_map is used for best performance as the
// toml specification does not require entries to be sorted
using mapStringToBase = std::unordered_map<std::string, std::shared_ptr<base>>;
#endif

template <class T>
class option
{
Expand Down Expand Up @@ -394,15 +405,12 @@ class table : public base
/**
* tables can be iterated over.
*/
using iterator
= std::unordered_map<std::string, std::shared_ptr<base>>::iterator;
using iterator = mapStringToBase::iterator;

/**
* tables can be iterated over. Const version.
*/
using const_iterator
= std::unordered_map<std::string,
std::shared_ptr<base>>::const_iterator;
using const_iterator = mapStringToBase::const_iterator;

iterator begin()
{
Expand Down Expand Up @@ -679,7 +687,7 @@ class table : public base
}
}
}
std::unordered_map<std::string, std::shared_ptr<base>> map_;
mapStringToBase map_;
};

inline void table_array::print(std::ostream& stream, size_t depth,
Expand Down

0 comments on commit d99bff7

Please sign in to comment.