18 constexpr
bool operator()(
const T &lhs,
const T &rhs)
const 25 template<
typename KeyT,
typename ValueT,
typename EqT = ListMapEqualTo<KeyT>>
29 using Pair = std::pair<KeyT, ValueT>;
30 using List = std::vector<Pair>;
32 using size_type = size_t;
33 using value_type = Pair;
34 using reference = Pair&;
35 using const_reference =
const Pair&;
36 using iterator =
typename List::iterator;
37 using const_iterator =
typename List::const_iterator;
39 iterator begin() {
return _list.begin(); }
40 iterator end() {
return _list.end(); }
41 const_iterator begin()
const {
return _list.begin(); }
42 const_iterator end()
const {
return _list.end(); }
44 size_t size()
const {
return _list.size(); }
45 bool empty()
const {
return _list.empty(); }
47 iterator find(
const KeyT& key)
50 for (iterator it=begin(); it!=e; ++it) {
51 if (_eq(it->first, key)) {
58 const_iterator find(
const KeyT& key)
const 60 const_iterator e=end();
61 for (const_iterator it=begin(); it!=e; ++it) {
62 if (_eq(it->first, key)) {
69 size_t count(
const KeyT& key)
const 71 return find(key) == end() ? 0 : 1;
74 ValueT& operator[](
const KeyT& key)
77 for (iterator it=begin(); it!=e; ++it) {
78 if (_eq(it->first, key)) {
82 _list.push_back(Pair(key, ValueT()));
83 return _list.back().second;
86 const ValueT& at(
const KeyT& key)
const 89 if (it == end()) {
throw std::domain_error(
"No such key in ListMap"); }
93 bool insert(
const Pair& p)
95 const_iterator e=end();
96 for (const_iterator it=begin(); it!=e; ++it) {
97 if (_eq(it->first, p.first)) {
105 void insert_or_assign(
const KeyT& key, ValueT&& value)
107 const_iterator e=end();
108 for (const_iterator it=begin(); it!=e; ++it) {
109 if (_eq(it->first, key)) {
110 it->second = std::move(value);
114 _list.push_back(std::make_pair(key, std::move(value)));
117 iterator erase(iterator it)
124 void erase(
const KeyT& key)
127 for (iterator it=begin(); it!=e; ++it) {
128 if (_eq(it->first, key)) {
138 _list.shrink_to_fit();
141 void clear() { _list.clear(); }
Linear lookup map for quick lookups among few values.
Definition: list_map.hpp:26
void shrink_to_fit()
Frees unnecessary memory.
Definition: list_map.hpp:136
like std::equal_to but no need to #include <functional>
Definition: list_map.hpp:16
Definition: coroutine.hpp:18