libamxc  1.10.3
C Generic Data Containers
Array iterators
Collaboration diagram for Array iterators:

Data Structures

struct  _amxc_array_it
 The array iterator structure. More...
 

Typedefs

typedef struct _amxc_array_it amxc_array_it_t
 The array iterator structure. More...
 
typedef int(* amxc_array_it_cmp_t) (amxc_array_it_t *it1, amxc_array_it_t *it2)
 Type definition of an array iterator compare callback function. More...
 

Functions

amxc_array_it_tamxc_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. More...
 
amxc_array_it_tamxc_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. More...
 
amxc_array_it_tamxc_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. More...
 
amxc_array_it_tamxc_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. More...
 
unsigned int amxc_array_it_index (const amxc_array_it_t *const it)
 Gets the index of the iterator in the array. More...
 
AMXC_INLINE void * amxc_array_it_get_data (const amxc_array_it_t *const it)
 Gets the data pointer of array iterator. More...
 
int amxc_array_it_set_data (amxc_array_it_t *const it, void *data)
 Sets the data pointer of an array iterator. More...
 
void * amxc_array_it_take_data (amxc_array_it_t *const it)
 Gets and removes a data pointer from the iterator. More...
 
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. More...
 

Detailed Description

Typedef Documentation

◆ amxc_array_it_cmp_t

typedef int(* amxc_array_it_cmp_t) (amxc_array_it_t *it1, amxc_array_it_t *it2)

Type definition of an array iterator compare callback function.

When sorting an array, the items in the array (iterators) must be compared. When calling amxc_array_sort a compare function must be provided using this signature.

Parameters
it1the first array iterator
it2the second array 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 213 of file amxc_array.h.

◆ amxc_array_it_t

The array iterator structure.

Function Documentation

◆ amxc_array_it_get_data()

AMXC_INLINE void* amxc_array_it_get_data ( const amxc_array_it_t *const  it)

Gets the data pointer of array iterator.

Parameters
itpointer to the item iterator
Returns
returns the data pointer of iterator or NULL if no data pointer was stored in that iterator.

Definition at line 729 of file amxc_array.h.

729  {
730  return (it != NULL) ? it->data : NULL;
731 }
static amxc_htable_it_t it[2000]

◆ amxc_array_it_get_next()

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.

This function iterates forwards over all items starting from the reference iterator and searches the next used item.

Parameters
referencearray iterator used as starting point.
Returns
returns the iterator of the next used item in the array starting from the reference iterator, or NULL if there are no more used items (End of array reached).

Definition at line 66 of file amxc_array_it.c.

66  {
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 }
#define when_null(x, l)
Definition: amxc_macros.h:126
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
static unsigned int array[2006]

◆ amxc_array_it_get_next_free()

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.

This function iterates forwards over all items starting from the reference iterator and searches the next free item.

Parameters
referencearray iterator used as starting point.
Returns
returns the iterator of the next free item in the array starting from the reference iterator, or NULL if there are no more free items (End of array reached).

Definition at line 87 of file amxc_array_it.c.

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

◆ amxc_array_it_get_previous()

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.

This function iterates backwards over all items starting from the reference iterator and searches the previous used item.

Parameters
referencearray iterator used as starting point.
Returns
returns the iterator of the previous used item in the array starting from the reference iterator, or NULL if there are no more used items (Start of array reached).

Definition at line 108 of file amxc_array_it.c.

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

◆ amxc_array_it_get_previous_free()

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.

This function iterates backwards over all items starting from the reference iterator and searches the previous free item.

Parameters
referencearray iterator used as starting point.
Returns
returns the iterator of the previous free item in the array starting from the reference iterator, or NULL if there are no more free items (Start of array reached).

Definition at line 128 of file amxc_array_it.c.

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

◆ amxc_array_it_index()

unsigned int amxc_array_it_index ( const amxc_array_it_t *const  it)

Gets the index of the iterator in the array.

Parameters
itpointer to the array iterator.
Returns
returns the index of the iterator.

Definition at line 148 of file amxc_array_it.c.

148  {
149  size_t index = 0;
150  when_null(it, exit);
151 
152  index = it - it->array->buffer;
153 
154 exit:
155  return index;
156 }

◆ amxc_array_it_set_data()

int amxc_array_it_set_data ( amxc_array_it_t *const  it,
void *  data 
)

Sets the data pointer of an array iterator.

Note
This function sets the pointer in the array. It is important that the pointer is valid as long as it is used in the array. Before freeing the memory block, remove the pointer from the array.
Parameters
itpointer to the item iterator
datapointer to the data
Returns
0 when the data is set, -1 otherwise.

Definition at line 158 of file amxc_array_it.c.

158  {
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 }
unsigned int amxc_array_it_index(const amxc_array_it_t *const it)
Gets the index of the iterator in the array.
char data[]

◆ amxc_array_it_swap()

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 iterators do not have to belong to the same array.

Parameters
it1pointer to the item iterator 1
it2pointer to the item iterator 2
Returns
0 when the iterators are swapped. TODO -1 if swapping fails.

Definition at line 218 of file amxc_array_it.c.

219  {
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 }
static amxc_llist_it_t it2
static amxc_llist_it_t it1

◆ amxc_array_it_take_data()

void* amxc_array_it_take_data ( amxc_array_it_t *const  it)

Gets and removes a data pointer from the iterator.

This functions resets the data pointer of the iterator back to NULL.

Parameters
itpointer to the item iterator
Returns
The data pointer of the iterator.

Definition at line 183 of file amxc_array_it.c.

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