2 * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
11 struct hashtable_list {
12 struct hashtable_list *prev;
13 struct hashtable_list *next;
16 /* "pair" may be a bit confusing a name, but think of it as a
17 key-value pair. In this case, it just encodes some extra data,
19 struct hashtable_pair {
21 struct hashtable_list list;
27 struct hashtable_bucket {
28 struct hashtable_list *first;
29 struct hashtable_list *last;
32 typedef struct hashtable {
34 struct hashtable_bucket *buckets;
35 size_t num_buckets; /* index to primes[] */
36 struct hashtable_list list;
40 #define hashtable_key_to_iter(key_) \
41 (&(container_of(key_, struct hashtable_pair, key)->list))
44 * hashtable_init - Initialize a hashtable object
46 * @hashtable: The (statically allocated) hashtable object
48 * Initializes a statically allocated hashtable object. The object
49 * should be cleared with hashtable_close when it's no longer used.
51 * Returns 0 on success, -1 on error (out of memory).
53 int hashtable_init(hashtable_t *hashtable);
56 * hashtable_close - Release all resources used by a hashtable object
58 * @hashtable: The hashtable
60 * Destroys a statically allocated hashtable object.
62 void hashtable_close(hashtable_t *hashtable);
65 * hashtable_set - Add/modify value in hashtable
67 * @hashtable: The hashtable object
69 * @serial: For addition order of keys
72 * If a value with the given key already exists, its value is replaced
73 * with the new value. Value is "stealed" in the sense that hashtable
74 * doesn't increment its refcount but decreases the refcount when the
75 * value is no longer needed.
77 * Returns 0 on success, -1 on failure (out of memory).
79 int hashtable_set(hashtable_t *hashtable,
80 const char *key, size_t serial,
84 * hashtable_get - Get a value associated with a key
86 * @hashtable: The hashtable object
89 * Returns value if it is found, or NULL otherwise.
91 void *hashtable_get(hashtable_t *hashtable, const char *key);
94 * hashtable_del - Remove a value from the hashtable
96 * @hashtable: The hashtable object
99 * Returns 0 on success, or -1 if the key was not found.
101 int hashtable_del(hashtable_t *hashtable, const char *key);
104 * hashtable_clear - Clear hashtable
106 * @hashtable: The hashtable object
108 * Removes all items from the hashtable.
110 void hashtable_clear(hashtable_t *hashtable);
113 * hashtable_iter - Iterate over hashtable
115 * @hashtable: The hashtable object
117 * Returns an opaque iterator to the first element in the hashtable.
118 * The iterator should be passed to hashtable_iter_* functions.
119 * The hashtable items are not iterated over in any particular order.
121 * There's no need to free the iterator in any way. The iterator is
122 * valid as long as the item that is referenced by the iterator is not
123 * deleted. Other values may be added or deleted. In particular,
124 * hashtable_iter_next() may be called on an iterator, and after that
125 * the key/value pair pointed by the old iterator may be deleted.
127 void *hashtable_iter(hashtable_t *hashtable);
130 * hashtable_iter_at - Return an iterator at a specific key
132 * @hashtable: The hashtable object
133 * @key: The key that the iterator should point to
135 * Like hashtable_iter() but returns an iterator pointing to a
138 void *hashtable_iter_at(hashtable_t *hashtable, const char *key);
141 * hashtable_iter_next - Advance an iterator
143 * @hashtable: The hashtable object
144 * @iter: The iterator
146 * Returns a new iterator pointing to the next element in the
147 * hashtable or NULL if the whole hastable has been iterated over.
149 void *hashtable_iter_next(hashtable_t *hashtable, void *iter);
152 * hashtable_iter_key - Retrieve the key pointed by an iterator
154 * @iter: The iterator
156 void *hashtable_iter_key(void *iter);
159 * hashtable_iter_serial - Retrieve the serial number pointed to by an iterator
161 * @iter: The iterator
163 size_t hashtable_iter_serial(void *iter);
166 * hashtable_iter_value - Retrieve the value pointed by an iterator
168 * @iter: The iterator
170 void *hashtable_iter_value(void *iter);
173 * hashtable_iter_set - Set the value pointed by an iterator
175 * @iter: The iterator
176 * @value: The value to set
178 void hashtable_iter_set(void *iter, json_t *value);