libamxc  1.10.3
C Generic Data Containers
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
amxc_llist.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 
67  amxc_llist_it_cmp_t cmp) {
68  int retval = 0;
69  bool swapped = false;
70 
71  do {
73  swapped = false;
74 
75  while(it != NULL && it->next != NULL) {
76  if(cmp(it, it->next) > 0) {
78  swapped = true;
79  } else {
80  it = it->next;
81  }
82  }
83  } while(swapped);
84 
85  return retval;
86 }
87 
89  int retval = -1;
90  when_null(llist, exit);
91 
92  *llist = (amxc_llist_t*) calloc(1, sizeof(amxc_llist_t));
93  when_null(*llist, exit);
94 
95  retval = 0;
96 
97 exit:
98  return retval;
99 }
100 
102  when_null(llist, exit);
103 
104  amxc_llist_clean(*llist, func);
105  free(*llist);
106  *llist = NULL;
107 exit:
108  return;
109 }
110 
112  int retval = -1;
113  when_null(llist, exit);
114 
115  llist->head = NULL;
116  llist->tail = NULL;
117 
118  retval = 0;
119 
120 exit:
121  return retval;
122 }
123 
125  amxc_llist_it_t* it = llist != NULL ? llist->head : NULL;
126  while(it != NULL) {
128  if(func != NULL) {
129  func(it);
130  }
131  it = llist->head;
132  }
133 }
134 
135 int amxc_llist_move(amxc_llist_t* const dest, amxc_llist_t* const src) {
136  int retval = -1;
137 
138  when_null(dest, exit);
139  when_null(src, exit);
140 
141  amxc_llist_for_each(it, src) {
142  amxc_llist_append(dest, it);
143  }
144 
145  retval = 0;
146 
147 exit:
148  return retval;
149 }
150 
151 size_t amxc_llist_size(const amxc_llist_t* const llist) {
152  size_t count = 0;
153 
154  // no check on null pointer is needed here.
155  // amxc_llist_first will return null anyway.
156  for(amxc_llist_it_t* it = llist != NULL ? llist->head : NULL;
157  it != NULL;
158  it = it->next) {
159  count++;
160  }
161 
162  return count;
163 }
164 
166  return llist != NULL ? (llist->head == NULL) : true;
167 }
168 
170  int retval = -1;
171  when_null(llist, exit);
172  when_null(it, exit);
173 
174  if(it->llist != NULL) {
176  }
177 
178  it->llist = llist;
179  it->prev = llist->tail;
180  it->next = NULL;
181 
182  if(llist->tail != NULL) {
183  llist->tail->next = it;
184  llist->tail = it;
185  } else {
186  llist->tail = it;
187  llist->head = it;
188  }
189 
190  retval = 0;
191 
192 exit:
193  return retval;
194 }
195 
197  int retval = -1;
198  when_null(llist, exit);
199  when_null(it, exit);
200 
201  if(it->llist != NULL) {
203  }
204 
205  it->llist = llist;
206  it->next = llist->head;
207  it->prev = NULL;
208 
209  if(llist->head != NULL) {
210  llist->head->prev = it;
211  llist->head = it;
212  } else {
213  llist->tail = it;
214  llist->head = it;
215  }
216 
217  retval = 0;
218 exit:
219  return retval;
220 }
221 
223  const unsigned int index) {
224  amxc_llist_it_t* it = NULL;
225  size_t count = 0;
226 
227  for(it = llist != NULL ? llist->head : NULL; it; it = it->next) {
228  if(count == index) {
229  break;
230  }
231  count++;
232  }
233 
234  return it;
235 }
236 
238  const unsigned int index,
239  amxc_llist_it_t* const it) {
240  int retval = -1;
241  amxc_llist_it_t* reference = NULL;
242 
243  size_t count = 0;
244 
245  for(reference = llist != NULL ? llist->head : NULL;
246  reference;
247  reference = reference->next) {
248  if(count == index) {
249  break;
250  }
251  count++;
252  }
253 
254  if(!reference && (index == count)) {
255  retval = amxc_llist_append(llist, it);
256  } else {
257  retval = amxc_llist_it_insert_before(reference, it);
258  }
259  return retval;
260 }
261 
263  int retval = -1;
264  when_null(llist, exit);
265  when_null(cmp, exit);
266 
268  retval = 0;
269  goto exit;
270  }
271 
272  retval = amxc_llist_sort_internal(llist, cmp);
273 
274 exit:
275  return retval;
276 }
static int amxc_llist_sort_internal(amxc_llist_t *const llist, amxc_llist_it_cmp_t cmp)
Definition: amxc_llist.c:66
Ambiorix linked list API header file.
#define when_null(x, l)
Definition: amxc_macros.h:126
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.
int(* amxc_llist_it_cmp_t)(amxc_llist_it_t *it1, amxc_llist_it_t *it2)
Type definition of a linked list iterator compare callback function.
Definition: amxc_llist.h:336
void amxc_llist_it_take(amxc_llist_it_t *const it)
Removes the iterator from the list.
int amxc_llist_it_swap(amxc_llist_it_t *it1, amxc_llist_it_t *it2)
Swaps two linked list iterators.
int amxc_llist_move(amxc_llist_t *const dest, amxc_llist_t *const src)
Moves all items from one linked list to another linked list.
Definition: amxc_llist.c:135
void amxc_llist_delete(amxc_llist_t **llist, amxc_llist_it_delete_t func)
Frees the previously allocated linked list.
Definition: amxc_llist.c:101
size_t amxc_llist_size(const amxc_llist_t *const llist)
Calculates the size of the linked list.
Definition: amxc_llist.c:151
int amxc_llist_new(amxc_llist_t **llist)
Allocates a linked list.
Definition: amxc_llist.c:88
int amxc_llist_append(amxc_llist_t *const llist, amxc_llist_it_t *const it)
Adds an item to the end of the linked list.
Definition: amxc_llist.c:169
int amxc_llist_init(amxc_llist_t *const llist)
Initializes a linked list.
Definition: amxc_llist.c:111
int amxc_llist_sort(amxc_llist_t *const llist, amxc_llist_it_cmp_t cmp)
Sorts a linked list.
Definition: amxc_llist.c:262
bool amxc_llist_is_empty(const amxc_llist_t *const llist)
Checks that the linked list is empty.
Definition: amxc_llist.c:165
amxc_llist_it_t * amxc_llist_get_at(const amxc_llist_t *const llist, const unsigned int index)
Gets an item at a certain position of the linked list.
Definition: amxc_llist.c:222
void amxc_llist_clean(amxc_llist_t *const llist, amxc_llist_it_delete_t func)
Removes all items from the linked list.
Definition: amxc_llist.c:124
int amxc_llist_set_at(amxc_llist_t *const llist, const unsigned int index, amxc_llist_it_t *const it)
Inserts an item at a certain position.
Definition: amxc_llist.c:237
#define amxc_llist_for_each(it, list)
Loops over the list from head to tail.
Definition: amxc_llist.h:253
void(* amxc_llist_it_delete_t)(amxc_llist_it_t *it)
Definition of the linked list item delete function.
Definition: amxc_llist.h:317
int amxc_llist_prepend(amxc_llist_t *const llist, amxc_llist_it_t *const it)
Adds an item to the beginning of the linked list.
Definition: amxc_llist.c:196
AMXC_INLINE amxc_llist_it_t * amxc_llist_get_first(const amxc_llist_t *const llist)
Gets the first item of the linked list.
Definition: amxc_llist.h:713
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_it * prev
Definition: amxc_llist.h:218
The linked list structure.
Definition: amxc_llist.h:228
amxc_llist_it_t * head
Definition: amxc_llist.h:229
amxc_llist_it_t * tail
Definition: amxc_llist.h:230
static amxc_htable_it_t it[2000]
static amxc_llist_t * llist