mapping::type::PairList. Add 'getValueById()' method.

This commit is contained in:
lganzzzo 2020-05-17 03:25:08 +03:00
parent e4d0776584
commit 32ae087417
3 changed files with 17 additions and 1 deletions

View File

@ -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);

View File

@ -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*();
}

View File

@ -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");
}