34 typedef const KEY value_type;
36 typedef PRED key_equal;
37 typedef ALLOC allocator_type;
38 typedef value_type *pointer, *const_pointer, &reference, &const_reference;
41 explicit hvec_set(
size_t icap = 0,
const hasher &hf = hasher(),
42 const key_equal &eql = key_equal(),
43 const allocator_type &a = allocator_type())
50 if (
this != std::addressof(that)) {
55 data.reserve(that.size());
56 insert(that.begin(), that.end());
64 hvec_set(ITER begin, ITER end,
const hasher &hf = hasher(),
const key_equal &eql = key_equal(),
65 const allocator_type &a = allocator_type())
67 data.reserve(end - begin);
68 for (
auto it = begin; it != end; ++it) insert(*it);
70 hvec_set(std::initializer_list<value_type> il,
const hasher &hf = hasher(),
71 const key_equal &eql = key_equal(),
const allocator_type &a = allocator_type())
73 data.reserve(il.size());
74 for (
auto &i : il) insert(i);
78 template <
class HVM,
class VT>
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_set, value_type> iterator;
131 typedef _iter<const hvec_set, 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()); }
138 value_type &front()
const {
return *begin(); }
139 value_type &back()
const {
144 bool empty()
const {
return inuse == 0; }
145 size_t size()
const {
return inuse; }
146 size_t max_size()
const {
return UINT32_MAX; }
147 bool operator==(
const hvec_set &a)
const {
148 if (inuse != a.inuse)
return false;
151 if (el != *it++)
return false;
154 bool operator!=(
const hvec_set &a)
const {
return !(*
this == a); }
156 hash_vector_base::clear();
160 iterator find(
const KEY &k) {
162 size_t idx = hash_vector_base::find(&k, &cache);
163 return idx ? iterator(*
this, idx - 1) : end();
165 const_iterator find(
const KEY &k)
const {
167 size_t idx = hash_vector_base::find(&k, &cache);
168 return idx ? const_iterator(*
this, idx - 1) : end();
170 size_t count(
const KEY &k)
const {
172 size_t idx = hash_vector_base::find(&k, &cache);
176 template <
typename... KK>
177 std::pair<iterator, bool> emplace(KK &&...k) {
178 return insert(value_type(std::forward<KK>(k)...));
180 std::pair<iterator, bool> insert(
const value_type &v) {
181 bool new_key =
false;
182 size_t idx = hv_insert(&v);
183 if (idx >= data.size()) {
187 }
else if ((new_key = erased[idx])) {
191 return std::make_pair(iterator(*
this, idx), new_key);
193 std::pair<iterator, bool> insert(value_type &&v) {
194 bool new_key =
false;
195 size_t idx = hv_insert(&v);
196 if (idx >= data.size()) {
198 data.push_back(std::move(v));
200 }
else if ((new_key = erased[idx])) {
202 data[idx] = std::move(v);
204 return std::make_pair(iterator(*
this, idx), new_key);
207 template <
typename InputIterator>
208 void insert(InputIterator first, InputIterator last) {
209 for (; first != last; ++first) insert(*first);
211 void insert(std::initializer_list<value_type> vl) {
return insert(vl.begin(), vl.end()); }
212 template <
class HVM,
class VT>
213 _iter<HVM, VT> erase(_iter<HVM, VT> it) {
214 BUG_CHECK(
this == it.self,
"incorrect iterator for hvec_set::erase");
219 data[it.idx] = KEY();
224 size_t erase(
const KEY &k) {
225 size_t idx = remove(&k);
226 if (idx + 1 == 0)
return 0;
227 if (idx < data.size()) {
233 using hash_vector_base::dump;
237 std::vector<KEY, ALLOC> data;
238 size_t hashfn(
const void *a)
const override {
return hf(*
static_cast<const KEY *
>(a)); }
239 bool cmpfn(
const void *a,
const void *b)
const override {
240 return eql(*
static_cast<const KEY *
>(a), *
static_cast<const KEY *
>(b));
242 bool cmpfn(
const void *a,
size_t b)
const override {
243 return eql(*
static_cast<const KEY *
>(a), data[b]);
245 const void *getkey(uint32_t i)
const override {
return &data[i]; }
246 void *getval(uint32_t)
override {
247 BUG(
"getval in set");
250 uint32_t limit()
override {
return data.size(); }
251 void resizedata(
size_t sz)
override { data.resize(sz); }
252 void moveentry(
size_t to,
size_t from)
override { data[to] = data[from]; }