19#ifndef OPEN_CPP_UTILS_MAP_H
20#define OPEN_CPP_UTILS_MAP_H
26namespace open_cpp_utils
29template<
typename Key,
typename Value,
class Alloc = std::allocator<struct map_pair>>
36 using value_type = Value;
39 using key_pointer = key_type*;
40 using const_key_pointer =
const key_type*;
41 using key_reference = key_type&;
42 using const_key_reference =
const key_type&;
44 using value_pointer = value_type*;
45 using const_value_pointer =
const value_type*;
46 using value_reference = value_type&;
47 using const_value_reference =
const value_type&;
50 struct hash {
size_t operator()(
const pair_type& pair)
const {
return std::hash<key_type>{}(pair.key); } };
61 bool operator==(
const map_pair& other)
const {
return key == other.key; }
64 map_pair(const_key_reference k) : key(k), value() { }
65 map_pair(const_key_reference k, const_value_reference v) : key(k), value(v) { }
67 map_pair(
map_pair&& v) noexcept : key(std::move(v.key)), value(std::move(v.value)) { }
79 if(&v ==
this)
return *
this;
81 key = std::move(v.key);
82 value = std::move(v.value);
94 map(std::initializer_list<pair_type> data) : table_(data) { }
99 void reserve(
size_t size) { table_.reserve(size); }
101 void insert(const_key_reference key, const_value_reference value);
102 void erase(const_key_reference key);
104 value_reference& operator[](const_key_reference key);
106 const_value_reference& get(const_key_reference key, const_value_reference& def = value_type())
const;
108 iterator find(const_key_reference key) {
return table_.find({ key, value_type() }); }
109 const_iterator find(const_key_reference key)
const {
return table_.find({ key, value_type() }); }
111 bool contains(const_key_reference key) {
return table_.contains({ key, value_type() }); }
113 iterator begin() {
return table_.begin(); }
114 iterator end() {
return table_.end(); }
116 const_iterator begin()
const {
return table_.begin(); }
117 const_iterator end()
const {
return table_.end(); }
119 size_t size()
const {
return table_.size(); }
127template<
typename Key,
typename Value,
class Alloc>
128void map<Key, Value, Alloc>::insert(const_key_reference key, const_value_reference value)
130 iterator it = find({ key, value });
131 if(it != end()) table_.insert({ key, value });
134template<
typename Key,
typename Value,
class Alloc>
135void map<Key, Value, Alloc>::erase(const_key_reference key)
137 table_.erase({ key, value_type() });
140template<
typename Key,
typename Value,
class Alloc>
141typename map<Key, Value, Alloc>::value_reference map<Key, Value, Alloc>::operator[](const_key_reference key)
143 iterator it = table_.find({ key, value_type() });
144 if(it == table_.end())
147 it = table_.find({ key, value_type() });
152template<
typename Key,
typename Value,
class Alloc>
153typename map<Key, Value, Alloc>::const_value_reference map<Key, Value, Alloc>::get(const_key_reference key,
154 const_value_reference def)
const
157 if(it == end())
return def;
Definition hash_table.h:120
Definition hash_table.h:86
Definition hash_table.h:35