libhatchet
Loading...
Searching...
No Matches
hxhash_table_nodes.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
9
10#include "libhatchet.h"
11
12// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
13#if HX_USE_MACROS_WITH_MODULE
14#error Header does not provide macros alone.
15#endif
16
17#include "hxhash_table.hpp"
18
19HX_NS_BEGIN_
20
26template<typename key_t_>
28public:
29 using key_t = key_t_;
30
33 hxhash_table_node_integer(const key_t_& key_) :
34 m_hash_next_(hxnull), m_key_(key_), m_hash_(hxkey_hash(key_)) { }
35
38 m_hash_next_(hxnull), m_key_(src_.m_key_), m_hash_(src_.m_hash_) { }
39
42 hxassertmsg(this != &other_, "self_assignment"); (void)other_;
43 return *this;
44 }
45
47 hxhash_table_node_integer* hash_next(void) const { return m_hash_next_; }
50 void set_hash_next(hxhash_table_node_integer* next_) { m_hash_next_ = next_; }
51
53 const key_t_& hash_key(void) const { return m_key_; }
55 hxhash_t hash_value(void) const { return m_hash_; }
58 static hxhash_t hash_value(key_t_ key_) { return hxkey_hash(key_); }
59
60private:
61 hxhash_table_node_integer(void) = delete;
62
63 hxhash_table_node_integer* m_hash_next_;
64 key_t_ m_key_;
65 hxhash_t m_hash_;
66};
67
86
91template <hxsystem_allocator_t allocator_=hxsystem_allocator_heap>
93public:
98 : hxhash_table_set_node<const char*>(hxstring_duplicate(k_, allocator_)) { }
99
101 ~hxhash_table_node_string(void) { hxfree(const_cast<char *>(this->hash_key())); }
102
108
109private:
110 // Copying is disallowed as it would involve a string copy.
111 hxhash_table_node_string(void) = delete;
113 void operator=(const hxhash_table_node_string&) = delete;
114};
115
116HX_NS_END_
hxhash_table_node_integer - node_t for use with hxhash_table for integer types.
Definition hxhash_table_nodes.hpp:27
hxhash_table_node_integer & operator=(const hxhash_table_node_integer &other_)
Assigns nothing. Hash table linkage of either node is not affected.
Definition hxhash_table_nodes.hpp:41
hxhash_t hash_value(void) const
Returns the cached hash value for the stored key.
Definition hxhash_table_nodes.hpp:55
static hxhash_t hash_value(key_t_ key_)
Returns the hash of a key without constructing a node.
Definition hxhash_table_nodes.hpp:58
hxhash_table_node_integer(const key_t_ &key_)
Constructs a node wrapping the key and caches its hash.
Definition hxhash_table_nodes.hpp:33
const key_t_ & hash_key(void) const
The key and hash identify the node_t and should not change once added.
Definition hxhash_table_nodes.hpp:53
key_t_ key_t
Definition hxhash_table_nodes.hpp:29
hxhash_table_node_integer(const hxhash_table_node_integer &src_)
Constructs an unlinked node with the same key.
Definition hxhash_table_nodes.hpp:37
void set_hash_next(hxhash_table_node_integer *next_)
Sets the next node in the table's embedded linked list.
Definition hxhash_table_nodes.hpp:50
hxhash_table_node_integer * hash_next(void) const
Returns the next node in the table's embedded linked list.
Definition hxhash_table_nodes.hpp:47
hxhash_table_node_string_literal * hash_next(void) const
Returns the next node in the table's embedded linked list.
Definition hxhash_table_nodes.hpp:81
hxhash_table_node_string_literal(const char *k_)
Constructor initializes the node with a string key and computes its hash.
Definition hxhash_table_nodes.hpp:77
hxhash_table_node_string - Subclass of hxhash_table_set_node for C strings.
Definition hxhash_table_nodes.hpp:92
hxhash_table_node_string * hash_next(void) const
Returns the next node in the table's embedded linked list.
Definition hxhash_table_nodes.hpp:104
hxhash_table_node_string(const char *k_)
Constructor allocates and duplicates the string key, then initializes the node.
Definition hxhash_table_nodes.hpp:97
~hxhash_table_node_string(void)
Destructor frees the allocated string key.
Definition hxhash_table_nodes.hpp:101
const const char *& hash_key(void) const
Definition hxhash_table.hpp:70
hxhash_table_set_node(ref_t_ &&key_)
Definition hxhash_table.hpp:55
hxhash_table_set_node * hash_next(void) const
Returns the next node in the table's embedded linked list.
Definition hxhash_table.hpp:63
A fixed-size hash table with embedded singly-linked list buckets.
hxhash_t hxkey_hash(T x_)
hxkey_hash(T) - Returns the xxhash32 of a numeric value cast to 32 bits.
Definition hxkey.hpp:165
void hxfree(void *ptr_) hxattr_noexcept hxattr_hot
hxfree - Frees memory previously allocated with hxmalloc or hxmalloc_ext.
char * hxstring_duplicate(const char *string_, hxsystem_allocator_t allocator_) hxattr_noexcept hxattr_allocator(hxfree) hxattr_nonnull(1) hxattr_hot
hxstring_duplicate - Allocates a copy of a string using the specified memory manager.
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:97
Provides core macros, memory management and feature detection.
#define hxassertmsg(x_,...)
hxassertmsg(bool x, ...) - Logs an error and terminates execution if x is false.
Definition libhatchet.h:101
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric constant 0.
Definition libhatchet.h:277
uint32_t hxhash_t
hxhash_t - Unsigned 32-bit hash value. Expect collisions.
Definition libhatchet.h:212