From 5925c11a1fe8b2e02b790dd40b031ae005d5b68f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 10 Mar 2015 15:42:04 +0200 Subject: Further operation implementation --- build/utility | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'build/utility') diff --git a/build/utility b/build/utility index fd7888c..93fb441 100644 --- a/build/utility +++ b/build/utility @@ -6,11 +6,15 @@ #define BUILD_UTILITY #include +#include // numeric_limits #include +#include #include -#include // strcmp +#include // strcmp() +#include #include #include +#include #include @@ -94,6 +98,53 @@ namespace build }; extern string_pool extension_pool; + + // A pool of strings in which each string is assigned an individual + // index (or id) of type I (e.g., uint8_t, uint16_t, etc., depending + // on how many entries are expected). Index value 0 is reserved to + // indicate the no entry condition. + // + template + struct string_table + { + // Find existing or insert new. + // + I + insert (const std::string& s) + { + std::size_t i (vec_.size () + 1); + auto r (map_.emplace (s, static_cast (i))); + + if (r.second) + { + assert (i <= std::numeric_limits::max ()); + vec_.push_back (&r.first->first); + } + + return r.first->second; + } + + // Find existing. + // + I + find (const std::string& s) const + { + auto i (map_.find (s)); + return i != map_.end () ? i->second : 0; + } + + // Reverse lookup. + // + const std::string& + operator[] (I i) const {assert (i > 0); return *vec_[i - 1];} + + I + size () const {return static_cast (vec_.size ());} + + private: + std::unordered_map map_; + std::vector vec_; + }; } #endif // BUILD_UTILITY -- cgit v1.1