diff ListOfArrays.h @ 0:bc4b3434367f

bare bones - create, add, iter
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Mon, 19 Dec 2011 13:25:30 +0100
parents
children fd441e4d0908
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ListOfArrays.h	Mon Dec 19 13:25:30 2011 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +/* 
     1.5 + * File:   ListOfArrays.h
     1.6 + * Author: Nina Engelhardt
     1.7 + *
     1.8 + * Created on December 16, 2011, 2:06 PM
     1.9 + */
    1.10 +
    1.11 +#ifndef LISTOFARRAYS_H
    1.12 +#define	LISTOFARRAYS_H
    1.13 +
    1.14 +typedef struct {
    1.15 +    ArrayFragment* next;
    1.16 +    void* data;
    1.17 +} ArrayFragment;
    1.18 +
    1.19 +typedef struct {
    1.20 +    ArrayFragment* first;
    1.21 +    ArrayFragment* last;
    1.22 +    size_t entry_size;
    1.23 +    int num_entries_per_fragment;
    1.24 +    int next_free_index;
    1.25 +} ListOfArrays;
    1.26 +
    1.27 +#define addToListOfArrays(type,value,list) do { \
    1.28 +    int offset_in_fragment = list->next_free_index % list->num_entries_per_fragment; \
    1.29 +    if(offset_in_fragment == 0){ \
    1.30 +        ArrayFragment* newBlock = (ArrayFragment*) VMS__malloc(sizeof(ArrayFragment*) + list->entry_size * list->num_entries_per_fragment); \
    1.31 +        newBlock->next == NULL; \
    1.32 +        if(list->first == NULL) {\
    1.33 +            list->first = newBlock; \
    1.34 +        } \
    1.35 +        if(list->last != NULL) { \
    1.36 +            list->last->next = newBlock; \
    1.37 +        } \
    1.38 +        list->last = newBlock; \
    1.39 +    } \
    1.40 +    (type*) typedFragment = (type*) list->last->data; \
    1.41 +    typedFragment[offset_in_fragment] = value; \
    1.42 +} while (0)
    1.43 +
    1.44 +typedef void  (*ListOfArraysFnPtr)  ( void * );  //fn has to cast void *
    1.45 +
    1.46 +#endif	/* LISTOFARRAYS_H */
    1.47 +