aocc23

Advent of Code 2023
git clone git://www.tkruger.se/aocc23.git
Log | Files | Refs | README

ht.h (1772B)


      1 #ifndef HT_H
      2 #define HT_H
      3 
      4 #include "stack_sd.h"
      5 
      6 #define HT_DEFAULT_SIZE 255
      7 #define HT_DEFAULT_STACK_SIZE 2
      8 #define HT_LOOKUP_NOT_FOUND (STACK_SD_LOOKUP_NOT_FOUND) // == -1
      9 
     10 typedef struct {
     11   size_t nelts;
     12   size_t nalloc;
     13 
     14   stack_sd *entries;
     15 } ht;
     16 
     17 /**
     18  * Intitialize a hash table.
     19  *
     20  * This initializes a hash table of <<alloc>> buckets, at the address
     21  * pointed to by <<t>>. Note that this memory needs to be cleared.
     22  *
     23  * @param t pointer to the ht to be initialized
     24  * @param alloc the number of buckets to allocate for
     25  */
     26 void ht_init_size(ht *t, const size_t alloc);
     27 
     28 /**
     29  * Initialize a hash table.
     30  *
     31  * This initializes a hash table of the default size which is
     32  * HT_DEFAULT_SIZE number of buckets. Note that this allocates memory
     33  * that needs to be ht_clear'd.
     34  *
     35  * @param t pointer to the th to initialized
     36  */
     37 void ht_init(ht *t);
     38 
     39 /**
     40  * Clear a stack.
     41  *
     42  * @param t the stack to clear
     43  */
     44 void ht_clear(ht *t);
     45 
     46 /**
     47  * Lookup a sd in the hashtable.
     48  *
     49  * Returns a nonnegative value if the sd is in the hash table (by value
     50  * comparison rather than pointer comparison of data). The value is the
     51  * index of the stack_sd in the hash bucket where the sd is found. If
     52  * the sd cannot be found in the stack the value
     53  *   HT_LOOKUP_NOT_FOUND == -1
     54  * is returned.
     55  *
     56  * @param t pointer to the hash table too look up in
     57  * @param d the sd to look up
     58  */
     59 int ht_lookup(ht *t, sd d);
     60 
     61 /**
     62  * Insert a as into the hashtable.
     63  *
     64  * Inserts a sd into the hash table. Note that this does not copy the
     65  * sd data, but only pushes the pointer onto the stack. The sd should
     66  * therefore not be cleared until ht has been cleared.
     67  *
     68  * @param t pointer to the hash table to insert into
     69  * @param d the sd to insert
     70  */
     71 void ht_insert(ht *t, sd d);
     72 
     73 #endif