@@ -8,11 +8,19 @@ Data Structures
88
99`struct hashmap`::
1010
11- The hash table structure.
11+ The hash table structure. Members can be used as follows, but should
12+ not be modified directly:
1213+
13- The `size` member keeps track of the total number of entries. The `cmpfn`
14- member is a function used to compare two entries for equality. The `table` and
15- `tablesize` members store the hash table and its size, respectively.
14+ The `size` member keeps track of the total number of entries (0 means the
15+ hashmap is empty).
16+ +
17+ `tablesize` is the allocated size of the hash table. A non-0 value indicates
18+ that the hashmap is initialized. It may also be useful for statistical purposes
19+ (i.e. `size / tablesize` is the current load factor).
20+ +
21+ `cmpfn` stores the comparison function specified in `hashmap_init()`. In
22+ advanced scenarios, it may be useful to change this, e.g. to switch between
23+ case-sensitive and case-insensitive lookup.
1624
1725`struct hashmap_entry`::
1826
@@ -58,6 +66,15 @@ Functions
5866+
5967`strihash` and `memihash` are case insensitive versions.
6068
69+ `unsigned int sha1hash(const unsigned char *sha1)`::
70+
71+ Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
72+ for use in hash tables. Cryptographic hashes are supposed to have
73+ uniform distribution, so in contrast to `memhash()`, this just copies
74+ the first `sizeof(int)` bytes without shuffling any bits. Note that
75+ the results will be different on big-endian and little-endian
76+ platforms, so they should not be stored or transferred over the net.
77+
6178`void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function, size_t initial_size)`::
6279
6380 Initializes a hashmap structure.
@@ -101,6 +118,20 @@ hashmap_entry) that has at least been initialized with the proper hash code
101118If an entry with matching hash code is found, `key` and `keydata` are passed
102119to `hashmap_cmp_fn` to decide whether the entry matches the key.
103120
121+ `void *hashmap_get_from_hash(const struct hashmap *map, unsigned int hash, const void *keydata)`::
122+
123+ Returns the hashmap entry for the specified hash code and key data,
124+ or NULL if not found.
125+ +
126+ `map` is the hashmap structure.
127+ +
128+ `hash` is the hash code of the entry to look up.
129+ +
130+ If an entry with matching hash code is found, `keydata` is passed to
131+ `hashmap_cmp_fn` to decide whether the entry matches the key. The
132+ `entry_or_key` parameter points to a bogus hashmap_entry structure that
133+ should not be used in the comparison.
134+
104135`void *hashmap_get_next(const struct hashmap *map, const void *entry)`::
105136
106137 Returns the next equal hashmap entry, or NULL if not found. This can be
@@ -162,6 +193,21 @@ more entries.
162193`hashmap_iter_first` is a combination of both (i.e. initializes the iterator
163194and returns the first entry, if any).
164195
196+ `const char *strintern(const char *string)`::
197+ `const void *memintern(const void *data, size_t len)`::
198+
199+ Returns the unique, interned version of the specified string or data,
200+ similar to the `String.intern` API in Java and .NET, respectively.
201+ Interned strings remain valid for the entire lifetime of the process.
202+ +
203+ Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned
204+ strings / data must not be modified or freed.
205+ +
206+ Interned strings are best used for short strings with high probability of
207+ duplicates.
208+ +
209+ Uses a hashmap to store the pool of interned strings.
210+
165211Usage example
166212-------------
167213
0 commit comments