Dynamic Array


Defines

#define DARRAY(name, type)
 Declare a DARRAY storage type.
#define DARRAY_DEFINE(name, type)
#define DARRAY_CLEAN(da)   do { (da)->num = 0; } while(0)
#define DARRAY_INIT(da)   do { (da)->num = (da)->max = 0; (da)->ds = NULL; }
#define DARRAY_DESTROY(da)   do { if((da)->ds) free((da)->ds); } while(0)

Detailed Description

DARRAY is a dynamic sized array/list, it's length is a variable. It is extended, automatically, if it is full and more elemnts are putted in.

Users of DARRAY must declare a new type to store data. The way to declear a new type is to invoke DARRAY() with paramters of name of type and type of data to be stored in. The new storage type is named with foo_t where foo is the name you pass in.

DARRAY_DEFINE() is inovked to define foo_add() function; foo is name of storage type. You can call foo_add() to add a data element into a storage object.

Get ith element in a storage object, use

 obj->ds[i]

To loop over elements in a storage object, us

 for(i = 0; i < obj->num; i++) {
        v = obj->ds[i];
        ......
 }

Define Documentation

#define DARRAY ( name,
type   ) 

Value:

struct _ ## name {                                      \
        int max, num;                                   \
        type *ds;                                       \
    };                                                  \
    typedef struct _ ## name name ## _t
Declare a DARRAY storage type.

Parameters:
name is name of storage type.
type is type of data elements that will be stored in.
Type of <name>_t is defined by the macro. It is used to define a storage object to contain data elements.

Definition at line 102 of file mb_tools.h.

#define DARRAY_CLEAN ( da   )     do { (da)->num = 0; } while(0)

Definition at line 123 of file mb_tools.h.

#define DARRAY_DEFINE ( name,
type   ) 

Value:

static int name ## _add(name ## _t *da, type v) {       \
        type *new_ds;                                   \
        int max;                                        \
        if(da->num >= (da)->max) {                      \
            max = (da)->max + 32;                       \
            new_ds = realloc(da->ds,                    \
                             max * sizeof(type));       \
            if(new_ds == NULL) return -1;               \
            da->ds = new_ds;                            \
            da->max = max;                              \
        }                                               \
        da->ds[da->num++] = v;                          \
        return 0;                                       \
    }

Definition at line 108 of file mb_tools.h.

#define DARRAY_DESTROY ( da   )     do { if((da)->ds) free((da)->ds); } while(0)

Definition at line 125 of file mb_tools.h.

#define DARRAY_INIT ( da   )     do { (da)->num = (da)->max = 0; (da)->ds = NULL; }

Definition at line 124 of file mb_tools.h.

SourceForge.net Logo