00001 /*--------------------------------------------------------------------------------- 00002 00003 A simple vector like dynamic data structure 00004 00005 Copyright (C) 2005 00006 Jason Rogers (dovoto) 00007 00008 This software is provided 'as-is', without any express or implied 00009 warranty. In no event will the authors be held liable for any 00010 damages arising from the use of this software. 00011 00012 Permission is granted to anyone to use this software for any 00013 purpose, including commercial applications, and to alter it and 00014 redistribute it freely, subject to the following restrictions: 00015 00016 1. The origin of this software must not be misrepresented; you 00017 must not claim that you wrote the original software. If you use 00018 this software in a product, an acknowledgment in the product 00019 documentation would be appreciated but is not required. 00020 2. Altered source versions must be plainly marked as such, and 00021 must not be misrepresented as being the original software. 00022 3. This notice may not be removed or altered from any source 00023 distribution. 00024 00025 00026 ---------------------------------------------------------------------------------*/ 00031 #ifndef __DYNAMICARRAY_H__ 00032 #define __DYNAMICARRAY_H__ 00033 00034 #include <stdlib.h> 00035 #include <string.h> 00036 00040 typedef struct DynamicArray 00041 { 00042 void** data; 00043 int cur_size; 00044 }DynamicArray; 00045 00046 00047 static inline 00054 void* DynamicArrayInit(DynamicArray* v, int initialSize) 00055 { 00056 v->cur_size = initialSize; 00057 v->data = (void**)malloc(sizeof(void*) * initialSize); 00058 00059 return v->data; 00060 } 00061 00062 static inline 00067 void DynamicArrayDelete(DynamicArray* v) 00068 { 00069 if(v->data) free(v->data); 00070 } 00071 00072 static inline 00079 void* DynamicArrayGet(DynamicArray* v, int index) 00080 { 00081 if(index >= v->cur_size) 00082 { 00083 return NULL; 00084 } 00085 00086 return v->data[index]; 00087 } 00088 00089 static inline 00096 void DynamicArraySet(DynamicArray *v, int index, void* item) 00097 { 00098 if(index >= v->cur_size) { 00099 v->data = (void**)realloc(v->data, sizeof(void*) * v->cur_size * 2); 00100 memset(v->data + v->cur_size, 0, sizeof(void*) * v->cur_size); 00101 v->cur_size *= 2; 00102 } 00103 00104 v->data[index] = item; 00105 } 00106 00107 #endif