24 #ifndef TSL_ARRAY_SET_H 25 #define TSL_ARRAY_SET_H 29 #include <initializer_list> 32 #include <type_traits> 63 bool StoreNullTerminator =
true,
64 class KeySizeT = std::uint16_t,
65 class IndexSizeT = std::uint32_t,
73 KeySizeT, IndexSizeT, GrowthPolicy>;
76 using char_type =
typename ht::char_type;
77 using key_size_type =
typename ht::key_size_type;
78 using index_size_type =
typename ht::index_size_type;
79 using size_type =
typename ht::size_type;
80 using hasher =
typename ht::hasher;
81 using key_equal =
typename ht::key_equal;
82 using iterator =
typename ht::iterator;
83 using const_iterator =
typename ht::const_iterator;
89 const Hash& hash = Hash()): m_ht(bucket_count, hash, ht::DEFAULT_MAX_LOAD_FACTOR)
93 template<
class InputIt,
typename std::enable_if<is_iterator<InputIt>::value>::type* =
nullptr>
95 size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
96 const Hash& hash = Hash()):
array_set(bucket_count, hash)
102 #ifdef TSL_AH_HAS_STRING_VIEW 110 array_set(std::initializer_list<
const CharT*> init,
111 size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
112 const Hash& hash = Hash()):
array_set(bucket_count, hash)
120 #ifdef TSL_AH_HAS_STRING_VIEW 130 array_set& operator=(std::initializer_list<
const CharT*> ilist) {
143 iterator
begin()
noexcept {
return m_ht.begin(); }
144 const_iterator
begin()
const noexcept {
return m_ht.begin(); }
145 const_iterator
cbegin()
const noexcept {
return m_ht.cbegin(); }
147 iterator
end()
noexcept {
return m_ht.end(); }
148 const_iterator
end()
const noexcept {
return m_ht.end(); }
149 const_iterator
cend()
const noexcept {
return m_ht.cend(); }
155 bool empty()
const noexcept {
return m_ht.empty(); }
156 size_type
size()
const noexcept {
return m_ht.size(); }
157 size_type
max_size()
const noexcept {
return m_ht.max_size(); }
158 size_type
max_key_size()
const noexcept {
return m_ht.max_key_size(); }
165 void clear()
noexcept { m_ht.clear(); }
169 #ifdef TSL_AH_HAS_STRING_VIEW 174 std::pair<iterator,
bool> insert(
const CharT* key) {
175 return m_ht.emplace(key, std::char_traits<CharT>::length(key));
178 std::pair<iterator,
bool> insert(
const std::basic_string<CharT>& key) {
179 return m_ht.emplace(key.data(), key.size());
182 std::pair<iterator,
bool>
insert_ks(
const CharT* key, size_type key_size) {
183 return m_ht.emplace(key, key_size);
188 template<
class InputIt,
typename std::enable_if<is_iterator<InputIt>::value>::type* =
nullptr>
189 void insert(InputIt first, InputIt last) {
190 if(std::is_base_of<std::forward_iterator_tag,
191 typename std::iterator_traits<InputIt>::iterator_category>::value)
193 const auto nb_elements_insert = std::distance(first, last);
194 const std::size_t nb_free_buckets = std::size_t(
float(
bucket_count())*max_load_factor()) -
size();
196 if(nb_elements_insert > 0 && nb_free_buckets < std::size_t(nb_elements_insert)) {
201 for(
auto it = first; it != last; ++it) {
208 #ifdef TSL_AH_HAS_STRING_VIEW 213 void insert(std::initializer_list<
const CharT*> ilist) {
214 insert(ilist.begin(), ilist.end());
220 #ifdef TSL_AH_HAS_STRING_VIEW 231 std::pair<iterator,
bool> emplace(
const CharT* key) {
232 return m_ht.emplace(key, std::char_traits<CharT>::length(key));
238 std::pair<iterator,
bool> emplace(
const std::basic_string<CharT>& key) {
239 return m_ht.emplace(key.data(), key.size());
245 std::pair<iterator,
bool>
emplace_ks(
const CharT* key, size_type key_size) {
246 return m_ht.emplace(key, key_size);
251 iterator
erase(const_iterator pos) {
return m_ht.erase(pos); }
252 iterator
erase(const_iterator first, const_iterator last) {
return m_ht.erase(first, last); }
254 #ifdef TSL_AH_HAS_STRING_VIEW 259 size_type erase(
const CharT* key) {
260 return m_ht.erase(key, std::char_traits<CharT>::length(key));
263 size_type erase(
const std::basic_string<CharT>& key) {
264 return m_ht.erase(key.data(), key.size());
267 size_type
erase_ks(
const CharT* key, size_type key_size) {
268 return m_ht.erase(key, key_size);
273 #ifdef TSL_AH_HAS_STRING_VIEW 284 size_type erase(
const CharT* key, std::size_t precalculated_hash) {
285 return m_ht.erase(key, std::char_traits<CharT>::length(key), precalculated_hash);
291 size_type erase(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
292 return m_ht.erase(key.data(), key.size(), precalculated_hash);
299 size_type
erase_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
300 return m_ht.erase(key, key_size, precalculated_hash);
312 #ifdef TSL_AH_HAS_STRING_VIEW 315 size_type count(
const CharT* key)
const {
return m_ht.count(key, std::char_traits<CharT>::length(key)); }
316 size_type count(
const std::basic_string<CharT>& key)
const {
return m_ht.count(key.data(), key.size()); }
318 size_type
count_ks(
const CharT* key, size_type key_size)
const {
return m_ht.count(key, key_size); }
322 #ifdef TSL_AH_HAS_STRING_VIEW 333 size_type count(
const CharT* key, std::size_t precalculated_hash)
const {
334 return m_ht.count(key, std::char_traits<CharT>::length(key), precalculated_hash);
340 size_type count(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
341 return m_ht.count(key.data(), key.size(), precalculated_hash);
348 size_type
count_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
349 return m_ht.count(key, key_size, precalculated_hash);
354 #ifdef TSL_AH_HAS_STRING_VIEW 363 iterator find(
const CharT* key) {
364 return m_ht.find(key, std::char_traits<CharT>::length(key));
367 const_iterator find(
const CharT* key)
const {
368 return m_ht.find(key, std::char_traits<CharT>::length(key));
371 iterator find(
const std::basic_string<CharT>& key) {
372 return m_ht.find(key.data(), key.size());
375 const_iterator find(
const std::basic_string<CharT>& key)
const {
376 return m_ht.find(key.data(), key.size());
379 iterator
find_ks(
const CharT* key, size_type key_size) {
380 return m_ht.find(key, key_size);
383 const_iterator
find_ks(
const CharT* key, size_type key_size)
const {
384 return m_ht.find(key, key_size);
389 #ifdef TSL_AH_HAS_STRING_VIEW 407 iterator find(
const CharT* key, std::size_t precalculated_hash) {
408 return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
414 const_iterator find(
const CharT* key, std::size_t precalculated_hash)
const {
415 return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
421 iterator find(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
422 return m_ht.find(key.data(), key.size(), precalculated_hash);
428 const_iterator find(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
429 return m_ht.find(key.data(), key.size(), precalculated_hash);
436 iterator
find_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
437 return m_ht.find(key, key_size, precalculated_hash);
443 const_iterator
find_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
444 return m_ht.find(key, key_size, precalculated_hash);
449 #ifdef TSL_AH_HAS_STRING_VIEW 458 std::pair<iterator, iterator> equal_range(
const CharT* key) {
459 return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
462 std::pair<const_iterator, const_iterator> equal_range(
const CharT* key)
const {
463 return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
466 std::pair<iterator, iterator> equal_range(
const std::basic_string<CharT>& key) {
467 return m_ht.equal_range(key.data(), key.size());
470 std::pair<const_iterator, const_iterator> equal_range(
const std::basic_string<CharT>& key)
const {
471 return m_ht.equal_range(key.data(), key.size());
474 std::pair<iterator, iterator>
equal_range_ks(
const CharT* key, size_type key_size) {
475 return m_ht.equal_range(key, key_size);
478 std::pair<const_iterator, const_iterator>
equal_range_ks(
const CharT* key, size_type key_size)
const {
479 return m_ht.equal_range(key, key_size);
484 #ifdef TSL_AH_HAS_STRING_VIEW 502 std::pair<iterator, iterator> equal_range(
const CharT* key, std::size_t precalculated_hash) {
503 return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
509 std::pair<const_iterator, const_iterator> equal_range(
const CharT* key, std::size_t precalculated_hash)
const {
510 return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
516 std::pair<iterator, iterator> equal_range(
const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
517 return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
523 std::pair<const_iterator, const_iterator> equal_range(
const std::basic_string<CharT>& key, std::size_t precalculated_hash)
const {
524 return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
531 std::pair<iterator, iterator>
equal_range_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash) {
532 return m_ht.equal_range(key, key_size, precalculated_hash);
538 std::pair<const_iterator, const_iterator>
equal_range_ks(
const CharT* key, size_type key_size, std::size_t precalculated_hash)
const {
539 return m_ht.equal_range(key, key_size, precalculated_hash);
558 void rehash(size_type count) { m_ht.rehash(count); }
559 void reserve(size_type count) { m_ht.reserve(count); }
566 key_equal
key_eq()
const {
return m_ht.key_eq(); }
575 iterator
mutable_iterator(const_iterator it)
noexcept {
return m_ht.mutable_iterator(it); }
587 template<
class Serializer>
589 m_ht.serialize(serializer);
610 template<
class Deserializer>
613 set.m_ht.deserialize(deserializer, hash_compatible);
619 if(lhs.size() != rhs.size()) {
623 for(
auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
624 const auto it_element_rhs = rhs.find_ks(it.key(), it.key_size());
625 if(it_element_rhs == rhs.cend()) {
634 return !operator==(lhs, rhs);
653 template<
class CharT,
656 bool StoreNullTerminator =
true,
657 class KeySizeT = std::uint16_t,
658 class IndexSizeT = std::uint32_t>
659 using array_pg_set =
array_set<CharT, Hash, KeyEqual, StoreNullTerminator,
iterator find_ks(const CharT *key, size_type key_size)
Definition: array_set.h:379
bool empty() const noexcept
Definition: array_set.h:155
std::pair< const_iterator, const_iterator > equal_range_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_set.h:538
void reserve(size_type count)
Definition: array_set.h:559
iterator find_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_set.h:436
const_iterator end() const noexcept
Definition: array_set.h:148
friend bool operator==(const array_set &lhs, const array_set &rhs)
Definition: array_set.h:618
array_set()
Definition: array_set.h:85
Definition: array_growth_policy.h:40
void rehash(size_type count)
Definition: array_set.h:558
Definition: array_growth_policy.h:39
void max_load_factor(float ml)
Definition: array_set.h:556
void serialize(Serializer &serializer) const
Definition: array_set.h:588
void clear() noexcept
Definition: array_set.h:165
Definition: array_hash.h:117
void shrink_to_fit()
Definition: array_set.h:159
const_iterator find_ks(const CharT *key, size_type key_size) const
Definition: array_set.h:383
size_type count_ks(const CharT *key, size_type key_size) const
Definition: array_set.h:318
iterator begin() noexcept
Definition: array_set.h:143
Definition: array_hash.h:102
iterator erase(const_iterator first, const_iterator last)
Definition: array_set.h:252
std::pair< iterator, bool > insert_ks(const CharT *key, size_type key_size)
Definition: array_set.h:182
hasher hash_function() const
Definition: array_set.h:565
std::pair< iterator, iterator > equal_range_ks(const CharT *key, size_type key_size)
Definition: array_set.h:474
size_type erase_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_set.h:299
array_set(size_type bucket_count, const Hash &hash=Hash())
Definition: array_set.h:88
std::pair< iterator, iterator > equal_range_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash)
Definition: array_set.h:531
const_iterator cbegin() const noexcept
Definition: array_set.h:145
friend void swap(array_set &lhs, array_set &rhs)
Definition: array_set.h:637
iterator mutable_iterator(const_iterator it) noexcept
Definition: array_set.h:575
size_type max_size() const noexcept
Definition: array_set.h:157
size_type erase_ks(const CharT *key, size_type key_size)
Definition: array_set.h:267
float load_factor() const
Definition: array_set.h:554
iterator erase(const_iterator pos)
Definition: array_set.h:251
std::size_t operator()(const CharT *key, std::size_t key_size) const
Definition: array_hash.h:79
float max_load_factor() const
Definition: array_set.h:555
const_iterator find_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_set.h:443
Definition: array_growth_policy.h:49
Definition: array_growth_policy.h:247
void swap(array_set &other)
Definition: array_set.h:305
static array_set deserialize(Deserializer &deserializer, bool hash_compatible=false)
Definition: array_set.h:611
std::pair< const_iterator, const_iterator > equal_range_ks(const CharT *key, size_type key_size) const
Definition: array_set.h:478
const_iterator begin() const noexcept
Definition: array_set.h:144
static const size_type MAX_KEY_SIZE
Definition: array_set.h:642
const_iterator cend() const noexcept
Definition: array_set.h:149
size_type count_ks(const CharT *key, size_type key_size, std::size_t precalculated_hash) const
Definition: array_set.h:348
size_type max_key_size() const noexcept
Definition: array_set.h:158
key_equal key_eq() const
Definition: array_set.h:566
size_type max_bucket_count() const
Definition: array_set.h:548
size_type bucket_count() const
Definition: array_set.h:547
size_type size() const noexcept
Definition: array_set.h:156
std::pair< const_iterator, const_iterator > equal_range(const std::basic_string_view< CharT > &key, std::size_t precalculated_hash) const
Definition: array_set.h:495
Definition: array_hash.h:742
std::pair< iterator, bool > emplace_ks(const CharT *key, size_type key_size)
Definition: array_set.h:245
array_set(InputIt first, InputIt last, size_type bucket_count=ht::DEFAULT_INIT_BUCKET_COUNT, const Hash &hash=Hash())
Definition: array_set.h:94
friend bool operator!=(const array_set &lhs, const array_set &rhs)
Definition: array_set.h:633
iterator end() noexcept
Definition: array_set.h:147
void insert(InputIt first, InputIt last)
Definition: array_set.h:189