libamxc  1.10.3
C Generic Data Containers
Hash Table Iterator
Collaboration diagram for Hash Table Iterator:

Data Structures

struct  _amxc_htable_it
 The hash table iterator structure. More...
 

Macros

#define amxc_htable_it_get_data(it, type, member)    ((type*) (((char*) it) - offsetof(type, member)))
 Gets the data pointer from an hash table iterator. More...
 

Functions

int amxc_htable_it_init (amxc_htable_it_t *const it)
 Initializes a hash table.iterator. More...
 
void amxc_htable_it_clean (amxc_htable_it_t *const it, amxc_htable_it_delete_t func)
 Removes the iterator from the htable and frees allocated memory. More...
 
amxc_htable_it_tamxc_htable_it_get_next (const amxc_htable_it_t *const reference)
 Gets the next iterator in the hash table. More...
 
amxc_htable_it_tamxc_htable_it_get_previous (const amxc_htable_it_t *const reference)
 Gets the previous iterator in the hash table. More...
 
amxc_htable_it_tamxc_htable_it_get_next_key (const amxc_htable_it_t *const reference)
 Gets the next iterator in the hash table with the same key. More...
 
amxc_htable_it_tamxc_htable_it_get_previous_key (const amxc_htable_it_t *const reference)
 Gets the previous iterator in the hash table with the same key. More...
 
void amxc_htable_it_take (amxc_htable_it_t *const it)
 Removes the iterator from the hash table. More...
 
AMXC_INLINE const char * amxc_htable_it_get_key (const amxc_htable_it_t *const it)
 Gets the key from the iterator. More...
 

Detailed Description

Macro Definition Documentation

◆ amxc_htable_it_get_data

#define amxc_htable_it_get_data (   it,
  type,
  member 
)     ((type*) (((char*) it) - offsetof(type, member)))

Gets the data pointer from an hash table iterator.

Definition at line 87 of file amxc_htable.h.

Function Documentation

◆ amxc_htable_it_clean()

void amxc_htable_it_clean ( amxc_htable_it_t *const  it,
amxc_htable_it_delete_t  func 
)

Removes the iterator from the htable and frees allocated memory.

If the iterator is in a hash table, it is removed from the table, when a delete function is provided, it is called to free up the memory.

Note
This function is not freeing the memory taken by the iterator, it uses a callback function for this.
Parameters
ita pointer to the hash table iterator structure
funcpointer to a function that is called to free the hash table item

Definition at line 82 of file amxc_htable_it.c.

82  {
83  char* key = NULL;
84  when_null(it, exit);
85 
86  // remove from htable if it is in one
88  key = it->key;
89  it->key = NULL;
90  if(func != NULL) {
91  func(key, it);
92  }
93 
94  free(key);
95 
96 exit:
97  return;
98 }
#define when_null(x, l)
Definition: amxc_macros.h:126
void amxc_htable_it_take(amxc_htable_it_t *const it)
Removes the iterator from the hash table.
static amxc_htable_it_t it[2000]

◆ amxc_htable_it_get_key()

AMXC_INLINE const char* amxc_htable_it_get_key ( const amxc_htable_it_t *const  it)

Gets the key from the iterator.

When an iterator is in a hash table, it has a key set. An iterator that is not in a list can have a key set.

Parameters
ita pointer to the hash table iterator structure
Returns
the key of the iterator or NULL if the iterator has no key

Definition at line 672 of file amxc_htable.h.

672  {
673  return it != NULL ? it->key : NULL;
674 }

◆ amxc_htable_it_get_next()

amxc_htable_it_t* amxc_htable_it_get_next ( const amxc_htable_it_t *const  reference)

Gets the next iterator in the hash table.

Searches the next hash table iterator in the hash table, using the provided iterator as a reference. The iterator returned can have another key as the reference provided. If it must have the same key, you should use amxc_htable_it_get_next_key

Parameters
referencea pointer to the hash table iterator structure
Returns
The next iterator in the hash table, starting from the reference or NULL if the reference was the last in the table.

Definition at line 100 of file amxc_htable_it.c.

100  {
101  amxc_htable_it_t* it = NULL;
102  when_null(reference, exit);
103  when_null(reference->ait, exit);
104 
105  if(reference->next != NULL) {
106  it = reference->next;
107  } else {
108  amxc_array_it_t* ait = amxc_array_it_get_next(reference->ait);
109  when_null(ait, exit);
110  it = (amxc_htable_it_t*) ait->data;
111  }
112 
113 exit:
114  return it;
115 }
amxc_array_it_t * amxc_array_it_get_next(const amxc_array_it_t *const reference)
Gets the next used item in the array, starting from the provided array iterator.
Definition: amxc_array_it.c:66
The array iterator structure.
Definition: amxc_array.h:174
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_htable_it_get_next_key()

