"Fossies" - the Fresh Open Source Software Archive

Member "jansson-2.14/src/hashtable.h" (19 Nov 2020, 5089 Bytes) of package /linux/www/jansson-2.14.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hashtable.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.13.1_vs_2.14.

    1 /*
    2  * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
    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 "jansson.h"
   12 #include <stdlib.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     size_t key_len;
   28     char key[1];
   29 };
   30 
   31 struct hashtable_bucket {
   32     struct hashtable_list *first;
   33     struct hashtable_list *last;
   34 };
   35 
   36 typedef struct hashtable {
   37     size_t size;
   38     struct hashtable_bucket *buckets;
   39     size_t order; /* hashtable has pow(2, order) buckets */
   40     struct hashtable_list list;
   41     struct hashtable_list ordered_list;
   42 } hashtable_t;
   43 
   44 #define hashtable_key_to_iter(key_)                                                      \
   45     (&(container_of(key_, struct hashtable_pair, key)->ordered_list))
   46 
   47 /**
   48  * hashtable_init - Initialize a hashtable object
   49  *
   50  * @hashtable: The (statically allocated) hashtable object
   51  *
   52  * Initializes a statically allocated hashtable object. The object
   53  * should be cleared with hashtable_close when it's no longer used.
   54  *
   55  * Returns 0 on success, -1 on error (out of memory).
   56  */
   57 int hashtable_init(hashtable_t *hashtable) JANSSON_ATTRS((warn_unused_result));
   58 
   59 /**
   60  * hashtable_close - Release all resources used by a hashtable object
   61  *
   62  * @hashtable: The hashtable
   63  *
   64  * Destroys a statically allocated hashtable object.
   65  */
   66 void hashtable_close(hashtable_t *hashtable);
   67 
   68 /**
   69  * hashtable_set - Add/modify value in hashtable
   70  *
   71  * @hashtable: The hashtable object
   72  * @key: The key
   73  * @key: The length of 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, size_t key_len, 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  * @key: The length of key
   92  *
   93  * Returns value if it is found, or NULL otherwise.
   94  */
   95 void *hashtable_get(hashtable_t *hashtable, const char *key, size_t key_len);
   96 
   97 /**
   98  * hashtable_del - Remove a value from the hashtable
   99  *
  100  * @hashtable: The hashtable object
  101  * @key: The key
  102  * @key: The length of key
  103  *
  104  * Returns 0 on success, or -1 if the key was not found.
  105  */
  106 int hashtable_del(hashtable_t *hashtable, const char *key, size_t key_len);
  107 
  108 /**
  109  * hashtable_clear - Clear hashtable
  110  *
  111  * @hashtable: The hashtable object
  112  *
  113  * Removes all items from the hashtable.
  114  */
  115 void hashtable_clear(hashtable_t *hashtable);
  116 
  117 /**
  118  * hashtable_iter - Iterate over hashtable
  119  *
  120  * @hashtable: The hashtable object
  121  *
  122  * Returns an opaque iterator to the first element in the hashtable.
  123  * The iterator should be passed to hashtable_iter_* functions.
  124  * The hashtable items are not iterated over in any particular order.
  125  *
  126  * There's no need to free the iterator in any way. The iterator is
  127  * valid as long as the item that is referenced by the iterator is not
  128  * deleted. Other values may be added or deleted. In particular,
  129  * hashtable_iter_next() may be called on an iterator, and after that
  130  * the key/value pair pointed by the old iterator may be deleted.
  131  */
  132 void *hashtable_iter(hashtable_t *hashtable);
  133 
  134 /**
  135  * hashtable_iter_at - Return an iterator at a specific key
  136  *
  137  * @hashtable: The hashtable object
  138  * @key: The key that the iterator should point to
  139  * @key: The length of key
  140  *
  141  * Like hashtable_iter() but returns an iterator pointing to a
  142  * specific key.
  143  */
  144 void *hashtable_iter_at(hashtable_t *hashtable, const char *key, size_t key_len);
  145 
  146 /**
  147  * hashtable_iter_next - Advance an iterator
  148  *
  149  * @hashtable: The hashtable object
  150  * @iter: The iterator
  151  *
  152  * Returns a new iterator pointing to the next element in the
  153  * hashtable or NULL if the whole hastable has been iterated over.
  154  */
  155 void *hashtable_iter_next(hashtable_t *hashtable, void *iter);
  156 
  157 /**
  158  * hashtable_iter_key - Retrieve the key pointed by an iterator
  159  *
  160  * @iter: The iterator
  161  */
  162 void *hashtable_iter_key(void *iter);
  163 
  164 /**
  165  * hashtable_iter_key_len - Retrieve the key length pointed by an iterator
  166  *
  167  * @iter: The iterator
  168  */
  169 size_t hashtable_iter_key_len(void *iter);
  170 
  171 /**
  172  * hashtable_iter_value - Retrieve the value pointed by an iterator
  173  *
  174  * @iter: The iterator
  175  */
  176 void *hashtable_iter_value(void *iter);
  177 
  178 /**
  179  * hashtable_iter_set - Set the value pointed by an iterator
  180  *
  181  * @iter: The iterator
  182  * @value: The value to set
  183  */
  184 void hashtable_iter_set(void *iter, json_t *value);
  185 
  186 #endif