Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarFlatHashSet.hpp
blob: 350b03a94a4822a029b0a083d9d8bcf10d7635b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#pragma once

#include "StarFlatHashTable.hpp"
#include "StarHash.hpp"

namespace Star {

template <typename Key, typename Hash = hash<Key>, typename Equals = std::equal_to<Key>, typename Allocator = std::allocator<Key>>
class FlatHashSet {
public:
  typedef Key key_type;
  typedef Key value_type;
  typedef size_t size_type;
  typedef ptrdiff_t difference_type;
  typedef Hash hasher;
  typedef Equals key_equal;
  typedef Allocator allocator_type;
  typedef value_type& reference;
  typedef value_type const& const_reference;
  typedef value_type* pointer;
  typedef value_type const* const_pointer;

private:
  struct GetKey {
    key_type const& operator()(value_type const& value) const;
  };

  typedef FlatHashTable<Key, Key, GetKey, Hash, Equals, Allocator> Table;

public:
  struct const_iterator {
    typedef std::forward_iterator_tag iterator_category;
    typedef typename FlatHashSet::value_type const value_type;
    typedef ptrdiff_t difference_type;
    typedef value_type* pointer;
    typedef value_type& reference;

    bool operator==(const_iterator const& rhs) const;
    bool operator!=(const_iterator const& rhs) const;

    const_iterator& operator++();
    const_iterator operator++(int);

    value_type& operator*() const;
    value_type* operator->() const;

    typename Table::const_iterator inner;
  };

  struct iterator {
    typedef std::forward_iterator_tag iterator_category;
    typedef typename FlatHashSet::value_type value_type;
    typedef ptrdiff_t difference_type;
    typedef value_type* pointer;
    typedef value_type& reference;

    bool operator==(iterator const& rhs) const;
    bool operator!=(iterator const& rhs) const;

    iterator& operator++();
    iterator operator++(int);

    value_type& operator*() const;
    value_type* operator->() const;

    operator const_iterator() const;

    typename Table::iterator inner;
  };

  FlatHashSet();
  explicit FlatHashSet(size_t bucketCount, hasher const& hash = hasher(),
      key_equal const& equal = key_equal(), allocator_type const& alloc = allocator_type());
  FlatHashSet(size_t bucketCount, allocator_type const& alloc);
  FlatHashSet(size_t bucketCount, hasher const& hash, allocator_type const& alloc);
  explicit FlatHashSet(allocator_type const& alloc);

  template <typename InputIt>
  FlatHashSet(InputIt first, InputIt last, size_t bucketCount = 0,
      hasher const& hash = hasher(), key_equal const& equal = key_equal(),
      allocator_type const& alloc = allocator_type());
  template <typename InputIt>
  FlatHashSet(InputIt first, InputIt last, size_t bucketCount, allocator_type const& alloc);
  template <typename InputIt>
  FlatHashSet(InputIt first, InputIt last, size_t bucketCount,
      hasher const& hash, allocator_type const& alloc);

  FlatHashSet(FlatHashSet const& other);
  FlatHashSet(FlatHashSet const& other, allocator_type const& alloc);
  FlatHashSet(FlatHashSet&& other);
  FlatHashSet(FlatHashSet&& other, allocator_type const& alloc);

  FlatHashSet(initializer_list<value_type> init, size_t bucketCount = 0,
      hasher const& hash = hasher(), key_equal const& equal = key_equal(),
      allocator_type const& alloc = allocator_type());
  FlatHashSet(initializer_list<value_type> init, size_t bucketCount, allocator_type const& alloc);
  FlatHashSet(initializer_list<value_type> init, size_t bucketCount, hasher const& hash,
      allocator_type const& alloc);

  FlatHashSet& operator=(FlatHashSet const& other);
  FlatHashSet& operator=(FlatHashSet&& other);
  FlatHashSet& operator=(initializer_list<value_type> init);

  iterator begin();
  iterator end();

  const_iterator begin() const;
  const_iterator end() const;

  const_iterator cbegin() const;
  const_iterator cend() const;

  size_t empty() const;
  size_t size() const;
  void clear();

  pair<iterator, bool> insert(value_type const& value);
  pair<iterator, bool> insert(value_type&& value);
  iterator insert(const_iterator hint, value_type const& value);
  iterator insert(const_iterator hint, value_type&& value);
  template <typename InputIt>
  void insert(InputIt first, InputIt last);
  void insert(initializer_list<value_type> init);

