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

@ -3,13 +3,43 @@
// can be found in the LICENSE file.
#include <map>
#include <vector>
#include "include/internal/cef_string_multimap.h"
#include "base/logging.h"
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
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;
}
std::pair<StringMultimap::iterator, StringMultimap::iterator> range_it =
impl->equal_range(key_str);
std::pair<StringMultimap::const_iterator, StringMultimap::const_iterator>
range_it = impl->equal_range(key_str);
size_t count = value_index;
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;
}
StringMultimap::const_iterator it = impl->begin();
for (size_t ct = 0; it != impl->end(); ++it, ct++) {
if (ct == index) {
return cef_string_set(it->first.c_str(), it->first.length(), key, true);
}
}
return 0;
const auto& [k, _] = (*impl)[index];
return cef_string_set(k.c_str(), k.length(), key, true);
}
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;
}
StringMultimap::const_iterator it = impl->begin();
for (size_t ct = 0; it != impl->end(); ++it, ct++) {
if (ct == index) {
return cef_string_set(it->second.c_str(), it->second.length(), value,
true);
}
}
return 0;
const auto& [_, v] = (*impl)[index];
return cef_string_set(v.c_str(), v.length(), value, true);
}
CEF_EXPORT int cef_string_multimap_append(cef_string_multimap_t map,