libamxc  1.10.3
C Generic Data Containers
amxc_htable.c File Reference

Ambiorix hash table API implementation. More...

#include <stdlib.h>
#include <string.h>
#include <amxc/amxc_hash.h>
#include <amxc/amxc_htable.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Functions

static void amxc_htable_insert_it (amxc_htable_t *const htable, amxc_htable_it_t *const it)
 
static int amxc_htable_grow (amxc_htable_t *const htable, size_t hint)
 
static void amxc_htable_it_delete_func (amxc_array_it_t *const it)
 
int amxc_htable_new (amxc_htable_t **htable, const size_t reserve)
 Allocates a hash table. More...
 
static int amxc_htable_cmp_keys (amxc_array_it_t *it1, amxc_array_it_t *it2)
 
void amxc_htable_delete (amxc_htable_t **htable, amxc_htable_it_delete_t func)
 Frees the previously allocated hash table. More...
 
int amxc_htable_init (amxc_htable_t *const htable, const size_t reserve)
 Initializes a hash table. More...
 
void amxc_htable_clean (amxc_htable_t *const htable, amxc_htable_it_delete_t func)
 Removes all items from the hash table. More...
 
void amxc_htable_set_hash_func (amxc_htable_t *const htable, amxc_htable_hash_func_t func)
 Sets the hash function for the hash table. More...
 
unsigned int amxc_htable_key2index (const amxc_htable_t *const htable, const char *const key)
 Converts a key into an index. More...
 
int amxc_htable_insert (amxc_htable_t *const htable, const char *const key, amxc_htable_it_t *const it)
 Inserts an item in the hash table. More...
 
amxc_htable_it_tamxc_htable_get (const amxc_htable_t *const htable, const char *const key)
 Gets a hash table iterator from the hash table. More...
 
amxc_htable_it_tamxc_htable_take (amxc_htable_t *const htable, const char *const key)
 Removes a hash table iterator from the hash table. More...
 
amxc_htable_it_tamxc_htable_get_first (const amxc_htable_t *const htable)
 Gets the first item stored in the table. More...
 
amxc_htable_it_tamxc_htable_get_last (const amxc_htable_t *const htable)
 Gets the last item stored in the table. More...
 
amxc_array_tamxc_htable_get_sorted_keys (const amxc_htable_t *const htable)
 Creates an array containing all keys of the hash table. More...
 
int amxc_htable_move (amxc_htable_t *const dest, amxc_htable_t *const src)
 Moves all items from one hash table to another hash table. More...
 

Detailed Description

Ambiorix hash table API implementation.

Definition in file amxc_htable.c.

Function Documentation

◆ amxc_htable_cmp_keys()

static int amxc_htable_cmp_keys ( amxc_array_it_t it1,
amxc_array_it_t it2 
)
static

Definition at line 165 of file amxc_htable.c.

165  {
166  const char* key1 = (const char*) amxc_array_it_get_data(it1);
167  const char* key2 = (const char*) amxc_array_it_get_data(it2);
168  return strcmp(key1 == NULL? "":key1, key2 == NULL? "":key2);
169 }
AMXC_INLINE void * amxc_array_it_get_data(const amxc_array_it_t *const it)
Gets the data pointer of array iterator.
Definition: amxc_array.h:729
static amxc_llist_it_t it2
static amxc_llist_it_t it1

◆ amxc_htable_grow()

static int amxc_htable_grow ( amxc_htable_t *const  htable,
size_t  hint 
)
static

Definition at line 85 of file amxc_htable.c.

