Fix complexity class for Dictionary::Get

fixes #8919
This commit is contained in:
Gunnar Beutner 2015-03-31 10:39:02 +02:00
parent f9d5846e30
commit 43f709c22a
2 changed files with 3 additions and 49 deletions

View File

@ -27,70 +27,25 @@ using namespace icinga;
REGISTER_PRIMITIVE_TYPE(Dictionary, Dictionary::GetPrototype()); REGISTER_PRIMITIVE_TYPE(Dictionary, Dictionary::GetPrototype());
/**
* Compares dictionary keys using the less operator.
*/
struct DictionaryKeyLessComparer
{
/**
* Compares two keys.
*
* @param a The first key.
* @param b The second key.
* @returns true if the first key is less than the second key, false
* otherwise
*/
bool operator()(const std::pair<String, Value>& a, const char *b)
{
return a.first < b;
}
/**
* Compares two keys.
*
* @param a The first key.
* @param b The second key.
* @returns true if the first key is less than the second key, false
* otherwise
*/
bool operator()(const char *a, const std::pair<String, Value>& b)
{
return a < b.first;
}
};
/** /**
* Retrieves a value from a dictionary. * Retrieves a value from a dictionary.
* *
* @param key The key whose value should be retrieved. * @param key The key whose value should be retrieved.
* @returns The value of an empty value if the key was not found. * @returns The value of an empty value if the key was not found.
*/ */
Value Dictionary::Get(const char *key) const Value Dictionary::Get(const String& key) const
{ {
ASSERT(!OwnsLock()); ASSERT(!OwnsLock());
ObjectLock olock(this); ObjectLock olock(this);
std::map<String, Value>::const_iterator it; std::map<String, Value>::const_iterator it = m_Data.find(key);
it = std::lower_bound(m_Data.begin(), m_Data.end(), key, DictionaryKeyLessComparer()); if (it == m_Data.end())
if (it == m_Data.end() || DictionaryKeyLessComparer()(key, *it))
return Empty; return Empty;
return it->second; return it->second;
} }
/**
* Retrieves a value from the dictionary.
*
* @param key The key whose value should be retrieved.
* @returns The value or an empty value if the key was not found.
*/
Value Dictionary::Get(const String& key) const
{
return Get(key.CStr());
}
/** /**
* Sets a value in the dictionary. * Sets a value in the dictionary.
* *

View File

@ -55,7 +55,6 @@ public:
inline ~Dictionary(void) inline ~Dictionary(void)
{ } { }
Value Get(const char *key) const;
Value Get(const String& key) const; Value Get(const String& key) const;
void Set(const String& key, const Value& value); void Set(const String& key, const Value& value);
bool Contains(const String& key) const; bool Contains(const String& key) const;