libamxc  1.10.3
C Generic Data Containers
amxc_llist.h
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 #if !defined(__AMXC_LLIST_H__)
56 #define __AMXC_LLIST_H__
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 #include <stddef.h>
64 #include <stdbool.h>
65 #include <stdint.h>
66 
67 #include <amxc/amxc_common.h>
68 
69 #define AMXC_LLIST_RANGE UINT32_MAX
70 
215 typedef struct _amxc_llist_it {
220  struct _amxc_llist* llist;
222 
228 typedef struct _amxc_llist {
232 
238 #define amxc_llist_it_get_data(it, type, member) \
239  amxc_container_of(it, type, member)
240 
253 #define amxc_llist_for_each(it, list) \
254  for(amxc_llist_it_t* it = amxc_llist_get_first(list), \
255  * it ## _next = amxc_llist_it_get_next(it); \
256  it; \
257  it = it ## _next, \
258  it ## _next = amxc_llist_it_get_next(it))
259 
270 #define amxc_llist_iterate(it, list) \
271  for(amxc_llist_it_t* it = amxc_llist_get_first(list); \
272  it; \
273  it = amxc_llist_it_get_next(it))
274 
287 #define amxc_llist_for_each_reverse(it, list) \
288  for(amxc_llist_it_t* it = amxc_llist_get_last(list), \
289  * it ## _prev = amxc_llist_it_get_previous(it); \
290  it; \
291  it = it ## _prev, \
292  it ## _prev = amxc_llist_it_get_previous(it))
293 
304 #define amxc_llist_iterate_reverse(it, list) \
305  for(amxc_llist_it_t* it = amxc_llist_get_last(list); \
306  it; \
307  it = amxc_llist_it_get_previous(it))
308 
318 
338 
360 
380 
403 
418 
438 int amxc_llist_move(amxc_llist_t* const dest, amxc_llist_t* const src);
439 
451 bool amxc_llist_is_empty(const amxc_llist_t* const llist);
452 
470 size_t amxc_llist_size(const amxc_llist_t* const llist);
471 
491 
511  amxc_llist_it_t* const it);
512 
536  const unsigned int index);
537 
562  const unsigned int index,
563  amxc_llist_it_t* const it);
564 
585 
602 
611 
628 int amxc_llist_it_insert_before(amxc_llist_it_t* const reference,
629  amxc_llist_it_t* const it);
630 
648 int amxc_llist_it_insert_after(amxc_llist_it_t* const reference,
649  amxc_llist_it_t* const it);
650 
662 unsigned int amxc_llist_it_index_of(const amxc_llist_it_t* const it);
663 
681 
697 
714  return llist != NULL ? llist->head : NULL;
715 }
716 
733  return llist != NULL ? llist->tail : NULL;
734 }
735 
755  return it;
756 }
757 
777  return it;
778 }
779 
804  const unsigned int index) {
807  return it;
808 }
809 
825  return reference != NULL ? reference->next : NULL;
826 }
827 
843  return reference != NULL ? reference->prev : NULL;
844 }
845 
858  return (it != NULL && it->llist != NULL) ? true : false;
859 }
860 
861 #ifdef __cplusplus
862 }
863 #endif
864 
865 #endif // __AMXC_LLIST_H__
#define AMXC_INLINE
Definition: amxc_common.h:64
int amxc_llist_it_init(amxc_llist_it_t *const it)
Initializes a linked list iterator.
Definition: amxc_llist_it.c:89
AMXC_INLINE bool amxc_llist_it_is_in_list(const amxc_llist_it_t *const it)
Checks that an iterator is in a list.
Definition: amxc_llist.h:857
AMXC_INLINE amxc_llist_it_t * amxc_llist_it_get_previous(const amxc_llist_it_t *const reference)
Gets the previous iterator in the list.
Definition: amxc_llist.h:842
struct _amxc_llist_it amxc_llist_it_t
The linked list iterator structure.
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.
AMXC_INLINE amxc_llist_it_t * amxc_llist_it_get_next(const amxc_llist_it_t *const reference)
Gets the next iterator in the list.
Definition: amxc_llist.h:824
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_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.
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
AMXC_INLINE amxc_llist_it_t * amxc_llist_take_last(amxc_llist_t *const llist)
Removes the last item from the linked list.
Definition: amxc_llist.h:774
AMXC_INLINE amxc_llist_it_t * amxc_llist_take_at(const amxc_llist_t *llist, const unsigned int index)
Removes an item at a certain position of the linked list.
Definition: amxc_llist.h:803
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
struct _amxc_llist amxc_llist_t
The linked list structure.
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
AMXC_INLINE amxc_llist_it_t * amxc_llist_take_first(amxc_llist_t *const llist)
Removes the first item from the linked list.
Definition: amxc_llist.h:752
int amxc_llist_set_at(amxc_llist_t *llist, const unsigned int index, amxc_llist_it_t *const it)
Inserts an item at a certain position.
Definition: amxc_llist.c:237
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_INLINE amxc_llist_it_t * amxc_llist_get_last(const amxc_llist_t *const llist)
Gets the last item of the linked list.
Definition: amxc_llist.h:732
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
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
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
static amxc_llist_it_t it2
static amxc_llist_it_t it1