00001 /* 00002 ** This file is part of the C Utility Toolkit (CUTK) 00003 ** Copyright (C) 2002-2005 Chris Osgood 00004 ** http://www.cutk.org 00005 ** 00006 ** This library is free software; you can redistribute it and/or 00007 ** modify it under the terms of the GNU Lesser General Public 00008 ** License as published by the Free Software Foundation; either 00009 ** version 2.1 of the License, or (at your option) any later version. 00010 ** 00011 ** This library is distributed in the hope that it will be useful, 00012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 ** Lesser General Public License for more details. 00015 ** 00016 ** You should have received a copy of the GNU Lesser General Public 00017 ** License along with this library; if not, write to the Free Software 00018 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 ** 00020 ** Please report all bugs and problems to "bugs at cutk.org". 00021 */ 00022 00023 /* 00024 ** A fast dynamic C array implementation similar to STL vectors. The main 00025 ** goals were to create a dynamic array system that feels much like normal C 00026 ** arrays and offers extremely high performance. 00027 ** 00028 ** The API is meant to be POSIX-like. That is, lowercase function names and no 00029 ** underscores. 00030 ** 00031 ** Function reference: 00032 ** aryalloc(type) -- Allocates a new array of "type" elements. 00033 ** aryallocsize(type, size)-- Allocates a new array of "type" elements with 00034 ** "size" initial elements. 00035 ** aryappend(&array, &elem)-- Adds and copies new element to the end of the 00036 ** array. 00037 ** arycat(&dest, &src) -- Concatenates two arrays. 00038 ** aryclear(&array) -- Erases all elements in the array. This does not 00039 ** free any memory. Use reserve() to free memory. 00040 ** arycpy(&dest, &src) -- Copies an array from src to dest. 00041 ** arydup(&array) -- Duplicates an array. 00042 ** aryerase(&array, index) -- Removes element at "index." 00043 ** aryfasterase(&array, index) -- Removes element at "index." by replacing it 00044 ** with the last item in the array. 00045 ** aryfree(&array) -- Free an array. 00046 ** aryinsert(&array, elem) -- Inserts a new element at position "index." 00047 ** aryreserve(&array, max) -- Allocates memory for "max" elements but does not 00048 ** change the size of the array unless smaller. 00049 ** aryresize(&array, size) -- Resizes the array to have "size" elements. 00050 ** arysize(&array) -- Number of elements in array. 00051 ** 00052 ** Note that all functions take a pointer to the array pointer as the first 00053 ** argument. Keep in mind that the array pointer may change as memory is 00054 ** allocated or deallocated. Otherwise it behaves similar to a normal array. 00055 ** 00056 ** Example usage (array of int's): 00057 ** 00058 ** int *my_array; 00059 ** my_array = aryalloc(int); 00060 ** aryresize(&my_array, 1000); 00061 ** my_array[0] = 1234; 00062 ** my_array[999] = 1234; 00063 ** array_size = arysize(&my_array); 00064 ** ... 00065 ** aryfree(&my_array); 00066 ** 00067 ** $Id: array.h 7 2005-01-02 01:02:22Z chris $ 00068 */ 00069 00070 #ifndef _ARRAY_H 00071 #define _ARRAY_H 00072 00073 /* stdlib is needed for the size_t type */ 00074 #include <stdlib.h> 00075 00076 #ifdef __cplusplus 00077 extern "C" { 00078 #endif 00079 00080 /* Main array type (not used externally but arysize/max macros use it) */ 00081 typedef struct array_type 00082 { 00083 size_t size; /* Number of elements in array */ 00084 size_t max; /* Allocated memory (max # elements) */ 00085 size_t esize; /* Element size; sizeof(ElementType) */ 00086 char ary[]; /* Raw array data */ 00087 } array_type; 00088 00089 /* Note: aryalloc_ (with underscore) is an internal function, use aryalloc() 00090 ** instead */ 00091 void* aryalloc_(size_t esize); 00092 void* aryallocsize_(size_t esize, size_t size); 00093 00094 /* Macros for low overhead API calls */ 00095 #define arysize(x) ((*((array_type**)(void*)(x))-1)->size) 00096 #define arymax(x) ((*((array_type**)(void*)(x))-1)->max) 00097 #define aryalloc(x) aryalloc_(sizeof(x)) 00098 #define aryallocsize(x,y) aryallocsize_(sizeof(x),y) 00099 #define aryclear(x) (((*((array_type**)(void*)(x))-1)->size)=0) 00100 00101 void aryfree(void* array); 00102 int aryreserve(void* array, size_t max); 00103 int aryresize(void* array, size_t size); 00104 int aryappend(void* array, const void* element); 00105 int aryinsert(void* array, size_t index); 00106 int aryerase(void* array, size_t index); 00107 int aryfasterase(void* array, size_t index); 00108 void* arydup(const void* array); 00109 int arycpy(void* dest, const void* src); 00110 int arycat(void* dest, const void* src); 00111 00112 #ifdef __cplusplus 00113 } 00114 #endif 00115 00116 #endif /* _ARRAY_H */