  template <typename... Args>
  pair<iterator, bool> emplace(Args&&... args);
  template <typename... Args>
  iterator emplace_hint(const_iterator hint, Args&&... args);

  iterator erase(const_iterator pos);
  iterator erase(const_iterator first, const_iterator last);
  size_t erase(key_type const& key);

  size_t count(key_type const& key) const;
  const_iterator find(key_type const& key) const;
  iterator find(key_type const& key);
  pair<iterator, iterator> equal_range(key_type const& key);
  pair<const_iterator, const_iterator> equal_range(key_type const& key) const;

  void reserve(size_t capacity);

  bool operator==(FlatHashSet const& rhs) const;
  bool operator!=(FlatHashSet const& rhs) const;

private:
  Table m_table;
};

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::GetKey::operator()(value_type const& value) const -> key_type const& {
  return value;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
bool FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator::operator==(const_iterator const& rhs) const {
  return inner == rhs.inner;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
bool FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator::operator!=(const_iterator const& rhs) const {
  return inner != rhs.inner;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator::operator++() -> const_iterator& {
  ++inner;
  return *this;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator::operator++(int) -> const_iterator {
  const_iterator copy(*this);
  operator++();
  return copy;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator::operator*() const -> value_type& {
  return *inner;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator::operator->() const -> value_type* {
  return &operator*();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
bool FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator==(iterator const& rhs) const {
  return inner == rhs.inner;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
bool FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator!=(iterator const& rhs) const {
  return inner != rhs.inner;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator++() -> iterator& {
  ++inner;
  return *this;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator++(int) -> iterator {
  iterator copy(*this);
  operator++();
  return copy;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator*() const -> value_type& {
  return *inner;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator->() const -> value_type* {
  return &operator*();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::iterator::operator typename FlatHashSet<Key, Hash, Equals, Allocator>::const_iterator() const {
  return const_iterator{inner};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet()
  : FlatHashSet(0) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(size_t bucketCount, hasher const& hash,
    key_equal const& equal, allocator_type const& alloc)
  : m_table(bucketCount, GetKey(), hash, equal, alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(size_t bucketCount, allocator_type const& alloc)
  : FlatHashSet(bucketCount, hasher(), key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(size_t bucketCount, hasher const& hash,
    allocator_type const& alloc)
  : FlatHashSet(bucketCount, hash, key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(allocator_type const& alloc)
  : FlatHashSet(0, hasher(), key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
template <typename InputIt>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(InputIt first, InputIt last, size_t bucketCount,
    hasher const& hash, key_equal const& equal, allocator_type const& alloc)
  : FlatHashSet(bucketCount, hash, equal, alloc) {
  insert(first, last);
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
template <typename InputIt>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(InputIt first, InputIt last, size_t bucketCount,
    allocator_type const& alloc)
  : FlatHashSet(first, last, bucketCount, hasher(), key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
template <typename InputIt>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(InputIt first, InputIt last, size_t bucketCount,
    hasher const& hash, allocator_type const& alloc)
  : FlatHashSet(first, last, bucketCount, hash, key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(FlatHashSet const& other)
  : FlatHashSet(other, other.m_table.getAllocator()) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(FlatHashSet const& other, allocator_type const& alloc)
  : FlatHashSet(alloc) {
  operator=(other);
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(FlatHashSet&& other)
  : FlatHashSet(std::move(other), other.m_table.getAllocator()) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(FlatHashSet&& other, allocator_type const& alloc)
  : FlatHashSet(alloc) {
  operator=(std::move(other));
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(initializer_list<value_type> init, size_t bucketCount,
    hasher const& hash, key_equal const& equal, allocator_type const& alloc)
  : FlatHashSet(bucketCount, hash, equal, alloc) {
  operator=(init);
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(initializer_list<value_type> init, size_t bucketCount, allocator_type const& alloc)
  : FlatHashSet(init, bucketCount, hasher(), key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>::FlatHashSet(initializer_list<value_type> init, size_t bucketCount,
    hasher const& hash, allocator_type const& alloc)
  : FlatHashSet(init, bucketCount, hash, key_equal(), alloc) {}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>& FlatHashSet<Key, Hash, Equals, Allocator>::operator=(FlatHashSet const& other) {
  m_table.clear();
  m_table.reserve(other.size());
  for (auto const& p : other)
    m_table.insert(p);
  return *this;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>& FlatHashSet<Key, Hash, Equals, Allocator>::operator=(FlatHashSet&& other) {
  m_table = std::move(other.m_table);
  return *this;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
FlatHashSet<Key, Hash, Equals, Allocator>& FlatHashSet<Key, Hash, Equals, Allocator>::operator=(initializer_list<value_type> init) {
  clear();
  insert(init.begin(), init.end());
  return *this;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::begin() -> iterator {
  return iterator{m_table.begin()};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::end() -> iterator {
  return iterator{m_table.end()};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::begin() const -> const_iterator {
  return const_iterator{m_table.begin()};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::end() const -> const_iterator {
  return const_iterator{m_table.end()};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::cbegin() const -> const_iterator {
  return begin();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::cend() const -> const_iterator {
  return end();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
size_t FlatHashSet<Key, Hash, Equals, Allocator>::empty() const {
  return m_table.empty();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
size_t FlatHashSet<Key, Hash, Equals, Allocator>::size() const {
  return m_table.size();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
void FlatHashSet<Key, Hash, Equals, Allocator>::clear() {
  m_table.clear();
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::insert(value_type const& value) -> pair<iterator, bool> {
  auto res = m_table.insert(value);
  return {iterator{res.first}, res.second};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::insert(value_type&& value) -> pair<iterator, bool> {
  auto res = m_table.insert(std::move(value));
  return {iterator{res.first}, res.second};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::insert(const_iterator i, value_type const& value) -> iterator {
  return insert(i, value_type(value));
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::insert(const_iterator, value_type&& value) -> iterator {
  return insert(std::move(value)).first;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
template <typename InputIt>
void FlatHashSet<Key, Hash, Equals, Allocator>::insert(InputIt first, InputIt last) {
  m_table.reserve(m_table.size() + std::distance(first, last));
  for (auto i = first; i != last; ++i)
    m_table.insert(*i);
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
void FlatHashSet<Key, Hash, Equals, Allocator>::insert(initializer_list<value_type> init) {
  insert(init.begin(), init.end());
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
template <typename... Args>
auto FlatHashSet<Key, Hash, Equals, Allocator>::emplace(Args&&... args) -> pair<iterator, bool> {
  return insert(value_type(forward<Args>(args)...));
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
template <typename... Args>
auto FlatHashSet<Key, Hash, Equals, Allocator>::emplace_hint(const_iterator i, Args&&... args) -> iterator {
  return insert(i, value_type(forward<Args>(args)...));
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::erase(const_iterator pos) -> iterator {
  return iterator{m_table.erase(pos.inner)};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::erase(const_iterator first, const_iterator last) -> iterator {
  return iterator{m_table.erase(first.inner, last.inner)};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
size_t FlatHashSet<Key, Hash, Equals, Allocator>::erase(key_type const& key) {
  auto i = m_table.find(key);
  if (i != m_table.end()) {
    m_table.erase(i);
    return 1;
  }
  return 0;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
size_t FlatHashSet<Key, Hash, Equals, Allocator>::count(Key const& key) const {
  if (m_table.find(key) != m_table.end())
    return 1;
  else
    return 0;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::find(key_type const& key) const -> const_iterator {
  return const_iterator{m_table.find(key)};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::find(key_type const& key) -> iterator {
  return iterator{m_table.find(key)};
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::equal_range(key_type const& key) -> pair<iterator, iterator> {
  auto i = find(key);
  if (i != end()) {
    auto j = i;
    ++j;
    return {i, j};
  } else {
    return {i, i};
  }
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
auto FlatHashSet<Key, Hash, Equals, Allocator>::equal_range(key_type const& key) const -> pair<const_iterator, const_iterator> {
  auto i = find(key);
  if (i != end()) {
    auto j = i;
    ++j;
    return {i, j};
  } else {
    return {i, i};
  }
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
void FlatHashSet<Key, Hash, Equals, Allocator>::reserve(size_t capacity) {
  m_table.reserve(capacity);
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
bool FlatHashSet<Key, Hash, Equals, Allocator>::operator==(FlatHashSet const& rhs) const {
  return m_table == rhs.m_table;
}

template <typename Key, typename Hash, typename Equals, typename Allocator>
bool FlatHashSet<Key, Hash, Equals, Allocator>::operator!=(FlatHashSet const& rhs) const {
  return m_table != rhs.m_table;
}

}