Make string map append API match the manuals

Reduce cef_string_[multi]map_key() and cef_string_[multi]map_value() complexities
from O(N) to O(1).
This commit is contained in:
Sergey Markelov 2023-04-13 16:08:48 +00:00 committed by Marshall Greenblatt
parent c3648f42b2
commit 78ea5d8c61
3 changed files with 76 additions and 36 deletions

View File

@ -75,7 +75,8 @@ CEF_EXPORT int cef_string_map_value(cef_string_map_t map,
cef_string_t* value); cef_string_t* value);
/// ///
/// Append a new key/value pair at the end of the string map. /// Append a new key/value pair at the end of the string map. If the key exists,
/// overwrite the existing value with a new value w/o changing the pair order.
/// ///
CEF_EXPORT int cef_string_map_append(cef_string_map_t map, CEF_EXPORT int cef_string_map_append(cef_string_map_t map,
const cef_string_t* key, const cef_string_t* key,

View File

@ -3,13 +3,44 @@
// can be found in the LICENSE file. // can be found in the LICENSE file.
#include <map> #include <map>
#include <vector>
#include "include/internal/cef_string_map.h" #include "include/internal/cef_string_map.h"
#include "base/logging.h" #include "base/logging.h"
namespace { namespace {
using StringMap = std::map<CefString, CefString>;
class StringMap {
using Map = std::map<CefString, CefString>;
using value_type = Map::value_type;
public:
using const_iterator = Map::const_iterator;
size_t size() const { return map_ref_.size(); }
size_t count(const CefString& key) const { return map_.count(key); }
const_iterator find(const CefString& value) const { return map_.find(value); }
const_iterator cend() const { return map_.cend(); }
const value_type& operator[](size_t pos) const { return *map_ref_[pos]; }
void insert(value_type&& value) {
// does not invalidate iterators
const auto [it, inserted] = map_.insert(std::move(value));
if (inserted) {
map_ref_.push_back(std::move(it));
}
}
void clear() {
map_ref_.clear();
map_.clear();
}
private:
Map map_;
std::vector<Map::const_iterator> map_ref_;
};
} // namespace } // namespace
CEF_EXPORT cef_string_map_t cef_string_map_alloc() { CEF_EXPORT cef_string_map_t cef_string_map_alloc() {
@ -29,7 +60,7 @@ CEF_EXPORT int cef_string_map_find(cef_string_map_t map,
DCHECK(value); DCHECK(value);
StringMap* impl = reinterpret_cast<StringMap*>(map); StringMap* impl = reinterpret_cast<StringMap*>(map);
StringMap::const_iterator it = impl->find(CefString(key)); StringMap::const_iterator it = impl->find(CefString(key));
if (it == impl->end()) { if (it == impl->cend()) {
return 0; return 0;
} }
@ -48,13 +79,8 @@ CEF_EXPORT int cef_string_map_key(cef_string_map_t map,
return 0; return 0;
} }
StringMap::const_iterator it = impl->begin(); const auto& [k, _] = (*impl)[index];
for (size_t ct = 0; it != impl->end(); ++it, ct++) { return cef_string_set(k.c_str(), k.length(), key, true);
if (ct == index) {
return cef_string_set(it->first.c_str(), it->first.length(), key, true);
}
}
return 0;
} }
CEF_EXPORT int cef_string_map_value(cef_string_map_t map, CEF_EXPORT int cef_string_map_value(cef_string_map_t map,
@ -68,14 +94,8 @@ CEF_EXPORT int cef_string_map_value(cef_string_map_t map,
return 0; return 0;
} }
StringMap::const_iterator it = impl->begin(); const auto& [_, v] = (*impl)[index];
for (size_t ct = 0; it != impl->end(); ++it, ct++) { return cef_string_set(v.c_str(), v.length(), value, true);
if (ct == index) {
return cef_string_set(it->second.c_str(), it->second.length(), value,
true);
}
}
return 0;
} }
CEF_EXPORT int cef_string_map_append(cef_string_map_t map, CEF_EXPORT int cef_string_map_append(cef_string_map_t map,

View File

@ -3,13 +3,43 @@
// can be found in the LICENSE file. // can be found in the LICENSE file.
#include <map> #include <map>
#include <vector>
#include "include/internal/cef_string_multimap.h" #include "include/internal/cef_string_multimap.h"
#include "base/logging.h" #include "base/logging.h"
namespace { namespace {
using StringMultimap = std::multimap<CefString, CefString>;
class StringMultimap {
using Map = std::multimap<CefString, CefString>;
using value_type = Map::value_type;
public:
using const_iterator = Map::const_iterator;
size_t size() const { return map_ref_.size(); }
size_t count(const CefString& key) const { return map_.count(key); }
const value_type operator[](size_t pos) const { return *map_ref_[pos]; }
std::pair<const_iterator, const_iterator> equal_range(
const CefString& key) const {
return map_.equal_range(key);
}
void insert(value_type&& value) {
auto it = map_.insert(std::move(value)); // does not invalidate iterators
map_ref_.push_back(std::move(it));
}
void clear() {
map_ref_.clear();
map_.clear();
}
private:
Map map_;
std::vector<Map::const_iterator> map_ref_;
};
} // namespace } // namespace
CEF_EXPORT cef_string_multimap_t cef_string_multimap_alloc() { CEF_EXPORT cef_string_multimap_t cef_string_multimap_alloc() {
@ -46,8 +76,8 @@ CEF_EXPORT int cef_string_multimap_enumerate(cef_string_multimap_t map,
return 0; return 0;
} }
std::pair<StringMultimap::iterator, StringMultimap::iterator> range_it = std::pair<StringMultimap::const_iterator, StringMultimap::const_iterator>
impl->equal_range(key_str); range_it = impl->equal_range(key_str);
size_t count = value_index; size_t count = value_index;
while (count-- && range_it.first != range_it.second) { while (count-- && range_it.first != range_it.second) {
@ -73,13 +103,8 @@ CEF_EXPORT int cef_string_multimap_key(cef_string_multimap_t map,
return 0; return 0;
} }
StringMultimap::const_iterator it = impl->begin(); const auto& [k, _] = (*impl)[index];
for (size_t ct = 0; it != impl->end(); ++it, ct++) { return cef_string_set(k.c_str(), k.length(), key, true);
if (ct == index) {
return cef_string_set(it->first.c_str(), it->first.length(), key, true);
}
}
return 0;
} }
CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map, CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map,
@ -93,14 +118,8 @@ CEF_EXPORT int cef_string_multimap_value(cef_string_multimap_t map,
return 0; return 0;
} }
StringMultimap::const_iterator it = impl->begin(); const auto& [_, v] = (*impl)[index];
for (size_t ct = 0; it != impl->end(); ++it, ct++) { return cef_string_set(v.c_str(), v.length(), value, true);
if (ct == index) {
return cef_string_set(it->second.c_str(), it->second.length(), value,
true);
}
}
return 0;
} }
CEF_EXPORT int cef_string_multimap_append(cef_string_multimap_t map, CEF_EXPORT int cef_string_multimap_append(cef_string_multimap_t map,