libamxc  1.10.3
C Generic Data Containers
amxc_array_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 
57 #include <amxc/amxc_array.h>
58 #include <amxc/amxc_macros.h>
59 
67  amxc_array_it_t* it = NULL;
68  amxc_array_t* array = NULL;
69  size_t pos = 0;
70  when_null(reference, exit);
71 
72  array = reference->array;
73  pos = (reference - array->buffer);
74  pos++;
75  while(pos < array->items && !array->buffer[pos].data) {
76  pos++;
77  }
78 
79  if(pos < array->items) {
80  it = &array->buffer[pos];
81  }
82 
83 exit:
84  return it;
85 }
86 
88  amxc_array_it_t* it = NULL;
89  amxc_array_t* array = NULL;
90  size_t pos = 0;
91  when_null(reference, exit);
92 
93  array = reference->array;
94  pos = (reference - array->buffer);
95  pos++;
96  while(pos < array->items && array->buffer[pos].data != NULL) {
97  pos++;
98  }
99 
100  if(pos < array->items) {
101  it = &array->buffer[pos];
102  }
103 
104 exit:
105  return it;
106 }
107 
109  amxc_array_it_t* it = NULL;
110  amxc_array_t* array = NULL;
111  size_t pos = 0;
112  when_null(reference, exit);
113 
114  array = reference->array;
115  pos = (reference - array->buffer);
116  while(pos > 0 && !array->buffer[pos - 1].data) {
117  pos--;
118  }
119 
120  if(pos > 0) {
121  it = &array->buffer[pos - 1];
122  }
123 
124 exit:
125  return it;
126 }
127 
129  amxc_array_it_t* it = NULL;
130  amxc_array_t* array = NULL;
131  size_t pos = 0;
132  when_null(reference, exit);
133 
134  array = reference->array;
135  pos = (reference - array->buffer);
136  while(pos > 0 && array->buffer[pos - 1].data != NULL) {
137  pos--;
138  }
139 
140  if((pos > 0) && !array->buffer[pos - 1].data) {
141  it = &array->buffer[pos - 1];
142  }
143 
144 exit:
145  return it;
146 }
147 
148 unsigned int amxc_array_it_index(const amxc_array_it_t* const it) {
149  size_t index = 0;
150  when_null(it, exit);
151 
152  index = it - it->array->buffer;
153 
154 exit:
155  return index;
156 }
157 
159  int retval = -1;
160  unsigned int index = 0;
161  amxc_array_t* array = NULL;
162 
163  when_null(it, exit);
164  when_null(data, exit);
165 
166  index = amxc_array_it_index(it);
167  array = it->array;
168  it->data = data;
169  if((index < array->first_used) ||
170  ((array->first_used == 0) && (array->buffer[0].data == NULL))) {
171  array->first_used = index;
172  }
173  if(index > array->last_used) {
174  array->last_used = index;
175  }
176 
177  retval = 0;
178 
179 exit:
180  return retval;
181 }
182 
184  void* data = NULL;
185  unsigned int index = 0;
186  amxc_array_t* array = NULL;
187 
188  when_null(it, exit);
189  when_null(it->data, exit);
190 
191  index = amxc_array_it_index(it);
192  array = it->array;
193 
194  data = it->data;
195  it->data = NULL;
196 
197  if(index == array->first_used) {
199  if(it == NULL) {
200  array->first_used = 0;
201  } else {
202  array->first_used = amxc_array_it_index(it);
203  }
204  }
205  if(index == array->last_used) {
207  if(it == NULL) {
208  array->last_used = 0;
209  } else {
210  array->last_used = amxc_array_it_index(it);
211  }
212  }
213 
214 exit:
215  return data;
216 }
217 
219  amxc_array_it_t* const it2) {
220  int retval = -1;
221  void* data = NULL;
222 
223  when_null(it1, exit);
224  when_null(it2, exit);
225 
226  data = it1->data;
227  it1->data = it2->data;
228  it2->data = data;
229 
230 exit:
231  return retval;
232 }
Ambiorix array API header file.
#define when_null(x, l)
Definition: amxc_macros.h:126
amxc_array_it_t * amxc_array_it_get_next_free(const amxc_array_it_t *const reference)
Gets the next free item in the array, starting from the provided array iterator.
Definition: amxc_array_it.c:87
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 *it)
Gets and removes a data pointer from the iterator.
amxc_array_it_t * amxc_array_it_get_previous_free(const amxc_array_it_t *const reference)
Gets the previous free item in the array, starting from the provided array iterator.
unsigned int amxc_array_it_index(const amxc_array_it_t *const it)
Gets the index of the iterator in the array.
int amxc_array_it_swap(amxc_array_it_t *const it1, amxc_array_it_t *const it2)
Swaps the content of the two array iterators.
The array iterator structure.
Definition: amxc_array.h:174
amxc_array_t * array
Definition: amxc_array.h:175
The array structure.
Definition: amxc_array.h:162
char data[]
static unsigned int array[2006]
static amxc_htable_it_t it[2000]
static amxc_llist_it_t it2
static amxc_llist_it_t it1