2 * Copyright (c) 2009-2016 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.
14 struct hashtable_list
{
15 struct hashtable_list
*prev
;
16 struct hashtable_list
*next
;
19 /* "pair" may be a bit confusing a name, but think of it as a
20 key-value pair. In this case, it just encodes some extra data,
22 struct hashtable_pair
{
23 struct hashtable_list list
;
24 struct hashtable_list ordered_list
;
30 struct hashtable_bucket
{
31 struct hashtable_list
*first
;
32 struct hashtable_list
*last
;
35 typedef struct hashtable
{
37 struct hashtable_bucket
*buckets
;
38 size_t order
; /* hashtable has pow(2, order) buckets */
39 struct hashtable_list list
;
40 struct hashtable_list ordered_list
;
44 #define hashtable_key_to_iter(key_) \
45 (&(container_of(key_, struct hashtable_pair, key)->ordered_list))
49 * hashtable_init - Initialize a hashtable object
51 * @hashtable: The (statically allocated) hashtable object
53 * Initializes a statically allocated hashtable object. The object
54 * should be cleared with hashtable_close when it's no longer used.
56 * Returns 0 on success, -1 on error (out of memory).
58 int hashtable_init(hashtable_t
*hashtable
) JANSSON_ATTRS(warn_unused_result
);
61 * hashtable_close - Release all resources used by a hashtable object
63 * @hashtable: The hashtable
65 * Destroys a statically allocated hashtable object.
67 void hashtable_close(hashtable_t
*hashtable
);
70 * hashtable_set - Add/modify value in hashtable
72 * @hashtable: The hashtable object
74 * @serial: For addition order of keys
77 * If a value with the given key already exists, its value is replaced
78 * with the new value. Value is "stealed" in the sense that hashtable
79 * doesn't increment its refcount but decreases the refcount when the
80 * value is no longer needed.
82 * Returns 0 on success, -1 on failure (out of memory).
84 int hashtable_set(hashtable_t
*hashtable
, const char *key
, json_t
*value
);
87 * hashtable_get - Get a value associated with a key
89 * @hashtable: The hashtable object
92 * Returns value if it is found, or NULL otherwise.
94 void *hashtable_get(hashtable_t
*hashtable
, const char *key
);
97 * hashtable_del - Remove a value from the hashtable
99 * @hashtable: The hashtable object
102 * Returns 0 on success, or -1 if the key was not found.
104 int hashtable_del(hashtable_t
*hashtable
, const char *key
);
107 * hashtable_clear - Clear hashtable
109 * @hashtable: The hashtable object
111 * Removes all items from the hashtable.
113 void hashtable_clear(hashtable_t
*hashtable
);
116 * hashtable_iter - Iterate over hashtable
118 * @hashtable: The hashtable object
120 * Returns an opaque iterator to the first element in the hashtable.
121 * The iterator should be passed to hashtable_iter_* functions.
122 * The hashtable items are not iterated over in any particular order.
124 * There's no need to free the iterator in any way. The iterator is
125 * valid as long as the item that is referenced by the iterator is not
126 * deleted. Other values may be added or deleted. In particular,
127 * hashtable_iter_next() may be called on an iterator, and after that
128 * the key/value pair pointed by the old iterator may be deleted.
130 void *hashtable_iter(hashtable_t
*hashtable
);
133 * hashtable_iter_at - Return an iterator at a specific key
135 * @hashtable: The hashtable object
136 * @key: The key that the iterator should point to
138 * Like hashtable_iter() but returns an iterator pointing to a
141 void *hashtable_iter_at(hashtable_t
*hashtable
, const char *key
);
144 * hashtable_iter_next - Advance an iterator
146 * @hashtable: The hashtable object
147 * @iter: The iterator
149 * Returns a new iterator pointing to the next element in the
150 * hashtable or NULL if the whole hastable has been iterated over.
152 void *hashtable_iter_next(hashtable_t
*hashtable
, void *iter
);
155 * hashtable_iter_key - Retrieve the key pointed by an iterator
157 * @iter: The iterator
159 void *hashtable_iter_key(void *iter
);
162 * hashtable_iter_value - Retrieve the value pointed by an iterator
164 * @iter: The iterator
166 void *hashtable_iter_value(void *iter
);
169 * hashtable_iter_set - Set the value pointed by an iterator
171 * @iter: The iterator
172 * @value: The value to set
174 void hashtable_iter_set(void *iter
, json_t
*value
);