85  {
86  int retval = -1;
87  size_t capacity = htable->table.items;
89  size_t start_bucket = htable->table.first_used;
90  size_t end_bucket = htable->table.last_used;
91 
92  htable->table.buffer = NULL;
93  htable->table.first_used = 0;
94  htable->table.last_used = 0;
95  htable->table.items = 0;
96  htable->items = 0;
97 
98  when_null(temp, exit);
99 
100  if(hint != 0) {
101  retval = amxc_array_grow(&htable->table, capacity + hint);
102  } else {
103  retval = amxc_array_grow(&htable->table, capacity > 1024 ? capacity + 1024 : 2 * capacity);
104  }
105  when_failed(retval, exit);
106 
107  for(size_t i = start_bucket; i <= end_bucket; i++) {
108  amxc_htable_it_t* hit = (amxc_htable_it_t*) temp[i].data;
109  amxc_htable_it_t* next = NULL;
110  if(hit == NULL) {
111  continue;
112  }
113  hit->ait = NULL;
114  next = hit->next;
115  hit->next = NULL;
117  while(next) {
118  hit = next;
119  hit->ait = NULL;
120  next = hit->next;
121  hit->next = NULL;
123  }
124  }
125  free(temp);
126 
127 exit:
128  return retval;
129 }
static void amxc_htable_insert_it(amxc_htable_t *const htable, amxc_htable_it_t *const it)
Definition: amxc_htable.c:68
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_null(x, l)
Definition: amxc_macros.h:126
int amxc_array_grow(amxc_array_t *const array, const size_t items)
Expands the array.
Definition: amxc_array.c:280
The array iterator structure.
Definition: amxc_array.h:174
size_t items
Definition: amxc_array.h:163
struct _amxc_array_it * buffer
Definition: amxc_array.h:166
size_t last_used
Definition: amxc_array.h:165
size_t first_used
Definition: amxc_array.h:164
The hash table iterator structure.
Definition: amxc_htable.h:138
amxc_array_it_t * ait
Definition: amxc_htable.h:139
amxc_htable_it_t * next
Definition: amxc_htable.h:141
amxc_array_t table
Definition: amxc_htable.h:176
size_t items
Definition: amxc_htable.h:177
char data[]
static amxc_htable_t * htable

◆ amxc_htable_insert_it()

static void amxc_htable_insert_it ( amxc_htable_t *const  htable,
amxc_htable_it_t *const  it 
)
static

Definition at line 68 of file amxc_htable.c.

69  {
70  unsigned int index = 0;
71  amxc_array_it_t* ait = NULL;
72 
75  ait = amxc_array_get_at(&htable->table, index);
76  // insert item
77  if((ait != NULL) && (ait->data != NULL)) {
78  it->next = (amxc_htable_it_t*) ait->data;
79  }
81  it->ait = ait;
82  htable->items++;
83 }
int amxc_array_it_set_data(amxc_array_it_t *const it, void *data)
Sets the data pointer of an array iterator.
amxc_array_it_t * amxc_array_get_at(const amxc_array_t *const array, const unsigned int index)
Gets the item iterator for the given index.
Definition: amxc_array.c:504
void amxc_htable_it_take(amxc_htable_it_t *const it)
Removes the iterator from the hash table.
unsigned int amxc_htable_key2index(const amxc_htable_t *const htable, const char *const key)
Converts a key into an index.
Definition: amxc_htable.c:225
static amxc_htable_it_t it[2000]

◆ amxc_htable_it_delete_func()

static void amxc_htable_it_delete_func ( amxc_array_it_t *const  it)
static

Definition at line 131 of file amxc_htable.c.

131  {
132  amxc_htable_t* htable = (amxc_htable_t*) it->array;
133  amxc_htable_it_t* current = (amxc_htable_it_t*) it->data;
134 
135  while(current != NULL) {
136  amxc_htable_it_t* next = current->next;
137  char* key = current->key;
138  current->key = NULL;
139  amxc_htable_it_take(current);
140  if(htable->it_del != NULL) {
141  htable->it_del(key, current);
142  }
143  free(key);
144  current = next;
145  }
146 }
The hash table structure.
Definition: amxc_htable.h:175
amxc_htable_it_delete_t it_del
Definition: amxc_htable.h:179