diff options
author | Henning Baldersheim <balder@oath.com> | 2018-10-27 11:19:13 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@oath.com> | 2018-11-01 15:14:38 +0100 |
commit | d31a1b7f2df4ea94821a9d98754f4ae186bbf2d8 (patch) | |
tree | 75515a07b65e9225ac3a43f7830d825e5cbb916d /searchlib | |
parent | c3982d42b9d5ca9aea7600f5bc5fc6cceb78505a (diff) |
Use a templated find() to enable lookup without object creation when objects are comparable.
Diffstat (limited to 'searchlib')
-rw-r--r-- | searchlib/src/vespa/searchlib/fef/properties.cpp | 2 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/util/stringenum.cpp | 52 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/util/stringenum.h | 53 |
3 files changed, 56 insertions, 51 deletions
diff --git a/searchlib/src/vespa/searchlib/fef/properties.cpp b/searchlib/src/vespa/searchlib/fef/properties.cpp index 95b74cd5dfd..dc4dcce8865 100644 --- a/searchlib/src/vespa/searchlib/fef/properties.cpp +++ b/searchlib/src/vespa/searchlib/fef/properties.cpp @@ -209,7 +209,7 @@ Properties::lookup(vespalib::stringref key) const if (key.empty()) { return Property(); } - Map::const_iterator node = _data.find(key); + Map::const_iterator node = _data.find<vespalib::stringref>(key); if (node == _data.end()) { return Property(); } diff --git a/searchlib/src/vespa/searchlib/util/stringenum.cpp b/searchlib/src/vespa/searchlib/util/stringenum.cpp index 60238c32cc6..efcf33e73ab 100644 --- a/searchlib/src/vespa/searchlib/util/stringenum.cpp +++ b/searchlib/src/vespa/searchlib/util/stringenum.cpp @@ -2,13 +2,13 @@ #include "stringenum.h" #include <vespa/fastlib/io/bufferedfile.h> +#include <vespa/vespalib/stllike/hashtable.hpp> #include <cassert> #include <vespa/log/log.h> LOG_SETUP(".seachlib.util.stringenum"); -namespace search { -namespace util { +namespace search::util { static inline char * StripString(char *str) @@ -32,7 +32,14 @@ StripString(char *str) return first; } -StringEnum::~StringEnum() { } +StringEnum::StringEnum() + : _numEntries(0), + _mapping(), + _reverseMap() +{ +} + +StringEnum::~StringEnum() = default; void StringEnum::CreateReverseMapping() const @@ -123,5 +130,44 @@ StringEnum::Load(const char *filename) return true; } +void +StringEnum::Clear() +{ + _reverseMap.clear(); + _mapping.clear(); + _numEntries = 0; +} + +int +StringEnum::Add(const char *str) +{ + Map::const_iterator found(_mapping.find(str)); + if (found != _mapping.end()) { + return found->second; + } else { + int value = _numEntries++; + _mapping[str] = value; + return value; + } } + +int +StringEnum::Lookup(const char *str) const +{ + Map::const_iterator found(_mapping.find(str)); + return (found != _mapping.end()) ? found->second : -1; +} + +const char * +StringEnum::Lookup(uint32_t value) const +{ + if (value >= _numEntries) + return NULL; + + if (_numEntries > _reverseMap.size()) + CreateReverseMapping(); + + return _reverseMap[value]; +} + } diff --git a/searchlib/src/vespa/searchlib/util/stringenum.h b/searchlib/src/vespa/searchlib/util/stringenum.h index 44b3afca539..bd234ba2e36 100644 --- a/searchlib/src/vespa/searchlib/util/stringenum.h +++ b/searchlib/src/vespa/searchlib/util/stringenum.h @@ -5,8 +5,7 @@ #include <vector> #include <vespa/vespalib/stllike/hash_map.h> -namespace search { -namespace util { +namespace search::util { /** * An object of this class represents an enumeration of a set of @@ -35,29 +34,17 @@ public: /** * Create an empty string enumeration. **/ - StringEnum() - : _numEntries(0), - _mapping(), - _reverseMap() - { - } + StringEnum(); /** * Destructor. **/ ~StringEnum(); - /** * Discard all entries held by this object. **/ - void Clear() - { - _reverseMap.clear(); - _mapping.clear(); - _numEntries = 0; - } - + void Clear(); /** * Add a string to this enumeration. Equal strings will get the same @@ -68,18 +55,7 @@ public: * @return the enumerated value for the given string. * @param str string you want to add. **/ - int Add(const char *str) - { - Map::const_iterator found(_mapping.find(str)); - if (found != _mapping.end()) { - return found->second; - } else { - int value = _numEntries++; - _mapping[str] = value; - return value; - } - } - + int Add(const char *str); /** * Obtain the enumerated value for the given string. @@ -87,12 +63,7 @@ public: * @return enumerated value or -1 if not present. * @param str the string to look up. **/ - int Lookup(const char *str) const - { - Map::const_iterator found(_mapping.find(str)); - return (found != _mapping.end()) ? found->second : -1; - } - + int Lookup(const char *str) const; /** * Obtain the string for the given enumerated value. @@ -100,17 +71,7 @@ public: * @return string or NULL if out of range. * @param value the enumerated value to look up. **/ - const char *Lookup(uint32_t value) const - { - if (value >= _numEntries) - return NULL; - - if (_numEntries > _reverseMap.size()) - CreateReverseMapping(); - - return _reverseMap[value]; - } - + const char *Lookup(uint32_t value) const; /** * Obtain the number of entries currently present in this @@ -141,5 +102,3 @@ public: }; } -} - |