Improved performance for Dictionary::Get.

This commit is contained in:
Gunnar Beutner 2012-08-06 12:03:38 +02:00
parent c0d02291a2
commit 4d02780f4c
4 changed files with 58 additions and 7 deletions

View File

@ -22,6 +22,37 @@
using namespace icinga;
struct DictionaryKeyLessComparer
{
bool operator()(const pair<String, Value>& a, const char *b)
{
return a.first < b;
}
bool operator()(const char *a, const pair<String, Value>& b)
{
return a < b.first;
}
};
/**
* Restrieves a value from a dictionary.
*
* @param key The key whose value should be retrieved.
* @returns The value of an empty value if the key was not found.
*/
Value Dictionary::Get(const char *key) const
{
map<String, Value>::const_iterator it;
it = std::lower_bound(m_Data.begin(), m_Data.end(), key, DictionaryKeyLessComparer());
if (it == m_Data.end() || DictionaryKeyLessComparer()(key, *it))
return Empty;
return it->second;
}
/**
* Retrieves a value from the dictionary.
*
@ -30,13 +61,7 @@ using namespace icinga;
*/
Value Dictionary::Get(const String& key) const
{
map<String, Value>::const_iterator it;
it = m_Data.find(key);
if (it == m_Data.end())
return Empty;
return it->second;
return Get(key.CStr());
}
/**

View File

@ -36,6 +36,7 @@ public:
typedef map<String, Value>::iterator Iterator;
Value Get(const char *key) const;
Value Get(const String& key) const;
void Set(const String& key, const Value& value);
String Add(const Value& value);

View File

@ -199,6 +199,26 @@ bool icinga::operator!=(const char *lhs, const String& rhs)
return lhs != static_cast<std::string>(rhs);
}
bool icinga::operator<(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) < rhs;
}
bool icinga::operator<(const char *lhs, const String& rhs)
{
return lhs < static_cast<std::string>(rhs);
}
bool icinga::operator>(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) > rhs;
}
bool icinga::operator>(const char *lhs, const String& rhs)
{
return lhs > static_cast<std::string>(rhs);
}
String::Iterator icinga::range_begin(String& x)
{
return x.Begin();

View File

@ -89,6 +89,11 @@ I2_BASE_API bool operator!=(const String& lhs, const String& rhs);
I2_BASE_API bool operator!=(const String& lhs, const char *rhs);
I2_BASE_API bool operator!=(const char *lhs, const String& rhs);
I2_BASE_API bool operator<(const String& lhs, const char *rhs);
I2_BASE_API bool operator<(const char *lhs, const String& rhs);
I2_BASE_API bool operator>(const String& lhs, const char *rhs);
I2_BASE_API bool operator>(const char *lhs, const String& rhs);
I2_BASE_API String::Iterator range_begin(String& x);
I2_BASE_API String::ConstIterator range_begin(const String& x);
I2_BASE_API String::Iterator range_end(String& x);