libamxc  1.10.3
C Generic Data Containers
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
amxc_array.c File Reference

Ambiorix array API implementation. More...

#include <stdlib.h>
#include <string.h>
#include <amxc/amxc_array.h>
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Macros

#define AMXC_ARRAY_AUTO_GROW_ITEMS   3
 
#define AMXC_ARRAY_SIZE(arr)   (sizeof(arr) / sizeof((arr)[0]))
 

Functions

static void amxc_array_initialize_items (amxc_array_t *array, const size_t start_pos)
 
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)
 
static int amxc_array_realloc (amxc_array_t *array, const size_t items)
 
static size_t amxc_array_calculate_last_used (amxc_array_t *array, const size_t start)
 
static size_t amxc_array_calculate_first_used (amxc_array_t *array, const size_t start)
 
static int amxc_array_sort_internal (amxc_array_t *const array, amxc_array_it_cmp_t cmp, int32_t lo, int32_t high)
 
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, 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_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_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_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...
 
int amxc_array_sort (amxc_array_t *const array, amxc_array_it_cmp_t cmp)
 Sorts the content of the array. More...
 

Detailed Description

Ambiorix array API implementation.

Definition in file amxc_array.c.

Macro Definition Documentation

◆ AMXC_ARRAY_AUTO_GROW_ITEMS

#define AMXC_ARRAY_AUTO_GROW_ITEMS   3

Definition at line 61 of file amxc_array.c.

◆ AMXC_ARRAY_SIZE

#define AMXC_ARRAY_SIZE (   arr)    (sizeof(arr) / sizeof((arr)[0]))

Definition at line 62 of file amxc_array.c.

Function Documentation

◆ amxc_array_calculate_first_used()

static size_t amxc_array_calculate_first_used ( amxc_array_t array,
const size_t  start 
)
static

Definition at line 126 of file amxc_array.c.

127  {
128  size_t index = start;
129  while(index < array->items && array->buffer[index].data == NULL) {
130  index++;
131  }
132 
133  return (index == array->items) ? 0 : index;
134 }
static unsigned int array[2006]

◆ amxc_array_calculate_last_used()

static size_t amxc_array_calculate_last_used ( amxc_array_t array,
const size_t  start 
)
static

Definition at line 116 of file amxc_array.c.

117  {
118  size_t index = start;
119  while(index > 0 && array->buffer[index].data == NULL) {
120  index--;
121  }
122 
123  return index;
124 }

◆ amxc_array_clean_items()

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 
)
static

Definition at line 80 of file amxc_array.c.

83  {
84  amxc_array_it_t* it = NULL;
85  for(unsigned int index = start_pos; index < start_pos + items; index++) {
86  it = &(array->buffer[index]);
87  if(it->data != NULL) {
88  if(func != NULL) {
89  func(it);
90  }
91  it->data = NULL;
92  }
93  }
94 
95  return;
96 }
The array iterator structure.
Definition: amxc_array.h:174
static amxc_htable_it_t it[2000]

◆ amxc_array_initialize_items()

static void amxc_array_initialize_items ( amxc_array_t array,
const size_t  start_pos 
)
static

Definition at line 70 of file amxc_array.c.

70  {
71  amxc_array_it_t* it = NULL;
72 
73  for(size_t index = start_pos; index < array->items; index++) {
74  it = &(array->buffer[index]);
75  it->array = array;
76  it->data = NULL;
77  }
78 }

◆ amxc_array_realloc()

static int amxc_array_realloc ( amxc_array_t array,
const size_t  items 
)
static

Definition at line 98 of file amxc_array.c.

98  {
99  int retval = -1;
100  amxc_array_it_t* buffer = NULL;
101 
102  if(array->buffer != NULL) {
103  buffer = (amxc_array_it_t*) realloc(array->buffer, sizeof(amxc_array_it_t) * items);
104  } else {
105  buffer = (amxc_array_it_t*) calloc(items, sizeof(amxc_array_it_t));
106  }
107  if(buffer != NULL) {
108  array->buffer = buffer;
109  array->items = items;
110  retval = 0;
111  }
112 
113  return retval;
114 }

◆ amxc_array_sort_internal()

static int amxc_array_sort_internal ( amxc_array_t *const  array,
amxc_array_it_cmp_t  cmp,
int32_t  lo,
int32_t  high 
)
static

Definition at line 136 of file amxc_array.c.

139  {
140  int retval = 0;
141  int32_t i = lo + 1;
142  int32_t j = high;
143 
144  while(i <= j) {
145  while(i <= high &&
146  cmp(&array->buffer[i], &array->buffer[lo]) <= 0 &&
147  i <= j) {
148  i++;
149  }
150  while(j >= lo &&
151  cmp(&array->buffer[j], &array->buffer[lo]) > 0 &&
152  i <= j) {
153  j--;
154  }
155  if(i <= j) {
156  amxc_array_it_swap(&array->buffer[i], &array->buffer[j]);
157  }
158  }
159  amxc_array_it_swap(&array->buffer[lo], &array->buffer[j]);
160 
161  if(j - 1 - lo > 1) {
162  amxc_array_sort_internal(array, cmp, lo, j - 1);
163  } else {
164  if(cmp(&array->buffer[lo], &array->buffer[lo + 1]) > 0) {
165  amxc_array_it_swap(&array->buffer[lo], &array->buffer[lo + 1]);
166  }
167  }
168  if(high - (j + 1) > 1) {
169  amxc_array_sort_internal(array, cmp, j + 1, high);
170  } else {
171  if(cmp(&array->buffer[high - 1], &array->buffer[high]) > 0) {
172  amxc_array_it_swap(&array->buffer[high - 1], &array->buffer[high]);
173  }
174  }
175 
176  return retval;
177 }
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.