dict

Dictionary for sd_t's
git clone git://www.tkruger.se/dict.git
Log | Files | Refs | README

dict.h (1651B)


      1 #ifndef DICT_H
      2 #define DICT_H
      3 
      4 #include "stack_sd.h"
      5 
      6 #define DICT_DEFAULT_SIZE 255
      7 #define DICT_SMALLEST_STACK_SIZE 2
      8 #define DICT_LOOKUP_NOT_FOUND (STACK_SD_LOOKUP_NOT_FOUND) // == -1
      9 #define DICT_FACTOR 2
     10 
     11 typedef struct {
     12   size_t nelts;
     13   size_t nalloc;
     14 
     15   size_t* nvalues;
     16   size_t* value_alloc;
     17   sd_t **keys;
     18   void ***values;
     19 } dict_t;
     20 
     21 /**
     22  * Initialises a dictionary with a given size (allocated). The
     23  * initialized dict_t will be "empty". This allocates memory that will
     24  * need to be cleared (use dict_clear).
     25  *
     26  * @param d pointer to the dict_t to initialise
     27  * @param alloc the size to allocate to it
     28  */
     29 void dict_init_size(dict_t *d, const size_t alloc);
     30 
     31 /**
     32  * Initialises a dictionary with the standard allocation size (which is
     33  * DICT_DEFAULT_SIZE == 255).
     34  *
     35  * @param d pointer to the dict_t to initialise
     36  */
     37 void dict_init(dict_t *d);
     38 
     39 /**
     40  * Clears an initialised dictionary.
     41  *
     42  * @param d pointer to the dict_t to clear
     43  */
     44 void dict_clear(dict_t *d);
     45 
     46 /**
     47  * Lookup an element in the dictionary, by its key. Returns the void
     48  * pointer which is stored as its value, if the dictionary contains the
     49  * key. Otherwise, NULL is returned.
     50  *
     51  * @param d pointer to the dict_t to lookup in
     52  * @param k the key to lookup with
     53  */
     54 void *dict_lookup(dict_t *d, sd_t k);
     55 
     56 /**
     57  * Insert an element (a key <-> value pair) into the dictionary. If the
     58  * key is already in the dictionary the value is replaced with the
     59  * provided value.
     60  *
     61  * @param d pointer to the dict_t to insert into
     62  * @param k the key of the element to insert
     63  * @param v the (void*) value to insert
     64  */
     65 void dict_insert(dict_t *t, sd_t k, void *v);
     66 
     67 #endif