mirror of https://github.com/Icinga/icinga2.git
Improved performance for Dictionary::Get.
This commit is contained in:
parent
c0d02291a2
commit
4d02780f4c
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue