12 #ifndef ROC_CORE_HASHMAP_H_
13 #define ROC_CORE_HASHMAP_H_
84 size_t EmbeddedCapacity = 0,
85 template <
class TT>
class OwnershipPolicy = RefCountedOwnership,
86 class Node = HashmapNode<> >
92 typedef typename OwnershipPolicy<T>::Pointer
Pointer;
98 : impl_(embedded_buckets_.memory(), NumEmbeddedBuckets, arena) {
105 while (data != NULL) {
107 T* elem = from_node_data_(data);
108 OwnershipPolicy<T>::release(*elem);
109 data = impl_.
front();
153 hash, (
const void*)&key,
158 return from_node_data_(data);
170 return from_node_data_(data);
182 return from_node_data_(node);
200 return from_node_data_(next_data);
218 return from_node_data_(prev_data);
245 if (!insert_(elem.key(), data)) {
248 OwnershipPolicy<T>::acquire(elem);
267 impl_.
remove(data,
false);
268 OwnershipPolicy<T>::release(elem);
294 NumEmbeddedBuckets = ((int)(EmbeddedCapacity == 0 ? 0
295 : EmbeddedCapacity <= 16 ? 16
297 * HashmapImpl::LoadFactorDen
298 + HashmapImpl::LoadFactorNum - 1)
299 / HashmapImpl::LoadFactorNum * 2
302 static HashmapData* to_node_data_(
const T& elem) {
303 return static_cast<const Node&
>(elem).hashmap_data();
306 static T* from_node_data_(HashmapData* data) {
307 return static_cast<T*
>(
static_cast<Node*
>(Node::hashmap_node(data)));
310 template <
class Key>
static bool key_equal_(HashmapData* node,
const void* key) {
311 T* elem = from_node_data_(node);
312 const Key& key_ref = *(
const Key*)key;
313 return T::key_equal(elem->key(), key_ref);
316 template <
class Key>
bool insert_(
const Key& key, HashmapData* node) {
319 node, hash, (
const void*)&key,
320 &Hashmap<T, EmbeddedCapacity, OwnershipPolicy, Node>::key_equal_<Key>);
323 AlignedStorage<NumEmbeddedBuckets *
sizeof(HashmapImpl::Bucket)> embedded_buckets_;
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
HashmapData * front() const
Get first node in hashmap.
bool insert(HashmapData *node, hashsum_t hash, const void *key, key_equals_callback callback)
Insert node into hashmap.
size_t size() const
Get number of nodes added to hashmap.
HashmapData * prevof(HashmapData *node) const
Get hashmap node previous to given one.
HashmapData * nextof(HashmapData *node) const
Get hashmap node next to given one.
HashmapData * back() const
Get last node in hashmap.
HashmapData * find_node(hashsum_t hash, const void *key, key_equals_callback callback) const
Find node in the hashmap.
void remove(HashmapData *node, bool skip_rehash)
Remove node from hashmap.
ROC_ATTR_NODISCARD bool grow()
Grow hashtable capacity.
bool contains(const HashmapData *node) const
Check if node belongs to hashmap.
size_t capacity() const
Get maximum number of nodes that can be added to hashmap before grow() should be called.
bool contains(const T &elem) const
Check if element belongs to hashmap.
void remove(T &elem)
Remove element from hashmap.
Pointer front() const
Get first element in hashmap. Elements are ordered by insertion.
Pointer nextof(T &elem) const
Get hashmap element next to given one. Elements are ordered by insertion.
ROC_ATTR_NODISCARD bool grow()
Grow hashtable capacity.
size_t size() const
Get number of elements added to hashmap.
bool is_empty() const
Check if size is zero.
OwnershipPolicy< T >::Pointer Pointer
Pointer type.
Pointer find(const Key &key) const
Find element in the hashmap by key.
size_t capacity() const
Get maximum number of elements that can be added to hashmap before grow() should be called.
Pointer prevof(T &elem) const
Get hashmap element previous to given one. Elements are ordered by insertion.
Pointer back() const
Get last element in hashmap. Elements are ordered by insertion.
~Hashmap()
Release ownership of all elements.
ROC_ATTR_NODISCARD bool insert(T &elem)
Insert element into hashmap.
Hashmap(IArena &arena)
Initialize empty hashmap with arena.
Base class for non-copyable objects.
Intrusive hash table implementation file.
size_t hashsum_t
Hash type.
Commonly used types and functions.
Hashmap node internal data.