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);
///
/// 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,
const cef_string_t* key,

View File

@ -3,13 +3,44 @@
// can be found in the LICENSE file.
#include <map>
#include <vector>
#include "include/internal/cef_string_map.h"
#include "base/logging.h"
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
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);
StringMap* impl = reinterpret_cast<StringMap*>(map);
StringMap::const_iterator it = impl->find(CefString(key));
if (it == impl->end()) {
if (it == impl->cend()) {
return 0;
}
@ -48,13 +79,8 @@ CEF_EXPORT int cef_string_map_key(cef_string_map_t map,
return 0;
}
StringMap::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_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;
}
StringMap::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_map_append(cef_string_map_t map,

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,