/*1* Copyright (c) 2009-2013 Petri Lehtinen <[email protected]>2*3* This library is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef HASHTABLE_H8#define HASHTABLE_H910struct hashtable_list {11struct hashtable_list *prev;12struct hashtable_list *next;13};1415/* "pair" may be a bit confusing a name, but think of it as a16key-value pair. In this case, it just encodes some extra data,17too */18struct hashtable_pair {19size_t hash;20struct hashtable_list list;21json_t *value;22size_t serial;23char key[1];24};2526struct hashtable_bucket {27struct hashtable_list *first;28struct hashtable_list *last;29};3031typedef struct hashtable {32size_t size;33struct hashtable_bucket *buckets;34size_t num_buckets; /* index to primes[] */35struct hashtable_list list;36} hashtable_t;373839#define hashtable_key_to_iter(key_) \40(&(container_of(key_, struct hashtable_pair, key)->list))4142/**43* hashtable_init - Initialize a hashtable object44*45* @hashtable: The (statically allocated) hashtable object46*47* Initializes a statically allocated hashtable object. The object48* should be cleared with hashtable_close when it's no longer used.49*50* Returns 0 on success, -1 on error (out of memory).51*/52int hashtable_init(hashtable_t *hashtable);5354/**55* hashtable_close - Release all resources used by a hashtable object56*57* @hashtable: The hashtable58*59* Destroys a statically allocated hashtable object.60*/61void hashtable_close(hashtable_t *hashtable);6263/**64* hashtable_set - Add/modify value in hashtable65*66* @hashtable: The hashtable object67* @key: The key68* @serial: For addition order of keys69* @value: The value70*71* If a value with the given key already exists, its value is replaced72* with the new value. Value is "stealed" in the sense that hashtable73* doesn't increment its refcount but decreases the refcount when the74* value is no longer needed.75*76* Returns 0 on success, -1 on failure (out of memory).77*/78int hashtable_set(hashtable_t *hashtable,79const char *key, size_t serial,80json_t *value);8182/**83* hashtable_get - Get a value associated with a key84*85* @hashtable: The hashtable object86* @key: The key87*88* Returns value if it is found, or NULL otherwise.89*/90void *hashtable_get(hashtable_t *hashtable, const char *key);9192/**93* hashtable_del - Remove a value from the hashtable94*95* @hashtable: The hashtable object96* @key: The key97*98* Returns 0 on success, or -1 if the key was not found.99*/100int hashtable_del(hashtable_t *hashtable, const char *key);101102/**103* hashtable_clear - Clear hashtable104*105* @hashtable: The hashtable object106*107* Removes all items from the hashtable.108*/109void hashtable_clear(hashtable_t *hashtable);110111/**112* hashtable_iter - Iterate over hashtable113*114* @hashtable: The hashtable object115*116* Returns an opaque iterator to the first element in the hashtable.117* The iterator should be passed to hashtable_iter_* functions.118* The hashtable items are not iterated over in any particular order.119*120* There's no need to free the iterator in any way. The iterator is121* valid as long as the item that is referenced by the iterator is not122* deleted. Other values may be added or deleted. In particular,123* hashtable_iter_next() may be called on an iterator, and after that124* the key/value pair pointed by the old iterator may be deleted.125*/126void *hashtable_iter(hashtable_t *hashtable);127128/**129* hashtable_iter_at - Return an iterator at a specific key130*131* @hashtable: The hashtable object132* @key: The key that the iterator should point to133*134* Like hashtable_iter() but returns an iterator pointing to a135* specific key.136*/137void *hashtable_iter_at(hashtable_t *hashtable, const char *key);138139/**140* hashtable_iter_next - Advance an iterator141*142* @hashtable: The hashtable object143* @iter: The iterator144*145* Returns a new iterator pointing to the next element in the146* hashtable or NULL if the whole hastable has been iterated over.147*/148void *hashtable_iter_next(hashtable_t *hashtable, void *iter);149150/**151* hashtable_iter_key - Retrieve the key pointed by an iterator152*153* @iter: The iterator154*/155void *hashtable_iter_key(void *iter);156157/**158* hashtable_iter_serial - Retrieve the serial number pointed to by an iterator159*160* @iter: The iterator161*/162size_t hashtable_iter_serial(void *iter);163164/**165* hashtable_iter_value - Retrieve the value pointed by an iterator166*167* @iter: The iterator168*/169void *hashtable_iter_value(void *iter);170171/**172* hashtable_iter_set - Set the value pointed by an iterator173*174* @iter: The iterator175* @value: The value to set176*/177void hashtable_iter_set(void *iter, json_t *value);178179#endif180181182