libamxc  1.10.3
C Generic Data Containers
Linked List Iterator
Collaboration diagram for Linked List Iterator:

Data Structures

struct  _amxc_llist_it
 The linked list iterator structure. More...
 

Macros

#define amxc_llist_it_get_data(it, type, member)    amxc_container_of(it, type, member)
 Gets the data pointer from a linked list iterator. More...
 

Typedefs

typedef struct _amxc_llist_it amxc_llist_it_t
 The linked list iterator structure. More...
 
typedef 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. More...
 

Functions

int amxc_llist_it_init (amxc_llist_it_t *const it)
 Initializes a linked list iterator. More...
 
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. More...
 
void amxc_llist_it_take (amxc_llist_it_t *const it)
 Removes the iterator from the list. More...
 
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. More...
 
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. More...
 
unsigned int amxc_llist_it_index_of (const amxc_llist_it_t *const it)
 Gets the index of an iterator in the list. More...
 
int amxc_llist_it_swap (amxc_llist_it_t *it1, amxc_llist_it_t *it2)
 Swaps two linked list iterators. More...
 
AMXC_INLINE amxc_llist_it_tamxc_llist_it_get_next (const amxc_llist_it_t *const reference)
 Gets the next iterator in the list. More...
 
AMXC_INLINE amxc_llist_it_tamxc_llist_it_get_previous (const amxc_llist_it_t *const reference)
 Gets the previous iterator in the list. More...
 
AMXC_INLINE bool amxc_llist_it_is_in_list (const amxc_llist_it_t *const it)
 Checks that an iterator is in a list. More...
 

Detailed Description

Macro Definition Documentation

◆ amxc_llist_it_get_data

#define amxc_llist_it_get_data (   it,
  type,
  member 
)     amxc_container_of(it, type, member)

Gets the data pointer from a linked list iterator.

Definition at line 238 of file amxc_llist.h.

Typedef Documentation

◆ amxc_llist_it_cmp_t

typedef 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.

When sorting a linked list, the items in the list (iterators) must be compared. When calling amxc_llist_sort a compare function must be provided using this signature.

Parameters
it1the first linked list iterator
it2the second linked list iterator
Returns
The callback function must return
  • 0 when the values are equal
  • < 0 when it1 is smaller then it2
  • > 0 when it1 is bigger then it2

Definition at line 336 of file amxc_llist.h.

◆ amxc_llist_it_t

The linked list iterator structure.

Function Documentation

◆ amxc_llist_it_clean()

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.

If the iterator is in a list, it is removed from the list, when a delete function is provided, it is called to free up the memory.

Note
This function is not freeing the memory taken by the iterator, it uses a callback function for this.
Parameters
ita pointer to the linked list iterator structure
funca pointer to a function that is called to free the linked list item

Definition at line 103 of file amxc_llist_it.c.

103  {
105  if((it != NULL) && (func != NULL)) {
106  func(it);
107  }
108 }
void amxc_llist_it_take(amxc_llist_it_t *const it)
Removes the iterator from the list.
static amxc_htable_it_t it[2000]

◆ amxc_llist_it_get_next()

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.

This function does not remove the item from the linked list.

Parameters
referencea pointer to the linked list structure used as reference
Returns
Returns the next iterator of the linked list, or NULL when there is not more item in the linked list.

Definition at line 824 of file amxc_llist.h.

824  {
825  return reference != NULL ? reference->next : NULL;
826 }
struct _amxc_llist_it * next
Definition: amxc_llist.h:216

◆ amxc_llist_it_get_previous()

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.

This function does not remove the item from the linked list.

Parameters
referencea pointer to the linked list structure used as reference
Returns
Returns the previous iterator of the linked list, or NULL when there is no more items in the linked list.

Definition at line 842 of file amxc_llist.h.

842  {
843  return reference != NULL ? reference->prev : NULL;
844 }
struct _amxc_llist_it * prev
Definition: amxc_llist.h:218

◆ amxc_llist_it_index_of()

unsigned int amxc_llist_it_index_of ( const amxc_llist_it_t *const  it)

Gets the index of an iterator in the list.

Parameters
ita pointer to the linked list iterator structure for which the index needs to be calculated
Returns
The index of the iterator or AMXC_LLIST_RANGE if the iterator is not in a list.

Definition at line 182 of file amxc_llist_it.c.

182  {
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 }
#define AMXC_LLIST_RANGE
Definition: amxc_llist.h:69
The linked list iterator structure.
Definition: amxc_llist.h:215

◆ amxc_llist_it_init()

int amxc_llist_it_init ( amxc_llist_it_t *const  it)

Initializes a linked list iterator.

Initializes the linked list iterator structure. All pointers are reset to NULL.

Note
When calling this function on an already initialized linked list iterator, the linked list iterator is reset and the list the iterator was in is corrupted. Use amxc_llist_it_take to remove the iterator from the list or amxc_llist_it_clean to remove it from the list and clean up the iterator
Parameters
ita pointer to the linked list iterator structure.
Returns
0 on success. -1 if a NULL pointer is given.

Definition at line 89 of file amxc_llist_it.c.

89  {
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 }
#define when_null(x, l)
Definition: amxc_macros.h:126
amxc_htable_it_t * next
Definition: amxc_htable.h:141

◆ amxc_llist_it_insert_after()

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.

If the iterator(it) is already in the list, it is first removed from the list before it is inserted at the correct position.

Parameters
referencea pointer to the linked list iterator structure that is used as reference
ita pointer to the linked list iterator structure that needs to be inserted
Returns
-1 if the reference iterator is not in a list. 0 if the iterator is inserted

Definition at line 157 of file amxc_llist_it.c.

158  {
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 }
struct _amxc_llist * llist
Definition: amxc_llist.h:220

◆ amxc_llist_it_insert_before()

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.

If the iterator is already in a list, it is first removed from the list.

Parameters
referencea pointer to the linked list iterator structure that is used as reference
ita pointer to the linked list iterator structure that needs to be inserted
Returns
-1 if the reference iterator is not in a list. 0 if the iterator is inserted

Definition at line 132 of file amxc_llist_it.c.

133  {
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 }

◆ amxc_llist_it_is_in_list()

AMXC_INLINE bool amxc_llist_it_is_in_list ( const amxc_llist_it_t *const  it)

Checks that an iterator is in a list.

Parameters
ita pointer to the linked list structure.
Returns
true when the iterator is in the list, or false if it is not in a list

Definition at line 857 of file amxc_llist.h.

857  {
858  return (it != NULL && it->llist != NULL) ? true : false;
859 }

◆ amxc_llist_it_swap()

int amxc_llist_it_swap ( amxc_llist_it_t it1,
amxc_llist_it_t it2 
)

Swaps two linked list iterators.

The iterators being swapped can be in the same linked list or in different linked lists.

Parameters
it1a pointer to the linked list iterator
it2a pointer to the linked list iterator
Returns
0 when swapping the iterators was successful, when failed a non zero value is returned.

Definition at line 200 of file amxc_llist_it.c.

201  {
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 }
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
The linked list structure.
Definition: amxc_llist.h:228
static amxc_llist_it_t it2
static amxc_llist_it_t it1

◆ amxc_llist_it_take()

void amxc_llist_it_take ( amxc_llist_it_t *const  it)

Removes the iterator from the list.

Parameters
ita pointer to the linked list iterator structure

Definition at line 110 of file amxc_llist_it.c.

110  {
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 }