amxc_htable_it_t* amxc_htable_it_get_next_key ( const amxc_htable_it_t *const  reference)

Gets the next iterator in the hash table with the same key.

Searches the next hash table iterator in the hash table, using the provided iterator as a reference. The iterator returned has the same key as the reference provided.

Parameters
referencea pointer to the hash table iterator structure
Returns
The next iterator in the hash table with the same key, starting from the reference or NULL if the reference was the last in the table with that key.

Definition at line 134 of file amxc_htable_it.c.

134  {
135  amxc_htable_it_t* it = NULL;
136  when_null(reference, exit);
137  when_null(reference->ait, exit);
138 
139  it = reference->next;
140  while(it != NULL && strcmp(it->key, reference->key) != 0) {
141  it = it->next;
142  }
143 
144 exit:
145  return it;
146 }

◆ amxc_htable_it_get_previous()

amxc_htable_it_t* amxc_htable_it_get_previous ( const amxc_htable_it_t *const  reference)

Gets the previous iterator in the hash table.

Searches the previous hash table iterator in the hash table, using the provided iterator as a reference. The iterator returned can have another key as the reference provided. If it must have the same key, you should use amxc_htable_it_get_previous_key

Parameters
referencea pointer to the hash table iterator structure
Returns
The previous iterator in the hash table, starting from the reference or NULL if the reference was the first in the table.

Definition at line 117 of file amxc_htable_it.c.

117  {
118  amxc_htable_it_t* it = NULL;
119  when_null(reference, exit);
120  when_null(reference->ait, exit);
121 
122  if(reference->next != NULL) {
123  it = reference->next;
124  } else {
126  when_null(ait, exit);
127  it = (amxc_htable_it_t*) ait->data;
128  }
129 
130 exit:
131  return it;
132 }
amxc_array_it_t * amxc_array_it_get_previous(const amxc_array_it_t *const reference)
Gets the previous used item in the array, starting from the provided array iterator.

◆ amxc_htable_it_get_previous_key()

amxc_htable_it_t* amxc_htable_it_get_previous_key ( const amxc_htable_it_t *const  reference)

Gets the previous iterator in the hash table with the same key.

Searches the previous hash table iterator in the hash table, using the provided iterator as a reference. The iterator returned has the same key as the reference provided.

Parameters
referencea pointer to the hash table iterator structure
Returns
The previous iterator in the hash table with the same key, starting from the reference or NULL if the reference was the first in the table with that key.

Definition at line 148 of file amxc_htable_it.c.

148  {
149  return amxc_htable_it_get_next_key(reference);
150 }
amxc_htable_it_t * amxc_htable_it_get_next_key(const amxc_htable_it_t *const reference)
Gets the next iterator in the hash table with the same key.

◆ amxc_htable_it_init()

int amxc_htable_it_init ( amxc_htable_it_t *const  it)

Initializes a hash table.iterator.

Initializes the hash table iterator structure. All pointers are reset to NULL.

Note
When calling this function on an already initialized hash table iterator, the hash table iterator is reset and data can be lost. Use amxc_htable_it_clean to remove the iterator from the table and to clean up the iterator.
Parameters
ita pointer to the hash table iterator structure.
Returns
0 on success. -1 if a NULL pointer is given.

Definition at line 68 of file amxc_htable_it.c.

68  {
69  int retval = -1;
70  when_null(it, exit);
71 
72  it->ait = NULL;
73  it->key = NULL;
74  it->next = NULL;
75 
76  retval = 0;
77 
78 exit:
79  return retval;
80 }

◆ amxc_htable_it_take()

void amxc_htable_it_take ( amxc_htable_it_t *const  it)

Removes the iterator from the hash table.

This function only removes the iterator from the hash table, it does not clean any memory allocated for the iterator.

Parameters
ita pointer to the hash table iterator structure

Definition at line 152 of file amxc_htable_it.c.

152  {
153  amxc_htable_t* htable = NULL;
154  when_null(it, exit);
155  when_null(it->ait, exit);
156 
158  if(it->ait->data != it) {
160  while(prev->next != it) {
161  prev = prev->next;
162  }
163  prev->next = it->next;
164  } else {
165  if(it->next != NULL) {
167  it->next = NULL;
168  } else {
170  }
171  }
172  it->ait = NULL;
173  it->next = NULL;
174  htable->items--;
175 
176 exit:
177  return;
178 }
int amxc_array_it_set_data(amxc_array_it_t *const it, void *data)
Sets the data pointer of an array iterator.
void * amxc_array_it_take_data(amxc_array_it_t *const it)
Gets and removes a data pointer from the iterator.
amxc_array_t * array
Definition: amxc_array.h:175
The hash table structure.
Definition: amxc_htable.h:175
size_t items
Definition: amxc_htable.h:177
static amxc_htable_t * htable