|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2016 Petri Lehtinen <[email protected]> |
| 3 | + * |
| 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. |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef HASHTABLE_H |
| 9 | +#define HASHTABLE_H |
| 10 | + |
| 11 | +#include <stdlib.h> |
| 12 | +#include "jansson.h" |
| 13 | + |
| 14 | +struct hashtable_list { |
| 15 | + struct hashtable_list *prev; |
| 16 | + struct hashtable_list *next; |
| 17 | +}; |
| 18 | + |
| 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, |
| 21 | + too */ |
| 22 | +struct hashtable_pair { |
| 23 | + struct hashtable_list list; |
| 24 | + struct hashtable_list ordered_list; |
| 25 | + size_t hash; |
| 26 | + json_t *value; |
| 27 | + char key[1]; |
| 28 | +}; |
| 29 | + |
| 30 | +struct hashtable_bucket { |
| 31 | + struct hashtable_list *first; |
| 32 | + struct hashtable_list *last; |
| 33 | +}; |
| 34 | + |
| 35 | +typedef struct hashtable { |
| 36 | + size_t size; |
| 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; |
| 41 | +} hashtable_t; |
| 42 | + |
| 43 | + |
| 44 | +#define hashtable_key_to_iter(key_) \ |
| 45 | + (&(container_of(key_, struct hashtable_pair, key)->ordered_list)) |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * hashtable_init - Initialize a hashtable object |
| 50 | + * |
| 51 | + * @hashtable: The (statically allocated) hashtable object |
| 52 | + * |
| 53 | + * Initializes a statically allocated hashtable object. The object |
| 54 | + * should be cleared with hashtable_close when it's no longer used. |
| 55 | + * |
| 56 | + * Returns 0 on success, -1 on error (out of memory). |
| 57 | + */ |
| 58 | +int hashtable_init(hashtable_t *hashtable); |
| 59 | + |
| 60 | +/** |
| 61 | + * hashtable_close - Release all resources used by a hashtable object |
| 62 | + * |
| 63 | + * @hashtable: The hashtable |
| 64 | + * |
| 65 | + * Destroys a statically allocated hashtable object. |
| 66 | + */ |
| 67 | +void hashtable_close(hashtable_t *hashtable); |
| 68 | + |
| 69 | +/** |
| 70 | + * hashtable_set - Add/modify value in hashtable |
| 71 | + * |
| 72 | + * @hashtable: The hashtable object |
| 73 | + * @key: The key |
| 74 | + * @serial: For addition order of keys |
| 75 | + * @value: The value |
| 76 | + * |
| 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. |
| 81 | + * |
| 82 | + * Returns 0 on success, -1 on failure (out of memory). |
| 83 | + */ |
| 84 | +int hashtable_set(hashtable_t *hashtable, const char *key, json_t *value); |
| 85 | + |
| 86 | +/** |
| 87 | + * hashtable_get - Get a value associated with a key |
| 88 | + * |
| 89 | + * @hashtable: The hashtable object |
| 90 | + * @key: The key |
| 91 | + * |
| 92 | + * Returns value if it is found, or NULL otherwise. |
| 93 | + */ |
| 94 | +void *hashtable_get(hashtable_t *hashtable, const char *key); |
| 95 | + |
| 96 | +/** |
| 97 | + * hashtable_del - Remove a value from the hashtable |
| 98 | + * |
| 99 | + * @hashtable: The hashtable object |
| 100 | + * @key: The key |
| 101 | + * |
| 102 | + * Returns 0 on success, or -1 if the key was not found. |
| 103 | + */ |
| 104 | +int hashtable_del(hashtable_t *hashtable, const char *key); |
| 105 | + |
| 106 | +/** |
| 107 | + * hashtable_clear - Clear hashtable |
| 108 | + * |
| 109 | + * @hashtable: The hashtable object |
| 110 | + * |
| 111 | + * Removes all items from the hashtable. |
| 112 | + */ |
| 113 | +void hashtable_clear(hashtable_t *hashtable); |
| 114 | + |
| 115 | +/** |
| 116 | + * hashtable_iter - Iterate over hashtable |
| 117 | + * |
| 118 | + * @hashtable: The hashtable object |
| 119 | + * |
| 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. |
| 123 | + * |
| 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. |
| 129 | + */ |
| 130 | +void *hashtable_iter(hashtable_t *hashtable); |
| 131 | + |
| 132 | +/** |
| 133 | + * hashtable_iter_at - Return an iterator at a specific key |
| 134 | + * |
| 135 | + * @hashtable: The hashtable object |
| 136 | + * @key: The key that the iterator should point to |
| 137 | + * |
| 138 | + * Like hashtable_iter() but returns an iterator pointing to a |
| 139 | + * specific key. |
| 140 | + */ |
| 141 | +void *hashtable_iter_at(hashtable_t *hashtable, const char *key); |
| 142 | + |
| 143 | +/** |
| 144 | + * hashtable_iter_next - Advance an iterator |
| 145 | + * |
| 146 | + * @hashtable: The hashtable object |
| 147 | + * @iter: The iterator |
| 148 | + * |
| 149 | + * Returns a new iterator pointing to the next element in the |
| 150 | + * hashtable or NULL if the whole hastable has been iterated over. |
| 151 | + */ |
| 152 | +void *hashtable_iter_next(hashtable_t *hashtable, void *iter); |
| 153 | + |
| 154 | +/** |
| 155 | + * hashtable_iter_key - Retrieve the key pointed by an iterator |
| 156 | + * |
| 157 | + * @iter: The iterator |
| 158 | + */ |
| 159 | +void *hashtable_iter_key(void *iter); |
| 160 | + |
| 161 | +/** |
| 162 | + * hashtable_iter_value - Retrieve the value pointed by an iterator |
| 163 | + * |
| 164 | + * @iter: The iterator |
| 165 | + */ |
| 166 | +void *hashtable_iter_value(void *iter); |
| 167 | + |
| 168 | +/** |
| 169 | + * hashtable_iter_set - Set the value pointed by an iterator |
| 170 | + * |
| 171 | + * @iter: The iterator |
| 172 | + * @value: The value to set |
| 173 | + */ |
| 174 | +void hashtable_iter_set(void *iter, json_t *value); |
| 175 | + |
| 176 | +#endif |
0 commit comments