37 typedef std::pair<const KEY, VAL> value_type;
38 typedef VAL mapped_type;
40 typedef PRED key_equal;
41 typedef ALLOC allocator_type;
43 typedef typename std::vector<value_type>::pointer pointer;
44 typedef typename std::vector<value_type>::const_pointer const_pointer;
45 typedef typename std::vector<value_type>::reference reference;
46 typedef typename std::vector<value_type>::const_reference const_reference;
50 explicit hvec_map(
size_t icap = 0,
const hasher &hf = hasher(),
51 const key_equal &eql = key_equal(),
52 const allocator_type &a = allocator_type())
59 if (
this != std::addressof(that)) {
64 data.reserve(that.size());
65 insert(that.begin(), that.end());
73 hvec_map(ITER begin, ITER end,
const hasher &hf = hasher(),
const key_equal &eql = key_equal(),
74 const allocator_type &a = allocator_type())
76 data.reserve(end - begin);
77 for (
auto it = begin; it != end; ++it) insert(*it);
79 hvec_map(std::initializer_list<value_type> il,
const hasher &hf = hasher(),
80 const key_equal &eql = key_equal(),
const allocator_type &a = allocator_type())
82 data.reserve(il.size());
83 for (
auto &i : il) insert(i);
87 template <
class HVM,
class VT>
93 _iter(HVM &s,
size_t i) : self(&s), idx(i) {}
96 using value_type = VT;
97 using difference_type = ssize_t;
98 using pointer =
typename HVM::pointer;
99 using reference =
typename HVM::reference;
100 using iterator_category = std::bidirectional_iterator_tag;
101 _iter(
const _iter &) =
default;
102 _iter &operator=(
const _iter &a) {
107 value_type &operator*()
const {
return self->data[idx]; }
108 value_type *operator->()
const {
return &self->data[idx]; }
109 _iter &operator++() {
112 }
while (self->erased[idx]);
115 _iter &operator--() {
118 }
while (self->erased[idx]);
121 _iter operator++(
int) {
126 _iter operator--(
int) {
131 bool operator==(
const _iter &a)
const {
return self == a.self && idx == a.idx; }
132 bool operator!=(
const _iter &a)
const {
return self != a.self || idx != a.idx; }
133 operator _iter<const HVM, const VT>()
const {
134 return _iter<const HVM, const VT>(*self, idx);
139 typedef _iter<hvec_map, value_type> iterator;
140 typedef _iter<const hvec_map, const value_type> const_iterator;
141 iterator begin() {
return iterator(*
this, erased.ffz()); }
142 iterator end() {
return iterator(*
this, data.size()); }
143 const_iterator begin()
const {
return const_iterator(*
this, erased.ffz()); }
144 const_iterator end()
const {
return const_iterator(*
this, data.size()); }
145 const_iterator cbegin()
const {
return const_iterator(*
this, erased.ffz()); }
146 const_iterator cend()
const {
return const_iterator(*
this, data.size()); }
148 bool empty()
const {
return inuse == 0; }
149 size_t size()
const {
return inuse; }
150 size_t max_size()
const {
return UINT32_MAX; }
151 bool operator==(
const hvec_map &a)
const {
152 if (inuse != a.inuse)
return false;
155 if (el != *it++)
return false;
158 bool operator!=(
const hvec_map &a)
const {
return !(*
this == a); }
160 hash_vector_base::clear();
164 iterator find(
const KEY &k) {
166 size_t idx = hash_vector_base::find(&k, &cache);
167 return idx ? iterator(*
this, idx - 1) : end();
169 const_iterator find(
const KEY &k)
const {
171 size_t idx = hash_vector_base::find(&k, &cache);
172 return idx ? const_iterator(*
this, idx - 1) : end();
174 size_t count(
const KEY &k)
const {
176 size_t idx = hash_vector_base::find(&k, &cache);
180 VAL &at(
const KEY &k) {
182 size_t idx = hash_vector_base::find(&k, &cache);
183 if (!idx || erased[idx - 1])
throw std::out_of_range(
"hvec_map::at");
184 return data[idx - 1].second;
186 const VAL &at(
const KEY &k)
const {
188 size_t idx = hash_vector_base::find(&k, &cache);
189 if (!idx || erased[idx - 1])
throw std::out_of_range(
"hvec_map::at");
190 return data[idx - 1].second;
194 VAL &operator[](
const KEY &k) {
195 size_t idx = hv_insert(&k);
196 if (idx >= data.size()) {
197 data.emplace_back(k, VAL());
198 return data.back().second;
202 const_cast<KEY &
>(data[idx].first) = k;
203 data[idx].second = VAL();
205 return data[idx].second;
208 VAL &operator[](KEY &&k) {
209 size_t idx = hv_insert(&k);
210 if (idx >= data.size()) {
211 data.emplace_back(std::move(k), VAL());
212 return data.back().second;
216 const_cast<KEY &
>(data[idx].first) = std::move(k);
217 data[idx].second = VAL();
219 return data[idx].second;
224 template <
typename... VV>
225 std::pair<iterator, bool> emplace(
const KEY &k, VV &&...v) {
226 bool new_key =
false;
227 size_t idx = hv_insert(&k);
228 if (idx >= data.size()) {
230 data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(k),
231 std::forward_as_tuple(std::forward<VV>(v)...));
234 if ((new_key = erased[idx])) {
236 const_cast<KEY &
>(data[idx].first) = k;
237 data[idx].second = VAL(std::forward<VV>(v)...);
239 data[idx].second = VAL(std::forward<VV>(v)...);
242 return std::make_pair(iterator(*
this, idx), new_key);
244 template <
typename... VV>
245 std::pair<iterator, bool> emplace(KEY &&k, VV &&...v) {
246 bool new_key =
false;
247 size_t idx = hv_insert(&k);
248 if (idx >= data.size()) {
250 data.emplace_back(std::piecewise_construct_t(), std::forward_as_tuple(std::move(k)),
251 std::forward_as_tuple(std::forward<VV>(v)...));
253 }
else if ((new_key = erased[idx])) {
255 const_cast<KEY &
>(data[idx].first) = std::move(k);
256 data[idx].second = VAL(std::forward<VV>(v)...);
258 return std::make_pair(iterator(*
this, idx), new_key);
260 std::pair<iterator, bool> insert(
const value_type &v) {
261 bool new_key =
false;
262 size_t idx = hv_insert(&v.first);
263 if (idx >= data.size()) {
267 }
else if ((new_key = erased[idx])) {
269 const_cast<KEY &
>(data[idx].first) = v.first;
270 data[idx].second = v.second;
272 return std::make_pair(iterator(*
this, idx), new_key);
274 template <
typename InputIterator>
275 void insert(InputIterator first, InputIterator last) {
276 for (; first != last; ++first) insert(*first);
278 void insert(std::initializer_list<value_type> vl) {
return insert(vl.begin(), vl.end()); }
279 template <
class HVM,
class VT>
280 _iter<HVM, VT> erase(_iter<HVM, VT> it) {
281 BUG_CHECK(
this == it.self,
"incorrect iterator for hvec_map::erase");
286 const_cast<KEY &
>(data[it.idx].first) = KEY();
287 data[it.idx].second = VAL();
292 size_t erase(
const KEY &k) {
293 size_t idx = remove(&k);
294 if (idx + 1 == 0)
return 0;
295 if (idx < data.size()) {
296 const_cast<KEY &
>(data[idx].first) = KEY();
297 data[idx].second = VAL();
302 using hash_vector_base::dump;
306 std::vector<value_type, ALLOC> data;
307 size_t hashfn(
const void *a)
const override {
return hf(*
static_cast<const KEY *
>(a)); }
308 bool cmpfn(
const void *a,
const void *b)
const override {
309 return eql(*
static_cast<const KEY *
>(a), *
static_cast<const KEY *
>(b));
311 bool cmpfn(
const void *a,
size_t b)
const override {
312 return eql(*
static_cast<const KEY *
>(a), data[b].first);
314 const void *getkey(uint32_t i)
const override {
return &data[i].first; }
315 void *getval(uint32_t i)
override {
return &data[i].second; }
316 uint32_t limit()
override {
return data.size(); }
317 void resizedata(
size_t sz)
override { data.resize(sz); }
318 void moveentry(
size_t to,
size_t from)
override {
320 data[to].~value_type();
321 new (&data[to]) value_type(std::move(data[from]));