libamxc  1.10.3
C Generic Data Containers
amxc_htable_it.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdlib.h>
56 #include <string.h>
57 
58 #include <amxc/amxc_hash.h>
59 #include <amxc/amxc_htable.h>
60 #include <amxc/amxc_macros.h>
61 
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 }
81 
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 }
99 
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 }
116 
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 }
133 
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 }
147 
149  return amxc_htable_it_get_next_key(reference);
150 }
151 
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 }
Ambiorix string hash functions header file.
Ambiorix hash table API header file.
#define when_null(x, l)
Definition: amxc_macros.h:126
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_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_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
void * amxc_array_it_take_data(amxc_array_it_t *const it)
Gets and removes a data pointer from the iterator.
amxc_htable_it_t * amxc_htable_it_get_next(const amxc_htable_it_t *const reference)
Gets the next iterator in the hash table.
int amxc_htable_it_init(amxc_htable_it_t *const it)
Initializes a hash table.iterator.
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.
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.
void amxc_htable_it_take(amxc_htable_it_t *const it)
Removes the iterator from the hash table.
amxc_htable_it_t * amxc_htable_it_get_previous(const amxc_htable_it_t *const reference)
Gets the previous iterator in the hash table.
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.
void(* amxc_htable_it_delete_t)(const char *key, amxc_htable_it_t *it)
Definition of the hash table item delete function.
Definition: amxc_htable.h:168
The array iterator structure.
Definition: amxc_array.h:174
amxc_array_t * array
Definition: amxc_array.h:175
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
The hash table structure.
Definition: amxc_htable.h:175
size_t items
Definition: amxc_htable.h:177
static amxc_htable_it_t it[2000]
static amxc_htable_t * htable