35 typedef std::pair<const KEY, VAL> value_type;
36 typedef VAL mapped_type;
38 typedef PRED key_equal;
39 typedef ALLOC allocator_type;
41 typedef typename std::vector<value_type>::pointer pointer;
42 typedef typename std::vector<value_type>::const_pointer const_pointer;
43 typedef typename std::vector<value_type>::reference reference;
44 typedef typename std::vector<value_type>::const_reference const_reference;
48 explicit hvec_map(
size_t icap = 0,
const hasher &hf = hasher(),
49 const key_equal &eql = key_equal(),
50 const allocator_type &a = allocator_type())
57 if (
this != std::addressof(that)) {
62 data.reserve(that.size());
63 insert(that.begin(), that.end());
71 hvec_map(ITER begin, ITER end,
const hasher &hf = hasher(),
const key_equal &eql = key_equal(),
72 const allocator_type &a = allocator_type())
74 data.reserve(end - begin);
75 for (
auto it = begin; it != end; ++it) insert(*it);
77 hvec_map(std::initializer_list<value_type> il,
const hasher &hf = hasher(),
78 const key_equal &eql = key_equal(),
const allocator_type &a = allocator_type())
80 data.reserve(il.size());
81 for (
auto &i : il) insert(i);
85 template <
class HVM,
class VT>
91 _iter(HVM &s,
size_t i) : self(&s), idx(i) {}
94 using value_type = VT;
95 using difference_type = ssize_t;
96 using pointer =
typename HVM::pointer;
97 using reference =
typename HVM::reference;
98 using iterator_category = std::bidirectional_iterator_tag;
99 _iter(
const _iter &) =
default;
100 _iter &operator=(
const _iter &a) {
105 value_type &operator*()
const {
return self->data[idx]; }
106 value_type *operator->()
const {
return &self->data[idx]; }
107 _iter &operator++() {
110 }
while (self->erased[idx]);
113 _iter &operator--() {
116 }
while (self->erased[idx]);
119 _iter operator++(
int) {
124 _iter operator--(
int) {
129 bool operator==(
const _iter &a)
const {
return self == a.self && idx == a.idx; }
130 bool operator!=(
const _iter &a)
const {
return self != a.self || idx != a.idx; }
131 operator _iter<const HVM, const VT>()
const {
132 return _iter<const HVM, const VT>(*self, idx);
137 typedef _iter<hvec_map, value_type> iterator;
138 typedef _iter<const hvec_map, const value_type> const_iterator;
139 iterator begin() {
return iterator(*
this, erased.ffz()); }
140 iterator end() {
return iterator(*
this, data.size()); }
141 const_iterator begin()
const {
return const_iterator(*
this, erased.ffz()); }
142 const_iterator end()
const {
return const_iterator(*
this, data.size()); }
143 const_iterator cbegin()
const {
return const_iterator(*
this, erased.ffz()); }
144 const_iterator cend()
const {
return const_iterator(*
this, data.size()); }
146 bool empty()
const {
return inuse == 0; }
147 size_t size()
const {
return inuse; }
148 size_t max_size()
const {
return UINT32_MAX; }
149 bool operator==(
const hvec_map &a)
const {
150 if (inuse != a.inuse)
return false;
153 if (el != *it++)
return false;
156 bool operator!=(
const hvec_map &a)
const {
return !(*
this == a); }
158 hash_vector_base::clear();
162 iterator find(
const KEY &k) {
164 size_t idx = hash_vector_base::find(&k, &cache);
165 return idx ? iterator(*
this, idx - 1) : end();
167 const_iterator find(
const KEY &k)
const {
169 size_t idx = hash_vector_base::find(&k, &cache);
170 return idx ? const_iterator(*
this, idx - 1) : end();
172 size_t count(
const KEY &k)
const {
174 size_t idx = hash_vector_base::find(&k, &cache);
178 VAL &at(
const KEY &k) {
180 size_t idx = hash_vector_base::find(&k, &cache);
181 if (!idx || erased[idx - 1])
throw std::out_of_range(
"hvec_map::at");
182 return data[idx - 1].second;
184 const VAL &at(
const KEY &k)
const {
186 size_t idx = hash_vector_base::find(&k, &cache);
187 if (!idx || erased[idx - 1])
throw std::out_of_range(
"hvec_map::at");
188 return data[idx - 1].second;
192 VAL &operator[](
const KEY &k) {
193 size_t idx = hv_insert(&k);
194 if (idx >= data.size()) {
195 data.emplace_back(k, VAL());
196 return data.back().second;
200 const_cast<KEY &
>(data[idx].first) = k;
201 data[idx].second = VAL();
203 return data[idx].second;
206 VAL &operator[](KEY &&k) {
207 size_t idx = hv_insert(&k);
208 if (idx >= data.size()) {
209 data.emplace_back(std::move(k), VAL());
210 return data.back().second;
214 const_cast<KEY &
>(data[idx].first) = std::move(k);
215 data[idx].second = VAL();
217 return data[idx].second;
222 template <
typename... VV>
223 std::pair<iterator, bool> emplace(
const KEY &k, VV &&...v) {
224 bool new_key =
false;
225 size_t idx = hv_insert(&k);
226 if (idx >= data.size()) {
228 data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(k),
229 std::forward_as_tuple(std::forward<VV>(v)...));
232 if ((new_key = erased[idx])) {
234 const_cast<KEY &
>(data[idx].first) = k;
235 data[idx].second = VAL(std::forward<VV>(v)...);
237 data[idx].second = VAL(std::forward<VV>(v)...);
240 return std::make_pair(iterator(*
this, idx), new_key);
242 template <
typename... VV>
243 std::pair<iterator, bool> emplace(KEY &&k, VV &&...v) {
244 bool new_key =
false;
245 size_t idx = hv_insert(&k);
246 if (idx >= data.size()) {
248 data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(std::move(k)),
249 std::forward_as_tuple(std::forward<VV>(v)...));
251 }
else if ((new_key = erased[idx])) {
253 const_cast<KEY &
>(data[idx].first) = std::move(k);
254 data[idx].second = VAL(std::forward<VV>(v)...);
256 return std::make_pair(iterator(*
this, idx), new_key);
258 std::pair<iterator, bool> insert(
const value_type &v) {
259 bool new_key =
false;
260 size_t idx = hv_insert(&v.first);
261 if (idx >= data.size()) {
265 }
else if ((new_key = erased[idx])) {
267 const_cast<KEY &
>(data[idx].first) = v.first;
268 data[idx].second = v.second;
270 return std::make_pair(iterator(*
this, idx), new_key);
272 template <
typename InputIterator>
273 void insert(InputIterator first, InputIterator last) {
274 for (; first != last; ++first) insert(*first);
276 void insert(std::initializer_list<value_type> vl) {
return insert(vl.begin(), vl.end()); }
277 template <
class HVM,
class VT>
278 _iter<HVM, VT> erase(_iter<HVM, VT> it) {
279 BUG_CHECK(
this == it.self,
"incorrect iterator for hvec_map::erase");
284 const_cast<KEY &
>(data[it.idx].first) = KEY();
285 data[it.idx].second = VAL();
290 size_t erase(
const KEY &k) {
291 size_t idx = remove(&k);
292 if (idx + 1 == 0)
return 0;
293 if (idx < data.size()) {
294 const_cast<KEY &
>(data[idx].first) = KEY();
295 data[idx].second = VAL();
300 using hash_vector_base::dump;
304 std::vector<value_type, ALLOC> data;
305 size_t hashfn(
const void *a)
const override {
return hf(*
static_cast<const KEY *
>(a)); }
306 bool cmpfn(
const void *a,
const void *b)
const override {
307 return eql(*
static_cast<const KEY *
>(a), *
static_cast<const KEY *
>(b));
309 bool cmpfn(
const void *a,
size_t b)
const override {
310 return eql(*
static_cast<const KEY *
>(a), data[b].first);
312 const void *getkey(uint32_t i)
const override {
return &data[i].first; }
313 void *getval(uint32_t i)
override {
return &data[i].second; }
314 uint32_t limit()
override {
return data.size(); }
315 void resizedata(
size_t sz)
override { data.resize(sz); }
316 void moveentry(
size_t to,
size_t from)
override {
318 data[to].~value_type();
319 new (&data[to]) value_type(std::move(data[from]));