31 #ifndef ETL_UNORDERED_SET_INCLUDED
32 #define ETL_UNORDERED_SET_INCLUDED
56 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
57 #include <initializer_list>
131 template <
typename TKey,
typename THash = etl::hash<TKey>,
typename TKeyEqual = etl::equal_to<TKey> >
136 typedef TKey value_type;
137 typedef TKey key_type;
138 typedef THash hasher;
139 typedef TKeyEqual key_equal;
140 typedef value_type& reference;
141 typedef const value_type& const_reference;
142 #if ETL_CPP11_SUPPORTED
143 typedef value_type&& rvalue_reference;
145 typedef value_type* pointer;
146 typedef const value_type* const_pointer;
147 typedef size_t size_type;
156 node_t(const_reference key_)
180 typedef typename iunordered_set::value_type value_type;
181 typedef typename iunordered_set::key_type key_type;
182 typedef typename iunordered_set::hasher hasher;
183 typedef typename iunordered_set::key_equal key_equal;
184 typedef typename iunordered_set::reference reference;
185 typedef typename iunordered_set::const_reference const_reference;
186 typedef typename iunordered_set::pointer pointer;
187 typedef typename iunordered_set::const_pointer const_pointer;
188 typedef typename iunordered_set::size_type size_type;
200 : pbuckets_end(other.pbuckets_end),
201 pbucket(other.pbucket),
212 if (inode == pbucket->
end())
216 while ((pbucket != pbuckets_end) && (pbucket->
empty()))
222 if (pbucket != pbuckets_end)
224 inode = pbucket->
begin();
242 pbuckets_end = other.pbuckets_end;
243 pbucket = other.pbucket;
249 reference operator *()
255 const_reference operator *()
const
263 return &(inode->key);
269 return &(inode->key);
273 pointer operator ->()
275 return &(inode->key);
279 const_pointer operator ->()
const
281 return &(inode->key);
287 return lhs.compare(rhs);
293 return !(lhs == rhs);
300 : pbuckets_end(pbuckets_end_),
309 return rhs.inode == inode;
319 bucket_t*& get_bucket_list_iterator()
340 typedef typename iunordered_set::value_type value_type;
341 typedef typename iunordered_set::key_type key_type;
342 typedef typename iunordered_set::hasher hasher;
343 typedef typename iunordered_set::key_equal key_equal;
344 typedef typename iunordered_set::reference reference;
345 typedef typename iunordered_set::const_reference const_reference;
346 typedef typename iunordered_set::pointer pointer;
347 typedef typename iunordered_set::const_pointer const_pointer;
348 typedef typename iunordered_set::size_type size_type;
360 : pbuckets_end(other.pbuckets_end),
361 pbucket(other.pbucket),
368 : pbuckets_end(other.pbuckets_end),
369 pbucket(other.pbucket),
380 if (inode == pbucket->
end())
385 while ((pbucket != pbuckets_end) && (pbucket->
empty()))
391 if (pbucket != pbuckets_end)
393 inode = pbucket->
begin();
411 pbuckets_end = other.pbuckets_end;
412 pbucket = other.pbucket;
418 const_reference operator *()
const
426 return &(inode->key);
430 const_pointer operator ->()
const
432 return &(inode->key);
438 return lhs.compare(rhs);
444 return !(lhs == rhs);
451 : pbuckets_end(pbuckets_end_),
460 return rhs.inode == inode;
470 bucket_t*& get_bucket_list_iterator()
486 typedef typename etl::iterator_traits<iterator>::difference_type difference_type;
494 return iterator(pbuckets + number_of_buckets, first, first->
begin());
521 return pbuckets[i].
begin();
530 return pbuckets[i].
cbegin();
539 return pbuckets[i].
cbegin();
548 return iterator(pbuckets + number_of_buckets, last, last->
end());
575 return pbuckets[i].
end();
584 return pbuckets[i].
cend();
593 return pbuckets[i].
cend();
602 return key_hash_function(key) % number_of_buckets;
611 size_t index = bucket(key);
613 return etl::distance(pbuckets[index].
begin(), pbuckets[index].
end());
622 return number_of_buckets;
631 return number_of_buckets;
641 template <
typename TIterator>
642 void assign(TIterator first_, TIterator last_)
644 #if defined(ETL_DEBUG)
645 difference_type d = etl::distance(first_, last_);
652 while (first_ != last_)
663 ETL_OR_STD::pair<iterator, bool>
insert(const_reference key)
665 ETL_OR_STD::pair<iterator, bool> result(
end(),
false);
673 bucket_t* pbucket = pbuckets + index;
680 node_t& node = create_data_node();
681 ::new (&node.key) value_type(key);
682 ETL_INCREMENT_DEBUG_COUNT
686 adjust_first_last_markers_after_insert(&bucket);
688 result.first =
iterator(pbuckets + number_of_buckets, pbucket, pbucket->
begin());
689 result.second =
true;
697 while (inode != bucket.
end())
700 if (inode->key == key)
710 if (inode == bucket.
end())
713 node_t& node = create_data_node();
714 ::new (&node.key) value_type(key);
715 ETL_INCREMENT_DEBUG_COUNT
719 adjust_first_last_markers_after_insert(&bucket);
722 result.first =
iterator(pbuckets + number_of_buckets, pbucket, inode_previous);
723 result.second =
true;
730 #if ETL_CPP11_SUPPORTED
736 ETL_OR_STD::pair<iterator, bool>
insert(rvalue_reference key)
738 ETL_OR_STD::pair<iterator, bool> result(
end(),
false);
746 bucket_t* pbucket = pbuckets + index;
747 bucket_t& bucket = *pbucket;
753 node_t& node = create_data_node();
754 ::new (&node.key) value_type(
etl::move(key));
755 ETL_INCREMENT_DEBUG_COUNT
758 bucket.insert_after(bucket.before_begin(), node);
759 adjust_first_last_markers_after_insert(&bucket);
761 result.first =
iterator(pbuckets + number_of_buckets, pbucket, pbucket->
begin());
762 result.second = true;
767 local_iterator inode_previous = bucket.before_begin();
768 local_iterator inode = bucket.begin();
770 while (inode != bucket.end())
773 if (inode->key == key)
783 if (inode == bucket.end())
786 node_t& node = create_data_node();
787 ::new (&node.key) value_type(
etl::move(key));
788 ETL_INCREMENT_DEBUG_COUNT
791 bucket.insert_after(inode_previous, node);
792 adjust_first_last_markers_after_insert(&bucket);
795 result.first = iterator(pbuckets + number_of_buckets, pbucket, inode_previous);
796 result.second = true;
815 #if ETL_CPP11_SUPPORTED
824 return insert(etl::move(key)).first;
835 template <
class TIterator>
836 void insert(TIterator first_, TIterator last_)
838 while (first_ != last_)
860 while ((icurrent != bucket.
end()) && (icurrent->key != key))
867 if (icurrent != bucket.
end())
870 icurrent->key.~value_type();
871 pnodepool->
release(&*icurrent);
872 adjust_first_last_markers_after_erase(&bucket);
874 ETL_DECREMENT_DEBUG_COUNT
887 iterator inext((pbuckets + number_of_buckets), ielement.get_bucket_list_iterator(), ielement.get_local_iterator());
890 bucket_t& bucket = ielement.get_bucket();
895 while (iprevious->etl_next != &*icurrent)
901 icurrent->key.~value_type();
902 pnodepool->
release(&*icurrent);
903 adjust_first_last_markers_after_erase(&bucket);
904 ETL_DECREMENT_DEBUG_COUNT
919 if ((first_ ==
begin()) && (last_ ==
end()))
926 iterator result((pbuckets + number_of_buckets), last_.get_bucket_list_iterator(), last_.get_local_iterator());
929 bucket_t* pbucket = first_.get_bucket_list_iterator();
930 bucket_t* pend_bucket = last_.get_bucket_list_iterator();
936 while (iprevious->etl_next != &*icurrent)
942 while ((icurrent != iend) || (pbucket != pend_bucket))
946 icurrent->key.~value_type();
947 pnodepool->
release(&*icurrent);
948 adjust_first_last_markers_after_erase(pbucket);
949 ETL_DECREMENT_DEBUG_COUNT
954 if ((icurrent != iend) || (pbucket != pend_bucket))
957 if ((icurrent == pbucket->
end()))
963 }
while (pbucket->
empty());
966 icurrent = pbucket->
begin();
989 return (
find(key) ==
end()) ? 0 : 1;
1001 bucket_t* pbucket = pbuckets + index;
1005 if (!bucket.
empty())
1011 while (inode != iend)
1014 if (key_equal_function(key, inode->key))
1016 return iterator(pbuckets + number_of_buckets, pbucket, inode);
1035 bucket_t* pbucket = pbuckets + index;
1039 if (!bucket.
empty())
1045 while (inode != iend)
1048 if (key_equal_function(key, inode->key))
1050 return iterator(pbuckets + number_of_buckets, pbucket, inode);
1078 return ETL_OR_STD::pair<iterator, iterator>(f, l);
1099 return ETL_OR_STD::pair<const_iterator, const_iterator>(f, l);
1107 return pnodepool->
size();
1131 return pnodepool->
empty();
1139 return pnodepool->
full();
1166 return key_hash_function;
1175 return key_equal_function;
1192 #if ETL_CPP11_SUPPORTED
1202 move(rhs.begin(), rhs.end());
1215 : pnodepool(&node_pool_),
1216 pbuckets(pbuckets_),
1217 number_of_buckets(number_of_buckets_)
1229 for (
size_t i = 0; i < number_of_buckets; ++i)
1233 if (!bucket.
empty())
1238 while (it != bucket.
end())
1241 it->key.~value_type();
1243 ETL_DECREMENT_DEBUG_COUNT
1259 #if ETL_CPP11_SUPPORTED
1265 #if defined(ETL_DEBUG)
1266 difference_type d = etl::distance(first, last);
1271 while (first != last)
1273 insert(etl::move(*first++));
1283 node_t& create_data_node()
1285 node_t* (
etl::ipool::*func)() = &etl::ipool::allocate<node_t>;
1286 return *(pnodepool->*func)();
1292 void adjust_first_last_markers_after_insert(bucket_t* pbucket)
1301 if (pbucket < first)
1305 else if (pbucket > last)
1315 void adjust_first_last_markers_after_erase(bucket_t* pbucket)
1324 if (pbucket == first)
1327 while (first->empty())
1332 else if (pbucket == last)
1335 bucket_t* pbucket = first;
1336 bucket_t* pend = last;
1340 while (pbucket != pend)
1342 if (!pbucket->empty())
1367 const size_t number_of_buckets;
1374 hasher key_hash_function;
1377 key_equal key_equal_function;
1380 ETL_DECLARE_DEBUG_COUNT
1385 #if defined(ETL_POLYMORPHIC_UNORDERED_SET) || defined(ETL_POLYMORPHIC_CONTAINERS)
1405 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
1418 template <
typename TKey,
typename TMapped,
typename TKeyCompare>
1421 return !(lhs == rhs);
1427 template <
typename TKey, const
size_t MAX_SIZE_,
size_t MAX_BUCKETS_ = MAX_SIZE_,
typename THash = etl::hash<TKey>,
typename TKeyEqual = etl::equal_to<TKey> >
1436 static const size_t MAX_SIZE = MAX_SIZE_;
1437 static const size_t MAX_BUCKETS = MAX_BUCKETS_;
1443 :
base(node_pool, buckets, MAX_BUCKETS)
1452 :
base(node_pool, buckets, MAX_BUCKETS)
1461 #if ETL_CPP11_SUPPORTED
1466 : base(node_pool, buckets, MAX_BUCKETS)
1471 base::move(other.begin(), other.end());
1482 template <
typename TIterator>
1484 :
base(node_pool, buckets, MAX_BUCKETS)
1486 base::assign(first_, last_);
1489 #if ETL_CPP11_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
1494 : base(node_pool, buckets, MAX_BUCKETS)
1496 base::assign(init.begin(), init.end());
1522 #if ETL_CPP11_SUPPORTED
1532 base::move(rhs.begin(), rhs.end());
1551 #if ETL_CPP17_SUPPORTED && ETL_NOT_USING_STLPORT && ETL_USING_STL
1552 template <
typename T,
typename... Ts>
1553 unordered_set(T, Ts...)
1554 ->unordered_set<etl::enable_if_t<(etl::is_same_v<T, Ts> && ...), T>, 1U +
sizeof...(Ts)>;
Definition: constant.h:45
const_iterator
Definition: intrusive_forward_list.h:423
iterator.
Definition: intrusive_forward_list.h:332
bool empty() const
Returns true if the list has no elements.
Definition: intrusive_forward_list.h:221
void clear()
Clears the intrusive_forward_list.
Definition: intrusive_forward_list.h:143
Definition: intrusive_forward_list.h:312
const_iterator cbegin() const
Gets the beginning of the intrusive_forward_list.
Definition: intrusive_forward_list.h:561
iterator erase_after(iterator position)
Erases the value at the specified position.
Definition: intrusive_forward_list.h:632
iterator insert_after(iterator position, value_type &value)
Inserts a value to the intrusive_forward_list after the specified position.
Definition: intrusive_forward_list.h:609
iterator end()
Gets the end of the intrusive_forward_list.
Definition: intrusive_forward_list.h:569
iterator before_begin()
Gets before the beginning of the intrusive_forward_list.
Definition: intrusive_forward_list.h:545
const_iterator cend() const
Gets the end of the intrusive_forward_list.
Definition: intrusive_forward_list.h:585
iterator begin()
Gets the beginning of the intrusive_forward_list.
Definition: intrusive_forward_list.h:529
Definition: unordered_set.h:337
Definition: unordered_set.h:177
A templated unordered_set implementation that uses a fixed size buffer.
Definition: unordered_set.h:1429
unordered_set()
Default constructor.
Definition: unordered_set.h:1442
unordered_set(TIterator first_, TIterator last_)
Definition: unordered_set.h:1483
~unordered_set()
Destructor.
Definition: unordered_set.h:1503
unordered_set(const unordered_set &other)
Copy constructor.
Definition: unordered_set.h:1451
bitset< MAXN > operator&(const bitset< MAXN > &lhs, const bitset< MAXN > &rhs)
Definition: bitset.h:1157
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
Definition: exception.h:47
size_t size() const
Returns the number of allocated items in the pool.
Definition: pool.h:309
bool empty() const
Definition: pool.h:318
void release_all()
Release all objects in the pool.
Definition: pool.h:264
bool full() const
Definition: pool.h:327
size_t max_size() const
Returns the maximum number of items in the pool.
Definition: pool.h:285
void release(const void *const p_object)
Definition: pool.h:255
size_t available() const
Returns the number of free items in the pool.
Definition: pool.h:301
size_t count(key_parameter_t key) const
Definition: unordered_set.h:987
key_equal key_eq() const
Definition: unordered_set.h:1173
iterator erase(const_iterator first_, const_iterator last_)
Definition: unordered_set.h:916
local_iterator end(size_t i)
Definition: unordered_set.h:573
size_type bucket_count() const
Definition: unordered_set.h:629
size_type max_size() const
Gets the maximum possible size of the unordered_set.
Definition: unordered_set.h:1113
size_type size() const
Gets the size of the unordered_set.
Definition: unordered_set.h:1105
local_iterator begin(size_t i)
Definition: unordered_set.h:519
void insert(TIterator first_, TIterator last_)
Definition: unordered_set.h:836
local_const_iterator cend(size_t i) const
Definition: unordered_set.h:591
float load_factor() const
Definition: unordered_set.h:1155
const_iterator begin() const
Definition: unordered_set.h:501
bool full() const
Checks to see if the unordered_set is full.
Definition: unordered_set.h:1137
iunordered_set & operator=(const iunordered_set &rhs)
Assignment operator.
Definition: unordered_set.h:1181
size_type bucket_size(key_parameter_t key) const
Definition: unordered_set.h:609
~iunordered_set()
For library debugging purposes only.
Definition: unordered_set.h:1392
size_type get_bucket_index(key_parameter_t key) const
Definition: unordered_set.h:600
const_iterator end() const
Definition: unordered_set.h:555
void clear()
Clears the unordered_set.
Definition: unordered_set.h:977
ETL_OR_STD::pair< const_iterator, const_iterator > equal_range(key_parameter_t key) const
Definition: unordered_set.h:1089
size_t available() const
Definition: unordered_set.h:1146
iterator insert(const_iterator, const_reference key)
Definition: unordered_set.h:810
void assign(TIterator first_, TIterator last_)
Definition: unordered_set.h:642
iterator find(key_parameter_t key)
Definition: unordered_set.h:997
ETL_OR_STD::pair< iterator, iterator > equal_range(key_parameter_t key)
Definition: unordered_set.h:1068
iterator end()
Definition: unordered_set.h:546
size_t erase(key_parameter_t key)
Definition: unordered_set.h:849
iunordered_set(pool_t &node_pool_, bucket_t *pbuckets_, size_t number_of_buckets_)
Constructor.
Definition: unordered_set.h:1214
const_iterator find(key_parameter_t key) const
Definition: unordered_set.h:1031
hasher hash_function() const
Definition: unordered_set.h:1164
local_const_iterator end(size_t i) const
Definition: unordered_set.h:582
bool empty() const
Checks to see if the unordered_set is empty.
Definition: unordered_set.h:1129
size_type max_bucket_count() const
Definition: unordered_set.h:620
ETL_OR_STD::pair< iterator, bool > insert(const_reference key)
Definition: unordered_set.h:663
local_const_iterator begin(size_t i) const
Definition: unordered_set.h:528
iterator begin()
Definition: unordered_set.h:492
local_const_iterator cbegin(size_t i) const
Definition: unordered_set.h:537
const_iterator cend() const
Definition: unordered_set.h:564
void initialise()
Initialise the unordered_set.
Definition: unordered_set.h:1224
size_type capacity() const
Gets the maximum possible size of the unordered_set.
Definition: unordered_set.h:1121
const_iterator cbegin() const
Definition: unordered_set.h:510
iterator erase(const_iterator ielement)
Definition: unordered_set.h:884
Definition: unordered_set.h:133
Definition: unordered_set.h:76
Definition: unordered_set.h:90
Definition: unordered_set.h:117
Definition: unordered_set.h:104
bool operator!=(const etl::iunordered_set< TKey, TMapped, TKeyCompare > &lhs, const etl::iunordered_set< TKey, TMapped, TKeyCompare > &rhs)
Definition: unordered_set.h:1419
bool operator==(const etl::iunordered_set< TKey, TMapped, TKeyCompare > &lhs, const etl::iunordered_set< TKey, TMapped, TKeyCompare > &rhs)
Definition: unordered_set.h:1406
Definition: absolute.h:37
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:582
A forward link.
Definition: intrusive_links.h:92
iterator
Definition: iterator.h:422
Definition: unordered_set.h:155