Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

array.c File Reference

Go to the source code of this file.

Functions

int array_init (Array *array, void *first_element, int size, int(*copy_data)(void *src, void *dest), int(*remove_data)(void *target), int(*compare)(void *d1, void *d2), void *(*index)(void *start, int idx), int(*init_data)(void *data))
 Initialize an array handler.

void array_free (Array *array)
 Free all memory allocated for the array structure.

int array_schedule_for_rem (Array *array, int idx)
 Schedule an element of the array for removing.

int compare_int (const void *int1, const void *int2)
int array_rem_all (Array *array)
 Remove a list of previouly scheduled element.

int array_init_element (Array *array)
 Initialize all elements of an array.

int array_add (Array *array, void *element)
 Add an element to the array (the element id added after the last element).

int array_rem (Array *array, int idx)
 Remove an element from an array. The elements next to the one removed are shifted to the left (so there is no gaps, and the elements' order does not change).

int array_insert (Array *array, int idx, void *data)
 Insert an element in an array.

int array_find (Array *array, void *data)
 Find an given element in the array.


Detailed Description

This file implements basic array manipulation tools.

Definition in file array.c.


Function Documentation

int array_add Array   array,
void *    element
 

Add an element to the array (the element id added after the last element).

Parameters:
array Pointer to the array handler to initialize.
element Pointer to the element to add to the array.
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Examples:
test_array.c.

Definition at line 208 of file array.c.

References s_array::array_size, s_array::copy_data, s_array::first_element, s_array::index, and s_array::number_of_elements.

int array_find Array   array,
void *    data
 

Find an given element in the array.

Parameters:
array Pointer to the array data structure.
data Pointer to the user defined element to find.
Returns:
If the element is found, then the function returns the index of the element. Otherwise the function returns -1.
Examples:
test_array.c.

Definition at line 394 of file array.c.

References s_array::compare, s_array::first_element, s_array::index, and s_array::number_of_elements.

void array_free Array   array
 

Free all memory allocated for the array structure.

Warning:
This function DOES NOT free memory allocated for the user's data (this is the responsability of the user).
Examples:
test_array.c.

Definition at line 73 of file array.c.

References s_array::to_remove.

int array_init Array   array,
void *    first_element,
int    size,
int(*    copy_data)(void *src, void *dest),
int(*    remove_data)(void *target),
int(*    compare)(void *d1, void *d2),
void *(*    index)(void *start, int idx),
int(*    init_data)(void *data)
 

Initialize an array handler.

Parameters:
array Pointer to the array handler to initialize.
first_element pointer to the first element of the array.
size Size of the array (that is: the maximum number of elements in the array).
copy_data User defined function used to copy data from/to the array. Upon successful completion the function returns 0, otherwise it returns 1. See the description of struct s_array for a complete description. WARNING: The function copy_data MUST take care off memory allocation/desallocation (if required).
remove_data User defined function used to remove an element from the array. Upon successful completion the function returns 0, otherwise it returns 1. See the description of struct s_array for a complete description. remove_data can be a NULL pointer if no special operation is required to remove an element.
compare User defined function used to compare two elements of the array. The function must return:
  • -1 if d1 < d2
  • 0 if d1 = d2
  • +1 if d1 > d2
  • -2 if an error occured.
See the description of struct s_array for a complete description.
index User defined function that returns a pointer to the element which index is idx. See the description of struct s_array for a complete description.
init_data User defined function used to initialize data. You can specify a NULL pointer if initialization is not required. See the description of struct s_array for a complete description.
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Examples:
test_array.c.

Definition at line 38 of file array.c.

References s_array::array_size, s_array::compare, s_array::copy_data, s_array::element_to_remove, s_array::first_element, s_array::index, s_array::init_data, s_array::number_of_elements, s_array::remove_data, and s_array::to_remove.

int array_init_element Array   array
 

Initialize all elements of an array.

Parameters:
array Pointer to an array data structure.
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Warning:
Depending on what you put in the function init_data(), you may call this function one or more times.
Examples:
test_array.c.

Definition at line 184 of file array.c.

References s_array::array_size, s_array::first_element, s_array::index, and s_array::init_data.

int array_insert Array   array,
int    idx,
void *    data
 

Insert an element in an array.

Parameters:
array Pointer to an array data structure.
idx Index in the array where to insert the new elememt.
data Pointer to a user defined data (that represents the content of the new element).
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Examples:
test_array.c.

Definition at line 330 of file array.c.

References s_array::array_size, s_array::copy_data, s_array::first_element, s_array::index, and s_array::number_of_elements.

int array_rem Array   array,
int    idx
 

Remove an element from an array. The elements next to the one removed are shifted to the left (so there is no gaps, and the elements' order does not change).

Parameters:
array Pointer to the array handler to initialize.
idx Index of the element to remove.
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Examples:
test_array.c.

Definition at line 257 of file array.c.

References s_array::copy_data, s_array::first_element, s_array::index, s_array::number_of_elements, and s_array::remove_data.

Referenced by array_rem_all().

int array_rem_all Array   array
 

Remove a list of previouly scheduled element.

Parameters:
array Pointer to an array data structure.
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Examples:
test_array.c.

Definition at line 140 of file array.c.

References array_rem(), s_array::element_to_remove, issort(), and s_array::to_remove.

int array_schedule_for_rem Array   array,
int    idx
 

Schedule an element of the array for removing.

Parameters:
array Pointer to an array data structure.
idx Index of the element to remove.
Returns:
Upon successful completion the function returns 0, otherwise it returns -1.
Examples:
test_array.c.

Definition at line 85 of file array.c.

References s_array::array_size, s_array::element_to_remove, and s_array::to_remove.


Generated on Thu Apr 3 16:23:44 2003 for Common_C_libraries by doxygen1.3-rc1