22class hvec_map : hash_vector_base {
28 typedef std::pair<const KEY, VAL> value_type;
29 typedef VAL mapped_type;
31 typedef PRED key_equal;
32 typedef ALLOC allocator_type;
34 typedef typename std::vector<value_type>::pointer pointer;
35 typedef typename std::vector<value_type>::const_pointer const_pointer;
36 typedef typename std::vector<value_type>::reference reference;
37 typedef typename std::vector<value_type>::const_reference const_reference;
41 explicit hvec_map(
size_t icap = 0,
const hasher &hf = hasher(),
42 const key_equal &eql = key_equal(),
43 const allocator_type &a = allocator_type())
44 : hash_vector_base(
true,
false, icap), hf(hf), eql(eql), data(a) {
47 hvec_map(
const hvec_map &) =
default;
48 hvec_map(hvec_map &&) =
default;
49 hvec_map &operator=(
const hvec_map &that) {
50 if (
this != std::addressof(that)) {
55 data.reserve(that.size());
56 insert(that.begin(), that.end());
61 hvec_map &operator=(hvec_map &&) =
default;
62 ~hvec_map() =
default;
64 hvec_map(ITER begin, ITER end,
const hasher &hf = hasher(),
const key_equal &eql = key_equal(),
65 const allocator_type &a = allocator_type())
66 : hash_vector_base(
true,
false, end - begin), hf(hf), eql(eql), data(a) {
67 data.reserve(end - begin);
68 for (
auto it = begin; it != end; ++it) insert(*it);
70 hvec_map(std::initializer_list<value_type> il,
const hasher &hf = hasher(),
71 const key_equal &eql = key_equal(),
const allocator_type &a = allocator_type())
72 : hash_vector_base(
true,
false, il.size()), hf(hf), eql(eql), data(a) {
73 data.reserve(il.size());
74 for (
auto &i : il) insert(i);
78 template <
class HVM,
class VT>
83 friend class hvec_map;
84 _iter(HVM &s,
size_t i) : self(&s), idx(i) {}
87 using value_type = VT;
88 using difference_type = ssize_t;
89 using pointer =
typename HVM::pointer;
90 using reference =
typename HVM::reference;
91 using iterator_category = std::bidirectional_iterator_tag;
92 _iter(
const _iter &) =
default;
93 _iter &operator=(
const _iter &a) {
98 value_type &operator*()
const {
return self->data[idx]; }
99 value_type *operator->()
const {
return &self->data[idx]; }
100 _iter &operator++() {
103 }
while (self->erased[idx]);
106 _iter &operator--() {
109 }
while (self->erased[idx]);
112 _iter operator++(
int) {
117 _iter operator--(
int) {
122 bool operator==(
const _iter &a)
const {
return self == a.self && idx == a.idx; }
123 bool operator!=(
const _iter &a)
const {
return self != a.self || idx != a.idx; }
124 operator _iter<const HVM, const VT>()
const {
125 return _iter<const HVM, const VT>(*self, idx);
130 typedef _iter<hvec_map, value_type> iterator;
131 typedef _iter<const hvec_map, const value_type> const_iterator;
132 iterator begin() {
return iterator(*
this, erased.ffz()); }
133 iterator end() {
return iterator(*
this, data.size()); }
134 const_iterator begin()
const {
return const_iterator(*
this, erased.ffz()); }
135 const_iterator end()
const {
return const_iterator(*
this, data.size()); }
136 const_iterator cbegin()
const {
return const_iterator(*
this, erased.ffz()); }
137 const_iterator cend()
const {
return const_iterator(*
this, data.size()); }
139 bool empty()
const {
return inuse == 0; }
140 size_t size()
const {
return inuse; }
141 size_t max_size()
const {
return UINT32_MAX; }
142 bool operator==(
const hvec_map &a)
const {
143 if (inuse != a.inuse)
return false;
146 if (el != *it++)
return false;
149 bool operator!=(
const hvec_map &a)
const {
return !(*
this == a); }
151 hash_vector_base::clear();
155 iterator find(
const KEY &k) {
157 size_t idx = hash_vector_base::find(&k, &cache);
158 return idx ? iterator(*
this, idx - 1) : end();
160 const_iterator find(
const KEY &k)
const {
162 size_t idx = hash_vector_base::find(&k, &cache);
163 return idx ? const_iterator(*
this, idx - 1) : end();
165 size_t count(
const KEY &k)
const {
167 size_t idx = hash_vector_base::find(&k, &cache);
171 VAL &at(
const KEY &k) {
173 size_t idx = hash_vector_base::find(&k, &cache);
174 if (!idx || erased[idx - 1])
throw std::out_of_range(
"hvec_map::at");
175 return data[idx - 1].second;
177 const VAL &at(
const KEY &k)
const {
179 size_t idx = hash_vector_base::find(&k, &cache);
180 if (!idx || erased[idx - 1])
throw std::out_of_range(
"hvec_map::at");
181 return data[idx - 1].second;
185 VAL &operator[](
const KEY &k) {
186 size_t idx = hv_insert(&k);
187 if (idx >= data.size()) {
188 data.emplace_back(k, VAL());
189 return data.back().second;
193 const_cast<KEY &
>(data[idx].first) = k;
194 data[idx].second = VAL();
196 return data[idx].second;
199 VAL &operator[](KEY &&k) {
200 size_t idx = hv_insert(&k);
201 if (idx >= data.size()) {
202 data.emplace_back(std::move(k), VAL());
203 return data.back().second;
207 const_cast<KEY &
>(data[idx].first) = std::move(k);
208 data[idx].second = VAL();
210 return data[idx].second;
215 template <
typename... VV>
216 std::pair<iterator, bool> emplace(
const KEY &k, VV &&...v) {
217 bool new_key =
false;
218 size_t idx = hv_insert(&k);
219 if (idx >= data.size()) {
221 data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(k),
222 std::forward_as_tuple(std::forward<VV>(v)...));
225 if ((new_key = erased[idx])) {
227 const_cast<KEY &
>(data[idx].first) = k;
228 data[idx].second = VAL(std::forward<VV>(v)...);
230 data[idx].second = VAL(std::forward<VV>(v)...);
233 return std::make_pair(iterator(*
this, idx), new_key);
235 template <
typename... VV>
236 std::pair<iterator, bool> emplace(KEY &&k, VV &&...v) {
237 bool new_key =
false;
238 size_t idx = hv_insert(&k);
239 if (idx >= data.size()) {
241 data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(std::move(k)),
242 std::forward_as_tuple(std::forward<VV>(v)...));
244 }
else if ((new_key = erased[idx])) {
246 const_cast<KEY &
>(data[idx].first) = std::move(k);
247 data[idx].second = VAL(std::forward<VV>(v)...);
249 return std::make_pair(iterator(*
this, idx), new_key);
251 std::pair<iterator, bool> insert(
const value_type &v) {
252 bool new_key =
false;
253 size_t idx = hv_insert(&v.first);
254 if (idx >= data.size()) {
258 }
else if ((new_key = erased[idx])) {
260 const_cast<KEY &
>(data[idx].first) = v.first;
261 data[idx].second = v.second;
263 return std::make_pair(iterator(*
this, idx), new_key);
265 template <
typename InputIterator>
266 void insert(InputIterator first, InputIterator last) {
267 for (; first != last; ++first) insert(*first);
269 void insert(std::initializer_list<value_type> vl) {
return insert(vl.begin(), vl.end()); }
270 template <
class HVM,
class VT>
271 _iter<HVM, VT> erase(_iter<HVM, VT> it) {
272 BUG_CHECK(
this == it.self,
"incorrect iterator for hvec_map::erase");
277 const_cast<KEY &
>(data[it.idx].first) = KEY();
278 data[it.idx].second = VAL();
283 size_t erase(
const KEY &k) {
284 size_t idx = remove(&k);
285 if (idx + 1 == 0)
return 0;
286 if (idx < data.size()) {
287 const_cast<KEY &
>(data[idx].first) = KEY();
288 data[idx].second = VAL();
293 using hash_vector_base::dump;
297 std::vector<value_type, ALLOC> data;
298 size_t hashfn(
const void *a)
const override {
return hf(*
static_cast<const KEY *
>(a)); }
299 bool cmpfn(
const void *a,
const void *b)
const override {
300 return eql(*
static_cast<const KEY *
>(a), *
static_cast<const KEY *
>(b));
302 bool cmpfn(
const void *a,
size_t b)
const override {
303 return eql(*
static_cast<const KEY *
>(a), data[b].first);
305 const void *getkey(uint32_t i)
const override {
return &data[i].first; }
306 void *getval(uint32_t i)
override {
return &data[i].second; }
307 uint32_t limit()
override {
return data.size(); }
308 void resizedata(
size_t sz)
override { data.resize(sz); }
309 void moveentry(
size_t to,
size_t from)
override {
311 data[to].~value_type();
312 new (&data[to]) value_type(std::move(data[from]));