1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
*
* File: darray.c
* ---------------
* David Wright
* 10/8/98
*
* See darray.h for function descriptions
*/
#if !defined(UNDER_CE) && !defined(__KATANA__)
#include <assert.h>
#else
#define assert(a)
#endif
#include <stdlib.h>
#include <string.h>
#include "darray.h"
#ifdef _MFC_MEM_DEBUG
#define _CRTDBG_MAP_ALLOC 1
#include <crtdbg.h>
#endif
#define DEF_GROWBY 8
#ifdef _NO_NOPORT_H_
#define gsimalloc malloc
#define gsifree free
#define gsirealloc realloc
#else
#include "nonport.h" //for gsimalloc/realloc/free
#endif
// STRUCTURES
struct DArrayImplementation
{
int count, capacity;
int elemsize;
int growby;
ArrayElementFreeFn elemfreefn;
void *list; //array of elements
};
// PROTOTYPES
static void *mylsearch(const void *key, void *base, int count, int size,
ArrayCompareFn comparator);
static void *mybsearch(const void *elem, void *base, int num, int elemsize,
ArrayCompareFn comparator, int *found);
// FUNCTIONS
/* FreeElement
* Frees the element at position N in the array
*/
static void FreeElement(DArray array, int n)
{
if (array->elemfreefn != NULL)
array->elemfreefn(ArrayNth(array,n));
}
/* ArrayGrow
* Reallocates the array to a new size, incresed by growby
*/
static void ArrayGrow(DArray array)
{
array->capacity += array->growby;
array->list = gsirealloc(array->list, array->capacity * array->elemsize);
assert(array->list);
}
/* SetElement
* Sets the element at pos to the contents of elem
*/
static void SetElement(DArray array, const void *elem, int pos)
{
memcpy(ArrayNth(array,pos), elem, array->elemsize);
}
DArray ArrayNew(int elemSize, int numElemsToAllocate,
ArrayElementFreeFn elemFreeFn)
{
DArray array;
array = (DArray) gsimalloc(sizeof(struct DArrayImplementation));
assert(array);
assert(elemSize);
if (numElemsToAllocate == 0)
numElemsToAllocate = DEF_GROWBY;
array->count = 0;
array->capacity = numElemsToAllocate;;
array->elemsize = elemSize;
array->growby = numElemsToAllocate;
array->elemfreefn = elemFreeFn;
array->list = gsimalloc(array->capacity * array->elemsize);
assert(array->list);
return array;
}
void ArrayFree(DArray array)
{
int i;
assert(array);
for (i = 0; i < array->count; i++)
{
FreeElement(array, i);
}
gsifree(array->list);
gsifree(array);
}
int ArrayLength(const DArray array)
{
return array->count;
}
void *ArrayNth(DArray array, int n)
{
assert( (n >= 0) && (n < array->count));
return (char *)array->list + array->elemsize*n;
}
/* ArrayAppend
* Just do an Insert at the end of the array
*/
void ArrayAppend(DArray array, const void *newElem)
{
ArrayInsertAt(array, newElem, array->count);
}
void ArrayInsertAt(DArray array, const void *newElem, int n)
{
assert( (n >= 0) && (n <= array->count));
if (array->count == array->capacity)
ArrayGrow(array);
array->count++;
if (n < array->count - 1) //if we aren't appending
memmove(ArrayNth(array, n+1), ArrayNth(array,n),
(array->count - 1 - n) * array->elemsize);
SetElement(array, newElem, n);
}
void ArrayInsertSorted(DArray array, const void *newElem, ArrayCompareFn comparator)
{
int n;
void *res;
int found;
assert( comparator);
res=mybsearch(newElem, array->list, array->count, array->elemsize, comparator, &found);
n = (((char *)res - (char *)array->list) / array->elemsize);
ArrayInsertAt(array, newElem, n);
}
void ArrayRemoveAt(DArray array, int n)
{
assert( (n >= 0) && (n < array->count));
if (n < array->count - 1) //if not last element
memmove(ArrayNth(array,n),ArrayNth(array,n+1),
(array->count - 1 - n) * array->elemsize);
array->count--;
}
void ArrayDeleteAt(DArray array, int n)
{
assert( (n >= 0) && (n < array->count));
FreeElement(array,n);
ArrayRemoveAt(array, n);
}
void ArrayReplaceAt(DArray array, const void *newElem, int n)
{
assert( (n >= 0) && (n < array->count));
FreeElement(array, n);
SetElement(array, newElem,n);
}
void ArraySort(DArray array, ArrayCompareFn comparator)
{
qsort(array->list, array->count, array->elemsize, comparator);
}
//assert will be raised by ArrayNth if fromindex out of range
int ArraySearch(DArray array, const void *key, ArrayCompareFn comparator,
int fromIndex, int isSorted)
{
void *res;
int found = 1;
if (!array || array->count == 0)
return NOT_FOUND;
if (isSorted)
res=mybsearch(key, ArrayNth(array,fromIndex),
array->count - fromIndex, array->elemsize, comparator, &found);
else
res=mylsearch(key, ArrayNth(array, fromIndex),
array->count - fromIndex, array->elemsize, comparator);
if (res != NULL && found)
return (((char *)res - (char *)array->list) / array->elemsize);
else
return NOT_FOUND;
}
void ArrayMap(DArray array, ArrayMapFn fn, void *clientData)
{
int i;
assert(fn);
for (i = 0; i < array->count; i++)
fn(ArrayNth(array,i), clientData);
}
void ArrayMapBackwards(DArray array, ArrayMapFn fn, void *clientData)
{
int i;
assert(fn);
for (i = (array->count - 1) ; i >= 0 ; i--)
fn(ArrayNth(array,i), clientData);
}
void * ArrayMap2(DArray array, ArrayMapFn2 fn, void *clientData)
{
int i;
void * pcurr;
assert(fn);
for (i = 0; i < array->count; i++)
{
pcurr = ArrayNth(array,i);
if(!fn(pcurr, clientData))
return pcurr;
}
return NULL;
}
void * ArrayMapBackwards2(DArray array, ArrayMapFn2 fn, void *clientData)
{
int i;
void * pcurr;
assert(fn);
for (i = (array->count - 1) ; i >= 0 ; i--)
{
pcurr = ArrayNth(array,i);
if(!fn(pcurr, clientData))
return pcurr;
}
return NULL;
}
void ArrayClear(DArray array)
{
int i;
// This could be more optimal!
//////////////////////////////
for(i = (ArrayLength(array) - 1) ; i >= 0 ; i--)
ArrayDeleteAt(array, i);
}
/* mylsearch
* Implementation of a standard linear search on an array, since we
* couldn't use lfind
*/
static void *mylsearch(const void *key, void *base, int count, int size,
ArrayCompareFn comparator)
{
int i;
for (i = 0; i < count; i++)
{
if (comparator(key, (char *)base + size*i) == 0)
return (char *)base + size*i;
}
return NULL;
}
/* mybsearch
* Implementation of a bsearch, since its not available on all platforms
*/
static void *mybsearch(const void *elem, void *base, int num, int elemsize, ArrayCompareFn comparator, int *found)
{
int L, H, I, C;
L = 0;
H = num - 1;
*found = 0;
while (L <= H)
{
I = (L + H) >> 1;
C = comparator(((char *)base) + I * elemsize,elem);
if (C == 0)
*found = 1;
if (C < 0)
L = I + 1;
else
{
H = I - 1;
}
}
return ((char *)base) + L * elemsize;
}
|