libamxc  1.10.3
C Generic Data Containers
amxc_llist_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_llist.h>
58 #include <amxc/amxc_macros.h>
59 
61  amxc_llist_it_t* const it2) {
62  return (it1->next == it2);
63 }
64 
66  if(it->prev == NULL) {
67  if(it->llist != NULL) {
68  it->llist->head = it;
69  }
70  } else {
71  it->prev->next = it;
72  }
73 
74  if(it->next == NULL) {
75  if(it->llist != NULL) {
76  it->llist->tail = it;
77  }
78  } else {
79  it->next->prev = it;
80  }
81 }
82 
90  int retval = -1;
91  when_null(it, exit);
92 
93  it->next = NULL;
94  it->prev = NULL;
95  it->llist = NULL;
96 
97  retval = 0;
98 
99 exit:
100  return retval;
101 }
102 
105  if((it != NULL) && (func != NULL)) {
106  func(it);
107  }
108 }
109 
111  when_null(it, exit);
112  when_null(it->llist, exit);
113 
114  if(it->prev != NULL) {
115  it->prev->next = it->next;
116  } else {
117  it->llist->head = it->next;
118  }
119  if(it->next != NULL) {
120  it->next->prev = it->prev;
121  } else {
122  it->llist->tail = it->prev;
123  }
124 
125  it->next = NULL;
126  it->prev = NULL;
127  it->llist = NULL;
128 exit:
129  return;
130 }
131 
133  amxc_llist_it_t* const it) {
134  int retval = -1;
135  when_null(reference, exit);
136  when_null(it, exit);
137  when_null(reference->llist, exit);
138 
140 
141  it->next = reference;
142  it->prev = reference->prev;
143  it->llist = reference->llist;
144  reference->prev = it;
145 
146  if(it->prev != NULL) {
147  it->prev->next = it;
148  } else {
149  it->llist->head = it;
150  }
151 
152  retval = 0;
153 exit:
154  return retval;
155 }
156 
158  amxc_llist_it_t* const it) {
159  int retval = -1;
160  when_null(reference, exit);
161  when_null(it, exit);
162  when_null(reference->llist, exit);
163 
165 
166  it->next = reference->next;
167  it->prev = reference;
168  it->llist = reference->llist;
169  reference->next = it;
170 
171  if(it->next != NULL) {
172  it->next->prev = it;
173  } else {
174  it->llist->tail = it;
175  }
176 
177  retval = 0;
178 exit:
179  return retval;
180 }
181 
182 unsigned int amxc_llist_it_index_of(const amxc_llist_it_t* const it) {
183  size_t index = 0;
184  const amxc_llist_it_t* pos = NULL;
185  if((it == NULL) || (it->llist == NULL)) {
186  index = AMXC_LLIST_RANGE;
187  goto exit;
188  }
189 
190  pos = it;
191  while(pos->prev) {
192  index++;
193  pos = pos->prev;
194  }
195 
196 exit:
197  return index;
198 }
199 
201  amxc_llist_it_t* it2) {
202  int retval = -1;
203  amxc_llist_it_t* swapperVector[4] = { NULL, NULL, NULL, NULL };
204 
205  when_null(it1, exit);
206  when_null(it2, exit);
207 
208  if(it1 == it2) {
209  retval = 0;
210  goto exit;
211  }
212 
213  if(it1->llist != it2->llist) {
214  amxc_llist_t* temp = it1->llist;
215  it1->llist = it2->llist;
216  it2->llist = temp;
217  }
218 
219  if(it2->next == it1) {
220  amxc_llist_it_t* temp = it1;
221  it1 = it2;
222  it2 = temp;
223  }
224 
225  swapperVector[0] = it1->prev;
226  swapperVector[1] = it2->prev;
227  swapperVector[2] = it1->next;
228  swapperVector[3] = it2->next;
229 
231  it1->prev = swapperVector[2];
232  it2->prev = swapperVector[0];
233  it1->next = swapperVector[3];
234  it2->next = swapperVector[1];
235  } else {
236  it1->prev = swapperVector[1];
237  it2->prev = swapperVector[0];
238  it1->next = swapperVector[3];
239  it2->next = swapperVector[2];
240  }
241 
244 
245  retval = 0;
246 
247 exit:
248  return retval;
249 }
Ambiorix linked list API header file.
#define AMXC_LLIST_RANGE
Definition: amxc_llist.h:69
static void amxc_llist_it_update(amxc_llist_it_t *const it)
Definition: amxc_llist_it.c:65
static bool amxc_llist_it_are_adjacent(amxc_llist_it_t *const it1, amxc_llist_it_t *const it2)
Definition: amxc_llist_it.c:60
#define when_null(x, l)
Definition: amxc_macros.h:126
int amxc_llist_it_init(amxc_llist_it_t *const it)
Initializes a linked list iterator.
Definition: amxc_llist_it.c:89
int amxc_llist_it_insert_after(amxc_llist_it_t *const reference, amxc_llist_it_t *const it)
Inserts an iterator after another reference interator in the list.
int amxc_llist_it_insert_before(amxc_llist_it_t *const reference, amxc_llist_it_t *const it)
Inserts an iterator before a reference interator in the list.
void amxc_llist_it_clean(amxc_llist_it_t *const it, amxc_llist_it_delete_t func)
Removes the iterator from the list and frees allocated memory.
void amxc_llist_it_take(amxc_llist_it_t *const it)
Removes the iterator from the list.
unsigned int amxc_llist_it_index_of(const amxc_llist_it_t *const it)
Gets the index of an iterator in the list.
int amxc_llist_it_swap(amxc_llist_it_t *it1, amxc_llist_it_t *it2)
Swaps two linked list iterators.
void(* amxc_llist_it_delete_t)(amxc_llist_it_t *it)
Definition of the linked list item delete function.
Definition: amxc_llist.h:317
amxc_htable_it_t * next
Definition: amxc_htable.h:141
The linked list iterator structure.
Definition: amxc_llist.h:215
struct _amxc_llist_it * next
Definition: amxc_llist.h:216
struct _amxc_llist * llist
Definition: amxc_llist.h:220
struct _amxc_llist_it * prev
Definition: amxc_llist.h:218
The linked list structure.
Definition: amxc_llist.h:228
static amxc_htable_it_t it[2000]
static amxc_llist_it_t it2
static amxc_llist_it_t it1