#include "assert.h"
Go to the source code of this file.
Data Structures | |
| struct | Array_t |
| Array structure. More... | |
Defines | |
| #define | ARRAY_OUT_OF_MEM -10000 |
| #define | Array_Insert(type, a, i, datum) { type _array_datum_dup = datum; Array_InsertInternal( sizeof( type ), (a), (i) , & _array_datum_dup );} |
| Insert a new element into an array at the given position. | |
| #define | array_insert(type, a, i, datum) Array_Insert( type, a, i, datum ) |
| #define | Array_Alloc(type, number) Array_DoAlloc( sizeof(type), number) |
| Allocate and initialize an array of objects of type `type'. | |
| #define | array_alloc(type, number) Array_Alloc( type, number ) |
| #define | Array_InsertLast(type, array, datum) array_insert(type, array, (array)->num, datum) |
| Insert a new element at the end of the array. Equivalent to:. | |
| #define | array_insert_last(type, array, datum) Array_InsertLast(type, array, datum) |
| #define | Array_Fetch(type, a, i) ( * ( ( type * ) Array_FetchInternal( a, i, 0 ) ) ) |
| Fetch an element from an array. | |
| #define | array_fetch(type, a, i) Array_Fetch( type, a, i ) |
| #define | Array_FetchPtr(type, a, i) ( ( type * ) Array_FetchInternal( a, i, 0 ) ) |
| Fetch a pointer to an element in an array. | |
| #define | array_fetch_p(type, a, i) Array_FetchPtr(type, a, i) \ |
| #define | Array_FetchLast(type, array) Array_Fetch(type, array, ((array)->num)-1) |
| Fetch the last element from an array. | |
| #define | array_fetch_last(type, array) Array_FetchLast(type, array) \ |
| #define | Array_DeleteLast(type, array) ( * ( ( type * ) Array_FetchInternal( array, ((array)->num)-1, 1 ) ) ) |
| Fetch the last element of the array and delete it from the array. | |
| #define | array_delete_last(type, array) Array_DeleteLast( type, array ) |
| #define | Array_Data(type, array) (type *) Array_DoData(array) |
| Returns a normal `C' array from an array_t structure. | |
| #define | arrayForEachItem(type,array, i,data) |
| Macro to iterate over the items of an array. | |
| #define | array_insert_old(type, a, i, datum) |
| #define | array_fetch_old(type, a, i) |
Typedefs | |
| typedef Array_t | array_t |
Functions | |
| void | Array_Test () |
| Simple test of Array_t package. | |
| array_t * | Array_DoAlloc (int, int) |
| Allocate an array of number objects, each of size size. | |
| Array_t * | Array_Dup (Array_t *) |
| Create a duplicate copy of an array. | |
| Array_t * | Array_Join (Array_t *, Array_t *) |
Returns a new array which consists of the elements from array1 followed by the elements of array2. If the operation is not successful NIL(Array_t) is returned. | |
| void | Array_Free (Array_t *) |
| Deallocate an array. | |
| void | Array_Reset (Array_t *) |
| Set the array->num field to 0. This means that insert_last will happen from the beginning. array->n_size, which is the actual number of allocated bytes is unchanged. | |
| int | Array_Append (array_t *, array_t *) |
Appends the elements of array2 to the end of array1. Returns 1 if the operation is successful otherwise returns ARRAY_OUT_OF_MEM. | |
| void | Array_Sort (array_t *, int(*)()) |
| Sort the elements of an array. | |
| void | Array_Uniq (array_t *, int(*)(), void(*)()) |
| Compare adjacent elements of the array, and delete any duplicates. | |
| int | Array_Abort (array_t *, int) |
| Helper function for aborting an array computation. | |
| int | Array_Resize (array_t *, int) |
| Resize given array. | |
| char * | Array_DoData (array_t *) |
| Return a regular C array from given array. | |
| void | Array_Merge (array_t *, array_t *, array_t *, array_t *) |
| Form the union of 3 Array_t. | |
An array_t is a dynamically allocated array. As elements are inserted the array is automatically grown to accomodate the new elements.
The first element of the array is always element 0, and the last element is element n-1 (if the array contains n elements).
This array package is intended for generic objects (i.e., an array of int, array of char, array of double, array of struct foo *, or even array of struct foo).
Be careful when creating an array with holes (i.e., when there is no object stored at a particular position). An attempt to read such a position will return a 'zero' object. It is poor practice to assume that this value will be properly interpreted as, for example, (double) 0.0 or (char *) 0.
In the definitions below, 'typeof' indicates that the argument to the 'function' is a C data type; these 'functions' are actually implemented as macros.
Definition in file array.h.
| #define array_alloc | ( | type, | |||
| number | ) | Array_Alloc( type, number ) |
| #define Array_Alloc | ( | type, | |||
| number | ) | Array_DoAlloc( sizeof(type), number) |
Allocate and initialize an array of objects of type `type'.
Polymorphic arrays are okay as long as the type of largest object is used for initialization. The array can initially hold `number' objects. Typical use sets `number' to 0, and allows the array to grow dynamically.
| #define Array_Data | ( | type, | |||
| array | ) | (type *) Array_DoData(array) |
Returns a normal `C' array from an array_t structure.
This is sometimes necessary when dealing with functions which do not understand the array_t data type. A copy of the array is returned, and it is the users responsibility to free it. array_n() can be used to get the number of elements in the array.
| #define array_delete_last | ( | type, | |||
| array | ) | Array_DeleteLast( type, array ) |
| #define Array_DeleteLast | ( | type, | |||
| array | ) | ( * ( ( type * ) Array_FetchInternal( array, ((array)->num)-1, 1 ) ) ) |
| #define array_fetch | ( | type, | |||
| a, | |||||
| i | ) | Array_Fetch( type, a, i ) |
| #define Array_Fetch | ( | type, | |||
| a, | |||||
| i | ) | ( * ( ( type * ) Array_FetchInternal( a, i, 0 ) ) ) |
| #define array_fetch_last | ( | type, | |||
| array | ) | Array_FetchLast(type, array) \ |
| #define array_fetch_old | ( | type, | |||
| a, | |||||
| i | ) |
| #define array_fetch_p | ( | type, | |||
| a, | |||||
| i | ) | Array_FetchPtr(type, a, i) \ |
| #define Array_FetchLast | ( | type, | |||
| array | ) | Array_Fetch(type, array, ((array)->num)-1) |
Fetch the last element from an array.
A runtime error occurs if there are no elements in the array. There is no type-checking that the value at the given position is actually of the type used when dereferencing the array. Equivalent to:
array_fetch(type, array, array_n(array))
| #define Array_FetchPtr | ( | type, | |||
| a, | |||||
| i | ) | ( ( type * ) Array_FetchInternal( a, i, 0 ) ) |
| #define array_insert | ( | type, | |||
| a, | |||||
| i, | |||||
| datum | ) | Array_Insert( type, a, i, datum ) |
| #define Array_Insert | ( | type, | |||
| a, | |||||
| i, | |||||
| datum | ) | { type _array_datum_dup = datum; Array_InsertInternal( sizeof( type ), (a), (i) , & _array_datum_dup );} |
Insert a new element into an array at the given position.
The array is dynamically extended if necessary to accomodate the new item. It is a serious error if sizeof(type) is not the same as the type used when the array was allocated. It is also a serious error for 'position' to be less than zero.
| #define array_insert_last | ( | type, | |||
| array, | |||||
| datum | ) | Array_InsertLast(type, array, datum) |
| #define array_insert_old | ( | type, | |||
| a, | |||||
| i, | |||||
| datum | ) |
Value:
( -(a)->index != sizeof(type) ? array_abort((a),4) : 0,\ (a)->index = (i),\ (a)->index < 0 ? array_abort((a),0) : 0,\ (a)->index >= (a)->n_size ?\ a->tmpInteger = array_resize(a, (a)->index + 1) : 0,\ a->tmpInteger != ARRAY_OUT_OF_MEM ?\ *((type *) ((a)->space + (a)->index * (a)->obj_size)) = datum : datum,\ a->tmpInteger != ARRAY_OUT_OF_MEM ?\ ((a)->index >= (a)->num ? (a)->num = (a)->index + 1 : 0) : 0,\ a->tmpInteger != ARRAY_OUT_OF_MEM ?\ ((a)->index = -(int)sizeof(type)) : ARRAY_OUT_OF_MEM )
| #define Array_InsertLast | ( | type, | |||
| array, | |||||
| datum | ) | array_insert(type, array, (array)->num, datum) |
| #define arrayForEachItem | ( | type, | |||
| array, | |||||
| i, | |||||
| data | ) |
Value:
for((i) = 0; \ (((i) < array_n((array))) \ && (((data) = array_fetch(type, (array), (i))), 1)); \ (i)++)
type, type of object stored in array array, array to iterate i, int, local variable for iterator data object of type
| int Array_Abort | ( | Array_t * | a, | |
| int | i | |||
| ) |
| array_t* Array_DoAlloc | ( | int | , | |
| int | ||||
| ) |
| char* Array_DoData | ( | array_t * | ) |
Create a duplicate copy of an array.
If memory cannot be allocated for the new array, NIL(Array_t) is returned.
| void Array_Free | ( | Array_t * | array | ) |
Returns a new array which consists of the elements from array1 followed by the elements of array2. If the operation is not successful NIL(Array_t) is returned.
| void Array_Reset | ( | Array_t * | a | ) |
Set the array->num field to 0. This means that insert_last will happen from the beginning. array->n_size, which is the actual number of allocated bytes is unchanged.
This function exists simply to allow us to reuse an array. For example,
Array_t *foo;
foo = Array_Alloc( int, 8 );
for ( i = 0 ; i < 1024; i++ ) {
// do something with foo
// now we want to use foo again after we loop,
// so call
Array_Reset( foo );
}
| void Array_Sort | ( | Array_t * | array, | |
| int(*)() | compare | |||
| ) |
| void Array_Uniq | ( | Array_t * | array, | |
| int(*)() | compare, | |||
| void(*)() | free_func | |||
| ) |
Compare adjacent elements of the array, and delete any duplicates.
Usually the array should be sorted (using Array_Sort) before calling Array_Uniq. `compare' is defined as:
int
compare(obj1, obj2)
char *obj1;
char *obj2;
and returns -1 if obj1 < obj2, 0 if obj1 == obj2, or 1 if obj1 > obj2. free_func (if non-null) is defined as:
void
free_func(obj1)
char *obj1;
and frees the given array element.