libamxc  1.10.3
C Generic Data Containers
Array

The Ambiorix Dynamic Bucket Array. More...

Collaboration diagram for Array:

Modules

 Array iterators
 

Data Structures

struct  _amxc_array
 The array structure. More...
 

Typedefs

typedef struct _amxc_array amxc_array_t
 The array structure. More...
 
typedef void(* amxc_array_it_delete_t) (amxc_array_it_t *it)
 Definition of the array item delete callback function. More...
 

Functions

int8_t amxc_array_new (amxc_array_t **array, const size_t items)
 Allocates an array. More...
 
void amxc_array_delete (amxc_array_t **array, const amxc_array_it_delete_t func)
 Frees the previously allocated array. More...
 
int amxc_array_init (amxc_array_t *const array, const size_t items)
 Initializes an array. More...
 
void amxc_array_clean (amxc_array_t *const array, amxc_array_it_delete_t func)
 Removes all items from the array. More...
 
int amxc_array_grow (amxc_array_t *const array, const size_t items)
 Expands the array. More...
 
int amxc_array_shrink (amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
 Shrinks the array. More...
 
int amxc_array_shift_right (amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
 Shift all items to the right in the array. More...
 
int amxc_array_shift_left (amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
 Shift all items to the left in the array. More...
 
bool amxc_array_is_empty (const amxc_array_t *const array)
 Checks that the array is empty. More...
 
size_t amxc_array_size (const amxc_array_t *const array)
 Calculates the number of used items in the array. More...
 
amxc_array_it_tamxc_array_append_data (amxc_array_t *const array, void *data)
 Adds an item after the last used item in the array. More...
 
amxc_array_it_tamxc_array_prepend_data (amxc_array_t *const array, void *data)
 Adds an item before the first used item in the array. More...
 
amxc_array_it_tamxc_array_get_at (const amxc_array_t *const array, const unsigned int index)
 Gets the item iterator for the given index. More...
 
amxc_array_it_tamxc_array_set_data_at (amxc_array_t *const array, const unsigned int index, void *data)
 Sets data at the given index. More...
 
amxc_array_it_tamxc_array_get_first (const amxc_array_t *const array)
 Gets the item iterator of the first used item in the array. More...
 
amxc_array_it_tamxc_array_get_first_free (const amxc_array_t *const array)
 Gets the first free position in the array. More...
 
amxc_array_it_tamxc_array_get_last (const amxc_array_t *const array)
 Gets the item iterator of the last used item in the array. More...
 
amxc_array_it_tamxc_array_get_last_free (const amxc_array_t *const array)
 Gets the last free position in the array. More...
 
void * amxc_array_take_first_data (amxc_array_t *const array)
 Takes the data pointer from the first used item in the array. More...
 
void * amxc_array_take_last_data (amxc_array_t *const array)
 Takes the data pointer from the last used item in the array. More...
 
AMXC_INLINE size_t amxc_array_capacity (const amxc_array_t *const array)
 Gets the capacity of the array. More...
 
AMXC_INLINE void * amxc_array_get_data_at (const amxc_array_t *const array, const unsigned int index)
 Gets the data pointer of the item at the given index. More...
 
int amxc_array_sort (amxc_array_t *const array, amxc_array_it_cmp_t cmp)
 Sorts the content of the array. More...
 

Detailed Description

The Ambiorix Dynamic Bucket Array.

A bucket is basically a pointer to a memory block (like a structure). This array implementation provides an array of pointers (aka buckets), the allocated memory for the buckets is a sequential memory block.

A bucket is considered empty when the pointer is NULL.

The array will grow dynamically when items are added to the end.

The items (or buckets) are defined using amxc_array_it_t structure. This structure contains the pointer to the data and the pointer to the beginning of the array.

Creating A Bucket Array
A bucket array can be declared on the stack or allocated in the heap. When declaring a bucket array on the stack it must be correctly initialized.
Adding items
Items can be added:
Convert An Array Bucket Iterator To Data Struct
The amxc_array_it_t contains a pointer (void *) to the data. This pointer can be fetched using amxc_array_it_get_data and cast to the correct type.
Looping Over A Bucket Array
Using a simple for loop it is possible to iterate over all buckets available. To get the full size, including the empty buckets, use amxc_array_capacity. Using an index all buckets can be addressed using amxc_array_get_at

To get the number of used buckets in the array use amxc_array_size.

If you only want to loop over the used buckets (with a non NULL data pointer), use the functions amxc_array_get_first, amxc_array_it_get_next

Removing Data From The Bucket Array
Removing a pointer from a bucket is basically resetting the data pointer to NULL. The function amxc_array_it_take_data will reset the pointer of the bucket to NULL and will return the pointer.
Finding Empty Buckets
It is possible to find the empty buckets:

Typedef Documentation

◆ amxc_array_it_delete_t

typedef void(* amxc_array_it_delete_t) (amxc_array_it_t *it)

Definition of the array item delete callback function.

A pointer to a delete function is used in the following functions: amxc_array_delete, amxc_array_clean, amxc_array_shrink, amxc_array_shift_right, amxc_array_shift_left.

When an array contains pointers to heap allocated memory blocks, that are possibly nowhere else referenced, this may lead to memory leaks. Providing a callback function to free the memory is easier and more efficient then first iterating over the array freeing up and then call one of the previously mentioned functions.

Definition at line 194 of file amxc_array.h.

◆ amxc_array_t

typedef struct _amxc_array amxc_array_t

The array structure.

The number of available items in the allocated buffer is tracked in the items member of this structure. C does not provide any standard way to get the size of an allocated memory block.

The structure also contains the first_used and last_used members. These are mainly used to be able to add data after the last taken position or to add data before the first taken position.

Alternatively the functions inserting data after or before can iterate over the array start from the end or from the beginning, this could have some significant impact on the performance of these functions.

Although the members of this structure are publicly available it is not recommended to access them directly, use the provided functions.

Function Documentation

◆ amxc_array_append_data()

amxc_array_it_t* amxc_array_append_data ( amxc_array_t *const  array,
void *  data 
)

Adds an item after the last used item in the array.

The array grows if the item can not be added to the array.

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
arraya pointer to the array structure
dataa pointer to the data that is added to the array
Returns
returns the array iterator where the data is added, or NULL when adding the item fails.

Definition at line 429 of file amxc_array.c.

429  {
430  amxc_array_it_t* it = NULL;
431  size_t index = 0;
432  when_null(array, exit);
433  when_null(data, exit);
434 
435  if(!amxc_array_is_empty(array)) {
436  index = array->last_used + 1;
437  }
438 
439  if(index >= array->items) {
441  }
442 
443  it = amxc_array_set_data_at(array, index, data);
444 
445 exit:
446  return it;
447 }
#define AMXC_ARRAY_AUTO_GROW_ITEMS
Definition: amxc_array.c:61
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_null(x, l)
Definition: amxc_macros.h:126
amxc_array_it_t * amxc_array_set_data_at(amxc_array_t *const array, const unsigned int index, void *data)
Sets data at the given index.
Definition: amxc_array.c:474
bool amxc_array_is_empty(const amxc_array_t *const array)
Checks that the array is empty.
Definition: amxc_array.c:399
int amxc_array_grow(amxc_array_t *const array, const size_t items)
Expands the array.
Definition: amxc_array.c:280
The array iterator structure.
Definition: amxc_array.h:174
char data[]
static unsigned int array[2006]
static amxc_htable_it_t it[2000]

◆ amxc_array_capacity()

AMXC_INLINE size_t amxc_array_capacity ( const amxc_array_t *const  array)

Gets the capacity of the array.

The capacity of an array is the number of items that can be stored in the array.

Parameters
arraypointer to the array structure
Returns
returns the capacity of the array, expressed in number of items.

Definition at line 694 of file amxc_array.h.

694  {
695  return (array != NULL) ? array->items : 0;
696 }

◆ amxc_array_clean()

void amxc_array_clean ( amxc_array_t *const  array,
amxc_array_it_delete_t  func 
)

Removes all items from the array.

Removes all items from the array. If a delete function is provided, it is called for each used item in the array.

Parameters
arraya pointer to the array structure
funca pointer to a function that is called to free each used item in the array

Definition at line 261 of file amxc_array.c.

261  {
262  when_null(array, exit);
263 
264  if(func != NULL) {
265  // When no delete callback function is given,
266  // it is not needed to call the clean items.
267  amxc_array_clean_items(array, 0, array->items, func);
268  }
269 
270  free(array->buffer);
271  array->buffer = NULL;
272  array->items = 0;
273  array->first_used = 0;
274  array->last_used = 0;
275 
276 exit:
277  return;
278 }
static void amxc_array_clean_items(amxc_array_t *array, const size_t start_pos, const size_t items, amxc_array_it_delete_t func)
Definition: amxc_array.c:80

◆ amxc_array_delete()

void amxc_array_delete ( amxc_array_t **  array,
const amxc_array_it_delete_t  func 
)

Frees the previously allocated array.

Removes all items from the array, if a delete function is provided, it is called for each used item in the array.

Frees the allocated memory and sets the pointer to the array to NULL.

Note
Only call this function for arrays that are allocated on the heap using amxc_array_new
Parameters
arraya pointer to the location where the pointer to the array is stored
funca pointer to a function that is called to free each item in the array

Definition at line 213 of file amxc_array.c.

213  {
214  when_null(array, exit);
215  when_null(*array, exit);
216 
217  if(func != NULL) {
218  // When no delete callback function is given,
219  // it is not needed to call the clean items.
220  amxc_array_clean_items(*array, 0, (*array)->items, func);
221  }
222 
223  free((*array)->buffer);
224  free(*array);
225  *array = NULL;
226 
227 exit:
228  return;
229 }

◆ amxc_array_get_at()

amxc_array_it_t* amxc_array_get_at ( const amxc_array_t *const  array,
const unsigned int  index 
)

Gets the item iterator for the given index.

Parameters
arraya pointer to the array structure
indexthe position in the array for which the iterator has to be returned
Returns
returns the array iterator for the given index or NULL if the index is out of boundary.

Definition at line 504 of file amxc_array.c.

505  {
506  amxc_array_it_t* it = NULL;
507  when_null(array, exit);
508  when_null(array->buffer, exit);
509  when_true(index >= array->items, exit);
510 
511  it = &array->buffer[index];
512 
513 exit:
514  return it;
515 }
#define when_true(x, l)
Definition: amxc_macros.h:134

◆ amxc_array_get_data_at()

AMXC_INLINE void* amxc_array_get_data_at ( const amxc_array_t *const  array,
const unsigned int  index 
)

Gets the data pointer of the item at the given index.

Parameters
arraypointer to the array structure
indexthe index of the item
Returns
returns the data pointer of the item at the given index or NULL if no data pointer was stored in that item or when the index is out of boundery.

Definition at line 711 of file amxc_array.h.

712  {
714  return (it != NULL) ? it->data : NULL;
715 }
amxc_array_it_t * amxc_array_get_at(const amxc_array_t *const array, const unsigned int index)
Gets the item iterator for the given index.
Definition: amxc_array.c:504

◆ amxc_array_get_first()

amxc_array_it_t* amxc_array_get_first ( const amxc_array_t *const  array)

Gets the item iterator of the first used item in the array.

Parameters
arraya pointer to the array structure
Returns
returns the array iterator for the first used item in the array or NULL if there is no used item in the array.

Definition at line 517 of file amxc_array.c.

517  {
518  amxc_array_it_t* it = NULL;
519  when_null(array, exit);
520 
521  if(!amxc_array_is_empty(array)) {
522  it = &array->buffer[array->first_used];
523  }
524 
525 exit:
526  return it;
527 }

◆ amxc_array_get_first_free()

amxc_array_it_t* amxc_array_get_first_free ( const amxc_array_t *const  array)

Gets the first free position in the array.

Parameters
arraya pointer to the array structure
Returns
returns the array iterator for the first free item in the array or NULL if there is no free item in the array.

Definition at line 529 of file amxc_array.c.

529  {
530  amxc_array_it_t* it = NULL;
531  size_t index = 0;
532  when_null(array, exit);
533 
534  while(index < array->items && array->buffer[index].data != NULL) {
535  index++;
536  }
537 
538  if(index < array->items) {
539  it = &array->buffer[index];
540  }
541 
542 exit:
543  return it;
544 }

◆ amxc_array_get_last()

amxc_array_it_t* amxc_array_get_last ( const amxc_array_t *const  array)

Gets the item iterator of the last used item in the array.

Parameters
arraya pointer to the array structure
Returns
returns the array iterator for the last used item in the array or NULL if there is no used item in the array.

Definition at line 546 of file amxc_array.c.

546  {
547  amxc_array_it_t* it = NULL;
548  when_null(array, exit);
549 
550  if(!amxc_array_is_empty(array)) {
551  it = &array->buffer[array->last_used];
552  }
553 
554 exit:
555  return it;
556 }

◆ amxc_array_get_last_free()

amxc_array_it_t* amxc_array_get_last_free ( const amxc_array_t *const  array)

Gets the last free position in the array.

Parameters
arraya pointer to the array structure
Returns
returns the array iterator for the last free item in the array or NULL if there is no free item in the array.

Definition at line 558 of file amxc_array.c.

558  {
559  amxc_array_it_t* it = NULL;
560  size_t index = 0;
561  when_null(array, exit);
562 
563  index = array->items;
564  while(index > 0 && array->buffer[index - 1].data != NULL) {
565  index--;
566  }
567 
568  if(index > 0) {
569  it = &array->buffer[index - 1];
570  }
571 
572 exit:
573  return it;
574 }

◆ amxc_array_grow()

int amxc_array_grow ( amxc_array_t *const  array,
const size_t  items 
)

Expands the array.

Expands the array by the given number of items. Extra memory is allocated to be able to store the number of items requested.

Parameters
arraya pointer to the array structure
itemsthe number of items the array has to grow
Returns
0 on success. -1 if an error has occured.

Definition at line 280 of file amxc_array.c.

280  {
281  int retval = -1;
282  size_t old_items = 0;
283  when_null(array, exit);
284 
285  if(items == 0) {
286  retval = 0;
287  goto exit;
288  }
289 
290  old_items = array->items;
291  retval = amxc_array_realloc(array, array->items + items);
293 
294 exit:
295  return retval;
296 }
static void amxc_array_initialize_items(amxc_array_t *array, const size_t start_pos)
Definition: amxc_array.c:70
static int amxc_array_realloc(amxc_array_t *array, const size_t items)
Definition: amxc_array.c:98

◆ amxc_array_init()

int amxc_array_init ( amxc_array_t *const  array,
const size_t  items 
)

Initializes an array.

Initializes the array structure. Memory is allocated from the heap to be able to store the number of items requested. All the items are initialized to NULL. This function is typically called for arrays that are on the stack. Allocating and initializing an array on the heap can be done using amxc_array_new

Note
When calling this function on an already initialized array, that contains items, the array is reset and all items in the array are lost (This could potentially lead to memory leaks). Use amxc_array_clean to remove all items from the list.
Parameters
arraya pointer to the array structure.
itemsthe size of the array in number of items
Returns
0 on success. -1 if a NULL pointer is given.

Definition at line 231 of file amxc_array.c.

231  {
232  int retval = -1;
233  when_null(array, exit);
234 
235  // initialize array data
236  array->items = 0;
237  array->buffer = NULL;
238  array->first_used = 0;
239  array->last_used = 0;
240 
241  // if no items need to be pre-allocated, leave
242  if(items == 0) {
243  retval = 0;
244  goto exit;
245  }
246 
247  // allocate the buffer
248  amxc_array_realloc(array, items);
249  when_null(array->buffer, exit);
250 
251  // set the allocated size
252  array->items = items;
254 
255  retval = 0;
256 
257 exit:
258  return retval;
259 }

◆ amxc_array_is_empty()

bool amxc_array_is_empty ( const amxc_array_t *const  array)

Checks that the array is empty.

An array is empty if none of the items are used.

Parameters
arraya pointer to the array structure
Returns
returns true when the array contains no items that are used, false when there is at least one item used in the array.

Definition at line 399 of file amxc_array.c.

399  {
400  bool retval = true;
401  when_null(array, exit);
402 
403  if(array->last_used != 0) {
404  retval = false;
405  }
406 
407  if((array->buffer != NULL) && (array->buffer[0].data != NULL)) {
408  retval = false;
409  }
410 
411 exit:
412  return retval;
413 }

◆ amxc_array_new()

int8_t amxc_array_new ( amxc_array_t **  array,
const size_t  items 
)

Allocates an array.

Allocates and initializes memory to store an array. This function allocates memory from the heap, if an array is on the stack, it can be initialized using the function amxc_array_init

The size of the array is not fixed and can be changed with the functions amxc_array_grow or amxc_array_shrink

The size of the array is expressed in number of items that can be stored in the array. All items in the allocated array are set to NULL.

Note
The allocated memory must be freed when not used anymore, use amxc_array_delete to free the memory
Parameters
arraya pointer to the location where the pointer to the new array can be stored
itemsthe size of the array in number of items
Returns
-1 if an error occurred. 0 on success

Definition at line 179 of file amxc_array.c.

179  {
180  int8_t retval = -1;
181  when_null(array, exit);
182 
183  /* allocate the array structure */
184  *array = (amxc_array_t*) calloc(1, sizeof(amxc_array_t));
185  when_null(*array, exit);
186 
187  /* set the number of items in the array */
188  (*array)->items = items;
189  (*array)->first_used = 0;
190  (*array)->last_used = 0;
191 
192  /* if no items need to be pre-allocated, leave */
193  if(items == 0) {
194  retval = 0;
195  goto exit;
196  }
197 
198  /* allocate the buffer */
199  amxc_array_realloc(*array, items);
200  if((*array)->buffer == NULL) {
201  free(*array);
202  *array = NULL;
203  goto exit;
204  }
206 
207  retval = 0;
208 
209 exit:
210  return retval;
211 }
The array structure.
Definition: amxc_array.h:162

◆ amxc_array_prepend_data()

amxc_array_it_t* amxc_array_prepend_data ( amxc_array_t *const  array,
void *  data 
)

Adds an item before the first used item in the array.

The array grows if the item can not be added to the array.

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
arraya pointer to the array structure
dataa pointer to the data that is added to the array
Returns
returns the array iterator where the data is added, or NULL when adding the item fails.

Definition at line 449 of file amxc_array.c.

449  {
450  amxc_array_it_t* it = NULL;
451  size_t index = 0;
452  bool grow = false;
453  when_null(array, exit);
454  when_null(data, exit);
455 
456  grow = ((!amxc_array_is_empty(array) && array->first_used == 0) ||
457  (array->buffer == NULL));
458 
459  if(grow) {
462  }
463 
464  if(!amxc_array_is_empty(array)) {
465  index = array->first_used - 1;
466  }
467 
468  it = amxc_array_set_data_at(array, index, data);
469 
470 exit:
471  return it;
472 }
int amxc_array_shift_right(amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
Shift all items to the right in the array.
Definition: amxc_array.c:323

◆ amxc_array_set_data_at()

amxc_array_it_t* amxc_array_set_data_at ( amxc_array_t *const  array,
const unsigned int  index,
void *  data 
)

Sets data at the given index.

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
arraya pointer to the array structure
indexposition in the array where the data has to be set
datapointer to the data that has to be set in the array
Returns
returns the array iterator for the given index or NULL if the index is out of boundary.

Definition at line 474 of file amxc_array.c.

476  {
478  when_null(it, exit);
479 
480  if(data != NULL) {
482  array->last_used = index;
483  array->first_used = index;
484  } else {
485  array->last_used = (array->last_used < index) ? index : array->last_used;
486  array->first_used = (array->first_used > index) ? index : array->first_used;
487  }
488  it->data = data;
489  } else {
490  void* tmp = it->data;
491  it->data = data;
492  if((tmp != NULL) && (index == array->last_used)) {
493  array->last_used = amxc_array_calculate_last_used(array, array->last_used);
494  }
495  if((tmp != NULL) && (index == array->first_used)) {
496  array->first_used = amxc_array_calculate_first_used(array, array->first_used);
497  }
498  }
499 
500 exit:
501  return it;
502 }
static size_t amxc_array_calculate_first_used(amxc_array_t *array, const size_t start)
Definition: amxc_array.c:126
static size_t amxc_array_calculate_last_used(amxc_array_t *array, const size_t start)
Definition: amxc_array.c:116

◆ amxc_array_shift_left()

int amxc_array_shift_left ( amxc_array_t *const  array,
const size_t  items,
amxc_array_it_delete_t  func 
)

Shift all items to the left in the array.

Moves all items in the array to the left with the given number of items. Items that are moved out of the array are removed from the array. A function can be provided that is called for each used item that is removed from the array.

E.g. Shifting the array to the left by 3 items, moves the item on index 3 to index 0, the item on index 4 to index 1, ...

The items at the end of the array are reinitialized to NULL.

Parameters
arraya pointer to the array structure
itemsthe number of items that needs to be shifted
funca pointer to a function that is called to free each used item in the array, that is moved out of the array.
Returns
0 on success. -1 if an error has occured

Definition at line 361 of file amxc_array.c.

363  {
364  int retval = -1;
365  amxc_array_it_t* src = NULL;
366  amxc_array_it_t* dst = NULL;
367  size_t len = 0;
368  when_null(array, exit);
369  when_true(items > array->items, exit);
370 
371  if(!items) {
372  retval = 0;
373  goto exit;
374  }
375 
376  if(items == array->items) {
377  amxc_array_clean_items(array, 0, array->items, func);
378  retval = 0;
379  array->last_used = 0;
380  array->first_used = 0;
381  goto exit;
382  }
383 
384  src = &array->buffer[items];
385  dst = array->buffer;
386  len = (array->items - items) * sizeof(amxc_array_it_t);
387 
388  memmove(dst, src, len);
389  amxc_array_clean_items(array, array->items - items, items, func);
390  array->first_used = amxc_array_calculate_first_used(array, 0);
391  array->last_used = amxc_array_calculate_last_used(array, array->items - items);
392 
393  retval = 0;
394 
395 exit:
396  return retval;
397 }

◆ amxc_array_shift_right()

int amxc_array_shift_right ( amxc_array_t *const  array,
const size_t  items,
amxc_array_it_delete_t  func 
)

Shift all items to the right in the array.

Moves all items in the array to the right with the given number of items. Items that are moved out of the array are removed from the array. A function can be provided that is called for each used item that is removed from the array.

E.g. Shifting the array to the right by 3 items, moves the item on index 0 to index 3, the item on index 1 to index 4, ...

The items in the beginning of the array are reinitialized to NULL.

Parameters
arraya pointer to the array structure
itemsthe number of items that needs to be shifted
funca pointer to a function that is called to free each used item in the array, that is moved out of the array.
Returns
0 on success. -1 if an error has occured

Definition at line 323 of file amxc_array.c.

325  {
326  int retval = -1;
327  amxc_array_it_t* src = NULL;
328  amxc_array_it_t* dst = NULL;
329  size_t len = 0;
330  when_null(array, exit);
331  when_true(items > array->items, exit);
332 
333  if(!items) {
334  retval = 0;
335  goto exit;
336  }
337 
338  if(items == array->items) {
339  amxc_array_clean_items(array, 0, array->items, func);
340  array->last_used = 0;
341  array->first_used = 0;
342  retval = 0;
343  goto exit;
344  }
345 
346  src = array->buffer;
347  dst = &array->buffer[items];
348  len = (array->items - items) * sizeof(amxc_array_it_t);
349 
350  memmove(dst, src, len);
351  amxc_array_clean_items(array, 0, items, func);
352  array->first_used = amxc_array_calculate_first_used(array, items);
353  array->last_used = amxc_array_calculate_last_used(array, array->items - 1);
354 
355  retval = 0;
356 
357 exit:
358  return retval;
359 }

◆ amxc_array_shrink()

int amxc_array_shrink ( amxc_array_t *const  array,
const size_t  items,
amxc_array_it_delete_t  func 
)

Shrinks the array.

Shrinks the array by the given number of items. Memory is freed. If a delete function is provided it is called for each used item that is removed from the array.

Parameters
arraya pointer to the array structure
itemsthe number of items the array has to shrink
funcpointer to a function that is called to free each used item in the array
Returns
0 on success. -1 if an error has occured

Definition at line 298 of file amxc_array.c.

300  {
301  int retval = -1;
302  when_null(array, exit);
303  when_true(items > array->items, exit); // out of range
304 
305  if(items == array->items) {
306  amxc_array_clean(array, func);
307 
308  retval = 0;
309  goto exit;
310  }
311 
312  amxc_array_clean_items(array, array->items - items, items, func);
313  retval = amxc_array_realloc(array, array->items - items);
314  array->last_used = amxc_array_calculate_last_used(array, array->items - 1);
315  if(array->first_used > array->items - 1) {
316  array->first_used = 0;
317  }
318 
319 exit:
320  return retval;
321 }
void amxc_array_clean(amxc_array_t *const array, amxc_array_it_delete_t func)
Removes all items from the array.
Definition: amxc_array.c:261

◆ amxc_array_size()

size_t amxc_array_size ( const amxc_array_t *const  array)

Calculates the number of used items in the array.

Loops over all items in the array and counts the used items. The size of the array is expressed in used items.

Note
Use amxc_array_is_empty to check if an array is empty, do not use this function to check if there are used items in the array. This function is looping over the complete array and counts the used items. For large arrays this could have some noticeable performance impact.
Parameters
arraya pointer to the array structure
Returns
returns the number of used items in the array

Definition at line 415 of file amxc_array.c.

415  {
416  size_t retval = 0;
417  when_null(array, exit);
418 
419  for(size_t index = 0; index < array->items; index++) {
420  if(array->buffer[index].data != NULL) {
421  retval++;
422  }
423  }
424 
425 exit:
426  return retval;
427 }

◆ amxc_array_sort()

int amxc_array_sort ( amxc_array_t *const  array,
amxc_array_it_cmp_t  cmp 
)

Sorts the content of the array.

The smallest items according to the compare functions are at the beginning of the array, empty buckets are at the end.

Parameters
arraythe array that will be sorted
cmparray iterator content compare function
Returns
0 when the array is sorted. TODO -1 if sorting fails?

Definition at line 596 of file amxc_array.c.

596  {
597  int retval = -1;
598  size_t i = 0;
599  when_null(array, exit);
600  when_null(cmp, exit);
601 
602  if(array->last_used == 0) {
603  retval = 0;
604  goto exit;
605  }
606  i = array->last_used;
607  do {
608  i--;
609  if(array->buffer[i].data == NULL) {
610  amxc_array_it_swap(&array->buffer[i],
611  &array->buffer[array->last_used]);
612  array->last_used--;
613  }
614  } while(i != 0);
615 
616  retval = amxc_array_sort_internal(array, cmp, 0, array->last_used);
617 
618 exit:
619  return retval;
620 }
static int amxc_array_sort_internal(amxc_array_t *const array, amxc_array_it_cmp_t cmp, int32_t lo, int32_t high)
Definition: amxc_array.c:136
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.

◆ amxc_array_take_first_data()

void* amxc_array_take_first_data ( amxc_array_t *const  array)

Takes the data pointer from the first used item in the array.

Parameters
arraya pointer to the array structure
Returns
returns the data pointer of the first used item in the array or NULL if there is no used item in the array.

Definition at line 576 of file amxc_array.c.

576  {
577  void* data = NULL;
579  if(it != NULL) {
580  data = it->data;
582  }
583  return data;
584 }
unsigned int amxc_array_it_index(const amxc_array_it_t *const it)
Gets the index of the iterator in the array.
amxc_array_it_t * amxc_array_get_first(const amxc_array_t *const array)
Gets the item iterator of the first used item in the array.
Definition: amxc_array.c:517

◆ amxc_array_take_last_data()

void* amxc_array_take_last_data ( amxc_array_t *const  array)

Takes the data pointer from the last used item in the array.

Parameters
arraya pointer to the array structure
Returns
returns the data pointer of the last used item in the array or NULL if there is no used item in the array.

Definition at line 586 of file amxc_array.c.

586  {
587  void* data = NULL;
589  if(it != NULL) {
590  data = it->data;
592  }
593  return data;
594 }
amxc_array_it_t * amxc_array_get_last(const amxc_array_t *const array)
Gets the item iterator of the last used item in the array.
Definition: amxc_array.c:546