LibRCG  3.1.1
array.c File Reference

Implementation of a dynamic array. More...

Go to the source code of this file.

Functions

Array newArray (int size)
 Creates an empty array, with the specified initial capacity. More...
 
void arrayDelete (Array array)
 Deletes an array. More...
 
int arrayInsert (Array array, int index, void *elem, int replace)
 Inserts an new element at the specified position of an array. More...
 
int arrayRemove (Array array, int index, void **elem)
 Removes the element at the specified position of an array. More...
 
int arrayAt (Array array, int index, void **elem)
 Provides the element at the specified position of an array. More...
 
int arrayResize (Array array, int size)
 Increases the capacity of an array. More...
 
int arraySize (Array array)
 Returns the size of an array. More...
 
int arrayCapacity (Array array)
 Return the capacity of an array. More...
 
int arrayMap (Array array, void(*fun)(void *))
 Applies a function to the elements of an array. More...
 
Iterator arrayIterator (Array array)
 Creates an iterator from an array. More...
 

Detailed Description

Implementation of a dynamic array.

Author
Rui Carlos Gonçalves
Version
3.0
Date
10/2011

Definition in file array.c.

Function Documentation

int arrayAt ( Array  array,
int  index,
void **  elem 
)

Provides the element at the specified position of an array.

If there is no element at the specified position, it will be put the value NULL at elem.

Attention
This function puts at elem a pointer to the element at the specified position. Changes to this element will affect the element in the array.
Parameters
arraythe array
indexthe index of the element to be provided
elempointer where the element at the specified position will be put
Returns
0 if there was an element at the specified position
1 otherwise

Definition at line 95 of file array.c.

int arrayCapacity ( Array  array)

Return the capacity of an array.

Parameters
arraythe array
Returns
the capacity of the array

Definition at line 142 of file array.c.

void arrayDelete ( Array  array)

Deletes an array.

Attention
This function only frees the memory used by the array. It does not free the memory used by elements the array contains.
Parameters
arraythe array to be deleted

Definition at line 35 of file array.c.

int arrayInsert ( Array  array,
int  index,
void *  elem,
int  replace 
)

Inserts an new element at the specified position of an array.

The position, specified by argument index, must be a non negative integer. If necessary, the capacity of the array will be increased to index+1.

If the position is already filled, the replace argument specifies whether the new element should be added (it will be added only if replace!=0).

Attention
If the new element is NULL, it will not be inserted, and the size of the array will not change.
Parameters
arraythe array
indexthe index at which the new element is to be inserted
elemthe element to be inserted
replacespecify whether old value will be replaced
Returns
0 if the new element was inserted
1 if the position was already filled
2 if the position was not valid
3 if it was not possible increase the array size

Definition at line 43 of file array.c.

Iterator arrayIterator ( Array  array)

Creates an iterator from an array.

See Also
Iterator
Parameters
arraythe array
Returns
NULL if an error occurred
the iterator otherwise

Definition at line 169 of file array.c.

int arrayMap ( Array  array,
void(*)(void *)  fun 
)

Applies a function to the elements of an array.

The function to be applied must be of type void fun(void*).

Parameters
arraythe array
funthe function to be applied
Returns
0 if the array was not empty
1 otherwise

Definition at line 149 of file array.c.

int arrayRemove ( Array  array,
int  index,
void **  elem 
)

Removes the element at the specified position of an array.

Provides the value of the removed element if the value of elem is not NULL.

Attention
This function does not free the memory used by the removed element.
Parameters
arraythe array
indexthe index of the element to be removed
elempointer where the removed element should be put (or NULL)
Returns
0 if an element was removed from the specified position
1 otherwise

Definition at line 71 of file array.c.

int arrayResize ( Array  array,
int  size 
)

Increases the capacity of an array.

The new capacity must be greater than the current one.

Parameters
arraythe array
sizethe new capacity
Returns
0 if the capacity of the array was increased
1 if it was not possible change the array capacity
2 if the new capacity was less than the current one

Definition at line 114 of file array.c.

int arraySize ( Array  array)

Returns the size of an array.

Parameters
arraythe array
Returns
the size of the array

Definition at line 135 of file array.c.

Array newArray ( int  size)

Creates an empty array, with the specified initial capacity.

The initial capacity must be a positive number.

Parameters
sizethe initial capacity of the array
Returns
NULL if an error occurred
the new array otherwise

Definition at line 12 of file array.c.

LibRCG © 2004-2015   Rui Carlos Gonçalves