36 typedef const KEY value_type;
38 typedef PRED key_equal;
39 typedef ALLOC allocator_type;
40 typedef value_type *pointer, *const_pointer, &reference, &const_reference;
43 explicit hvec_set(
size_t icap = 0,
const hasher &hf = hasher(),
44 const key_equal &eql = key_equal(),
45 const allocator_type &a = allocator_type())
52 if (
this != std::addressof(that)) {
57 data.reserve(that.size());
58 insert(that.begin(), that.end());
66 hvec_set(ITER begin, ITER end,
const hasher &hf = hasher(),
const key_equal &eql = key_equal(),
67 const allocator_type &a = allocator_type())
69 data.reserve(end - begin);
70 for (
auto it = begin; it != end; ++it) insert(*it);
72 hvec_set(std::initializer_list<value_type> il,
const hasher &hf = hasher(),
73 const key_equal &eql = key_equal(),
const allocator_type &a = allocator_type())
75 data.reserve(il.size());
76 for (
auto &i : il) insert(i);
80 template <
class HVM,
class VT>
86 _iter(HVM &s,
size_t i) : self(&s), idx(i) {}
89 using value_type = VT;
90 using difference_type = ssize_t;
91 using pointer =
typename HVM::pointer;
92 using reference =
typename HVM::reference;
93 using iterator_category = std::bidirectional_iterator_tag;
94 _iter(
const _iter &) =
default;
95 _iter &operator=(
const _iter &a) {
100 value_type &operator*()
const {
return self->data[idx]; }
101 value_type *operator->()
const {
return &self->data[idx]; }
102 _iter &operator++() {
105 }
while (self->erased[idx]);
108 _iter &operator--() {
111 }
while (self->erased[idx]);
114 _iter operator++(
int) {
119 _iter operator--(
int) {
124 bool operator==(
const _iter &a)
const {
return self == a.self && idx == a.idx; }
125 bool operator!=(
const _iter &a)
const {
return self != a.self || idx != a.idx; }
126 operator _iter<const HVM, const VT>()
const {
127 return _iter<const HVM, const VT>(*self, idx);
132 typedef _iter<hvec_set, value_type> iterator;
133 typedef _iter<const hvec_set, const value_type> const_iterator;
134 iterator begin() {
return iterator(*
this, erased.ffz()); }
135 iterator end() {
return iterator(*
this, data.size()); }
136 const_iterator begin()
const {
return const_iterator(*
this, erased.ffz()); }
137 const_iterator end()
const {
return const_iterator(*
this, data.size()); }
138 const_iterator cbegin()
const {
return const_iterator(*
this, erased.ffz()); }
139 const_iterator cend()
const {
return const_iterator(*
this, data.size()); }
140 value_type &front()
const {
return *begin(); }
141 value_type &back()
const {
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_set &a)
const {
150 if (inuse != a.inuse)
return false;
153 if (el != *it++)
return false;
156 bool operator!=(
const hvec_set &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 template <
typename... KK>
179 std::pair<iterator, bool> emplace(KK &&...k) {
180 return insert(value_type(std::forward<KK>(k)...));
182 std::pair<iterator, bool> insert(
const value_type &v) {
183 bool new_key =
false;
184 size_t idx = hv_insert(&v);
185 if (idx >= data.size()) {
189 }
else if ((new_key = erased[idx])) {
193 return std::make_pair(iterator(*
this, idx), new_key);
195 std::pair<iterator, bool> insert(value_type &&v) {
196 bool new_key =
false;
197 size_t idx = hv_insert(&v);
198 if (idx >= data.size()) {
200 data.push_back(std::move(v));
202 }
else if ((new_key = erased[idx])) {
204 data[idx] = std::move(v);
206 return std::make_pair(iterator(*
this, idx), new_key);
209 template <
typename InputIterator>
210 void insert(InputIterator first, InputIterator last) {
211 for (; first != last; ++first) insert(*first);
213 void insert(std::initializer_list<value_type> vl) {
return insert(vl.begin(), vl.end()); }
214 template <
class HVM,
class VT>
215 _iter<HVM, VT> erase(_iter<HVM, VT> it) {
216 BUG_CHECK(
this == it.self,
"incorrect iterator for hvec_set::erase");
221 data[it.idx] = KEY();
226 size_t erase(
const KEY &k) {
227 size_t idx = remove(&k);
228 if (idx + 1 == 0)
return 0;
229 if (idx < data.size()) {
235 using hash_vector_base::dump;
239 std::vector<KEY, ALLOC> data;
240 size_t hashfn(
const void *a)
const override {
return hf(*
static_cast<const KEY *
>(a)); }
241 bool cmpfn(
const void *a,
const void *b)
const override {
242 return eql(*
static_cast<const KEY *
>(a), *
static_cast<const KEY *
>(b));
244 bool cmpfn(
const void *a,
size_t b)
const override {
245 return eql(*
static_cast<const KEY *
>(a), data[b]);
247 const void *getkey(uint32_t i)
const override {
return &data[i]; }
248 void *getval(uint32_t)
override {
249 BUG(
"getval in set");
252 uint32_t limit()
override {
return data.size(); }
253 void resizedata(
size_t sz)
override { data.resize(sz); }
254 void moveentry(
size_t to,
size_t from)
override { data[to] = data[from]; }