Azinix

array.h File Reference

Array data structures. More...

#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_tArray_DoAlloc (int, int)
 Allocate an array of number objects, each of size size.
Array_tArray_Dup (Array_t *)
 Create a duplicate copy of an array.
Array_tArray_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.


Detailed Description

Array data structures.

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 Documentation

#define array_alloc ( type,
number   )     Array_Alloc( type, number )

Definition at line 249 of file array.h.

#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.

Definition at line 246 of file array.h.

#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.

Definition at line 322 of file array.h.

#define array_delete_last ( type,
array   )     Array_DeleteLast( type, array )

Definition at line 311 of file array.h.

#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.

Definition at line 308 of file array.h.

#define array_fetch ( type,
a,
 )     Array_Fetch( type, a, i )

Definition at line 275 of file array.h.

#define Array_Fetch ( type,
a,
 )     ( * ( ( type * ) Array_FetchInternal( a, i, 0 ) ) )

Fetch an element from an array.

A runtime error occurs on an attempt to reference outside the bounds of the array. There is no type-checking that the value at the given position is actually of the type used when dereferencing the array.

Definition at line 272 of file array.h.

#define array_fetch_last ( type,
array   )     Array_FetchLast(type, array) \

Definition at line 300 of file array.h.

#define array_fetch_old ( type,
a,
 ) 

Value:

(a->tmpInteger = (i),               \
      (a->tmpInteger >= (a)->num) ? array_abort((a),1) : 0,\
      *((type *) ((a)->space + a->tmpInteger * (a)->obj_size)))

Definition at line 366 of file array.h.

#define array_fetch_p ( type,
a,
 )     Array_FetchPtr(type, a, i) \

Definition at line 284 of file array.h.

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

Definition at line 297 of file array.h.

#define Array_FetchPtr ( type,
a,
 )     ( ( type * ) Array_FetchInternal( a, i, 0 ) )

Fetch a pointer to an element in an array.

Definition at line 281 of file array.h.

#define array_insert ( type,
a,
i,
datum   )     Array_Insert( type, a, i, datum )

Definition at line 235 of file array.h.

#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.

Definition at line 232 of file array.h.

#define array_insert_last ( type,
array,
datum   )     Array_InsertLast(type, array, datum)

Definition at line 261 of file array.h.

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

Definition at line 353 of file array.h.

#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:.

array_insert(type, array, array_n(array), object)

Definition at line 258 of file array.h.

#define ARRAY_OUT_OF_MEM   -10000

Definition at line 119 of file array.h.

#define arrayForEachItem ( type,
array,
i,
data   ) 

Value:

for((i) = 0;                                                 \
      (((i) < array_n((array)))                                \
       && (((data) = array_fetch(type, (array), (i))), 1));    \
      (i)++)
Macro to iterate over the items of an array.

Definition at line 345 of file array.h.


Typedef Documentation

typedef Array_t array_t

Definition at line 49 of file array.h.


Function Documentation

int Array_Abort ( Array_t a,
int  i 
)

Helper function for aborting an array computation.

Would like to be void, except used in macros which need a return type

Definition at line 329 of file array.c.

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.

Definition at line 117 of file array.c.

array_t* Array_DoAlloc ( int  ,
int   
)

Allocate an array of number objects, each of size size.

Definition at line 38 of file array.c.

char* Array_DoData ( array_t  ) 

Return a regular C array from given array.

Definition at line 189 of file array.c.

Array_t* Array_Dup ( Array_t old  ) 

Create a duplicate copy of an array.

If memory cannot be allocated for the new array, NIL(Array_t) is returned.

Definition at line 86 of file array.c.

void Array_Free ( Array_t array  ) 

Deallocate an array.

If array is NULL nothing is done. Freeing the individual elements of the array is the responsibility of the user.

Definition at line 68 of file array.c.

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.

Definition at line 154 of file array.c.

void Array_Merge ( Array_t result,
Array_t rulesFromFsm,
Array_t rulesFromShortStringTable,
Array_t rulesFromNoContentTable 
)

Form the union of 3 Array_t.

nil arg means array empty. Entries are ints, indicating applicable rules. Assuming arrays passed in are sorted in ascending order, result is sorted in ascending order.

Definition at line 413 of file array.c.

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 );
               }

Definition at line 397 of file array.c.

int Array_Resize ( array_t ,
int   
)

Resize given array.

Definition at line 205 of file array.c.

void Array_Sort ( Array_t array,
int(*)()  compare 
)

Sort the elements of an array.

The compare function is defined as:

                int
                compare(obj1, obj2)
                char **obj1;
                char **obj2;
and should return -1 if obj1 < obj2, 0 if obj1 == obj2, or 1 if obj1 > obj2.

Definition at line 247 of file array.c.

void Array_Test (  ) 

Simple test of Array_t package.

Definition at line 519 of file array.c.

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.

Definition at line 282 of file array.c.