Back to index

lightning-sunbird  0.9+nobinonly
Functions
icalarray.c File Reference

An array of arbitrarily-sized elements which grows dynamically as elements are added. More...

#include <stdlib.h>
#include <string.h>
#include "icalarray.h"
#include "icalerror.h"

Go to the source code of this file.

Functions

static void icalarray_expand (icalarray *array, int space_needed)
icalarray * icalarray_new (int element_size, int increment_size)
 Constructor.
void icalarray_free (icalarray *array)
 Destructor.
void icalarray_append (icalarray *array, void *element)
voidicalarray_element_at (icalarray *array, int position)
void icalarray_remove_element_at (icalarray *array, int position)
void icalarray_sort (icalarray *array, int(*compare)(const void *, const void *))

Detailed Description

An array of arbitrarily-sized elements which grows dynamically as elements are added.

Definition in file icalarray.c.


Function Documentation

void icalarray_append ( icalarray *  array,
void element 
)

Definition at line 82 of file icalarray.c.

{
    if (array->num_elements >= array->space_allocated)
       icalarray_expand (array, 1);

    memcpy ((char *)(array->data) + ( array->num_elements * array->element_size ), element,
           array->element_size);
    array->num_elements++;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* icalarray_element_at ( icalarray *  array,
int  position 
)

Definition at line 95 of file icalarray.c.

{
    assert (position >= 0);
    assert (position < array->num_elements);

    return (char *)(array->data) + (position * array->element_size);
}

Here is the caller graph for this function:

static void icalarray_expand ( icalarray *  array,
int  space_needed 
) [static]

Definition at line 136 of file icalarray.c.

{
    int new_space_allocated;
    void *new_data;

    new_space_allocated = array->space_allocated + array->increment_size;

    if (space_needed > array->increment_size) 
       new_space_allocated += space_needed;

       /*
    new_data = realloc (array->data,
                     new_space_allocated * array->element_size);
       */
       new_data = malloc(new_space_allocated * array->element_size);
       memcpy(new_data,array->data,array->element_size*array->space_allocated);
       free(array->data);

    if (new_data) {
       array->data = new_data;
       array->space_allocated = new_space_allocated;
    } else {
       icalerror_set_errno(ICAL_ALLOCATION_ERROR);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void icalarray_free ( icalarray *  array)

Destructor.

Definition at line 73 of file icalarray.c.

{
    if (array->data)
       free (array->data);
    free (array);
}

Here is the caller graph for this function:

icalarray* icalarray_new ( int  element_size,
int  increment_size 
)

Constructor.

Definition at line 49 of file icalarray.c.

{
    icalarray *array;

    array = (icalarray*) malloc (sizeof (icalarray));
    if (!array) {
       icalerror_set_errno(ICAL_NEWFAILED_ERROR);
       return NULL;
    }

    array->element_size = element_size;
    array->increment_size = increment_size;
    array->num_elements = 0;
    array->space_allocated = 0;
    array->data = NULL;

    return array;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void icalarray_remove_element_at ( icalarray *  array,
int  position 
)

Definition at line 106 of file icalarray.c.

{
    void *dest;
    int elements_to_move;

    assert (position >= 0);
    assert (position < array->num_elements);

    dest = (char *)array->data + (position * array->element_size);
    elements_to_move = array->num_elements - position - 1;

    if (elements_to_move > 0)
       memmove (dest, (char *)dest + array->element_size,
               elements_to_move * array->element_size);

    array->num_elements--;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void icalarray_sort ( icalarray *  array,
int(*)(const void *, const void *)  compare 
)

Definition at line 127 of file icalarray.c.

{
    qsort (array->data, array->num_elements, array->element_size, compare);
}

Here is the call graph for this function:

Here is the caller graph for this function: