mirror of
https://github.com/oatpp/oatpp.git
synced 2025-02-17 17:29:57 +08:00
mapping::type::PairList. Add 'getValueById()' method.
This commit is contained in:
parent
e4d0776584
commit
32ae087417
@ -82,7 +82,7 @@ list->push_back("www");
|
||||
bool contains = set["b"]; // <--- Complexity = O(1);
|
||||
set->insert("z")
|
||||
|
||||
pairList["k1"] = "z"; // <--- Complexity = O(n);
|
||||
auto value = pairList.getValueByKey("k1"); // <--- Complexity = O(n); // note '.' here, not '->' !!!
|
||||
pairList->push_back({"key_z", "z"});
|
||||
|
||||
hashMap["k1"] = "z" // <--- Complexity = O(1);
|
||||
|
@ -93,6 +93,18 @@ OATPP_DEFINE_OBJECT_WRAPPER_DEFAULTS(PairListObjectWrapper, TemplateObjectType,
|
||||
return list.back().second;
|
||||
}
|
||||
|
||||
Value getValueByKey(const Key& key, const Value& defValue = nullptr) const {
|
||||
auto& list = *(this->m_ptr.get());
|
||||
auto it = list.begin();
|
||||
while(it != list.end()) {
|
||||
if(it->first == key) {
|
||||
return it->second;
|
||||
}
|
||||
it ++;
|
||||
}
|
||||
return defValue;
|
||||
}
|
||||
|
||||
TemplateObjectType& operator*() const {
|
||||
return this->m_ptr.operator*();
|
||||
}
|
||||
|
@ -103,6 +103,10 @@ void PairListTest::onRun() {
|
||||
|
||||
OATPP_ASSERT(map2["key1"] == "b");
|
||||
OATPP_ASSERT(map2["key2"] == "c");
|
||||
OATPP_ASSERT(map2.getValueByKey("key1") == "b");
|
||||
OATPP_ASSERT(map2.getValueByKey("key2") == "c");
|
||||
OATPP_ASSERT(map2.getValueByKey("key3") == nullptr);
|
||||
OATPP_ASSERT(map2.getValueByKey("key3", "default-val") == "default-val");
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user