Lines Matching +full:key +full:- +full:value

1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * Generic non-thread safe hash map implementation.
23 return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits); in hash_bits()
25 return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits); in hash_bits()
31 /* generic C-string hashing function */
43 typedef size_t (*hashmap_hash_fn)(long key, void *ctx);
48 * long-sized integers or pointers, this is achieved as follows:
49 * - interface functions that operate on keys and values are hidden
50 * behind auxiliary macros, e.g. hashmap_insert <-> hashmap__insert;
51 * - these auxiliary macros cast the key and value parameters as
53 * - for pointer parameters (e.g. old_key) the size of the pointed
55 * - when iterating using hashmap__for_each_* forms
56 * hasmap_entry->key should be used for integer keys and
57 * hasmap_entry->pkey should be used for pointer keys,
62 long key; member
66 long value; member
96 * - HASHMAP_ADD - only add key/value if key doesn't exist yet;
97 * - HASHMAP_SET - add key/value pair if key doesn't exist yet; otherwise,
98 * update value;
99 * - HASHMAP_UPDATE - update value, if key already exists; otherwise, do
100 * nothing and return -ENOENT;
101 * - HASHMAP_APPEND - always add key/value pair, even if key already exists.
103 * associated with the same key. Most useful read API for such hashmap is
105 * used, it will return last inserted key/value entry (first in a bucket
118 #p " pointee should be a long-sized integer or a pointer"); \
123 * hashmap__insert() adds key/value entry w/ various semantics, depending on
124 * provided strategy value. If a given key/value pair replaced already
125 * existing key/value pair, both old key and old value will be returned
129 int hashmap_insert(struct hashmap *map, long key, long value,
133 #define hashmap__insert(map, key, value, strategy, old_key, old_value) \ argument
134 hashmap_insert((map), (long)(key), (long)(value), (strategy), \
138 #define hashmap__add(map, key, value) \ argument
139 hashmap__insert((map), (key), (value), HASHMAP_ADD, NULL, NULL)
141 #define hashmap__set(map, key, value, old_key, old_value) \ argument
142 hashmap__insert((map), (key), (value), HASHMAP_SET, (old_key), (old_value))
144 #define hashmap__update(map, key, value, old_key, old_value) \ argument
145 hashmap__insert((map), (key), (value), HASHMAP_UPDATE, (old_key), (old_value))
147 #define hashmap__append(map, key, value) \ argument
148 hashmap__insert((map), (key), (value), HASHMAP_APPEND, NULL, NULL)
150 bool hashmap_delete(struct hashmap *map, long key, long *old_key, long *old_value);
152 #define hashmap__delete(map, key, old_key, old_value) \ argument
153 hashmap_delete((map), (long)(key), \
157 bool hashmap_find(const struct hashmap *map, long key, long *value);
159 #define hashmap__find(map, key, value) \ argument
160 hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
163 * hashmap__for_each_entry - iterate over all entries in hashmap
169 for (bkt = 0; bkt < map->cap; bkt++) \
170 for (cur = map->buckets[bkt]; cur; cur = cur->next)
173 * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
181 for (bkt = 0; bkt < map->cap; bkt++) \
182 for (cur = map->buckets[bkt]; \
183 cur && ({tmp = cur->next; true; }); \
187 * hashmap__for_each_key_entry - iterate over entries associated with given key
190 * @key: key to iterate entries for
193 for (cur = map->buckets \
194 ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
197 cur = cur->next) \
198 if (map->equal_fn(cur->key, (_key), map->ctx))
201 for (cur = map->buckets \
202 ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
204 cur && ({ tmp = cur->next; true; }); \
206 if (map->equal_fn(cur->key, (_key), map->ctx))