1 /****************************************************************************************/
\r
4 /* Author: Jim Mischel */
\r
5 /* Description: Lightweight dynamic array. */
\r
7 /* The contents of this file are subject to the Genesis3D Public License */
\r
8 /* Version 1.01 (the "License"); you may not use this file except in */
\r
9 /* compliance with the License. You may obtain a copy of the License at */
\r
10 /* http://www.genesis3d.com */
\r
12 /* Software distributed under the License is distributed on an "AS IS" */
\r
13 /* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
\r
14 /* the License for the specific language governing rights and limitations */
\r
15 /* under the License. */
\r
17 /* The Original Code is Genesis3D, released March 25, 1999. */
\r
18 /* Genesis3D Version 1.1 released November 15, 1999 */
\r
19 /* Copyright (C) 1999 WildTangent, Inc. All Rights Reserved */
\r
21 /****************************************************************************************/
\r
25 #include "basetype.h"
\r
33 typedef struct tag_Array Array;
\r
36 int ItemsAllocated; // number of items currently allocated
\r
38 void *Items; // array of data
\r
42 // Initialize an allocated array object
\r
43 geBoolean Array_Init (Array *pArray, int InitialSize, int ItemSize);
\r
45 // Free memory allocated by an array's items
\r
46 void Array_Uninit (Array *pArray);
\r
48 // Create an array object with given initial size (possibly 0).
\r
49 // The array contains items of size ItemSize (must be > 0)
\r
50 Array *Array_Create (int InitialSize, int ItemSize);
\r
52 // Destroy an array object
\r
53 void Array_Destroy (Array **ppArray);
\r
55 // Size the array to contain NewSize items.
\r
56 // returns new size in items.
\r
57 int Array_Resize (Array *pArray, int NewSize);
\r
60 // Returns the array's current size.
\r
61 int Array_GetSize (const Array *pArray);
\r
63 // Return size of items held by the array
\r
64 int Array_GetItemSize (const Array *pArray);
\r
66 // Returns a pointer to the item's data.
\r
67 void *Array_ItemPtr (Array *pArray, int Index);
\r
69 // Copies DataSize bytes from pData to array[Index]
\r
70 void Array_PutAt (Array *pArray, int Index, void *pData, int DataSize);
\r
72 // Inserts an item at the given position, moving all other items
\r
73 // down by 1. The last item in the array is lost...
\r
74 void Array_InsertAt (Array *pArray, int Index, void *pData, int DataSize);
\r
76 // Deletes the item at and moves all following items in the array up
\r
77 // to fill in the empty spot.
\r
78 void Array_DeleteAt (Array *pArray, int Index);
\r
81 #define Array_GetSize(a) ((a)->ItemsAllocated)
\r
82 #define Array_GetItemSize(a) ((a)->ItemSize)
\r
83 #define Array_ItemPtr(a,i) ((void *)(((long)((a)->Items))+((i)*(a)->ItemSize)))
\r
84 #define Array_PutAt(a,i,d,s) (memcpy (Array_ItemPtr((a),(i)),(d),(s)))
\r
85 #define Array_InsertAt(a,i,d,s) \
\r
86 (memmove(Array_ItemPtr((a),(i)+1),Array_ItemPtr((a),(i)),((a)->ItemsAllocated-(i)-1)*(a)->ItemSize), \
\r
87 memcpy(Array_ItemPtr((a),(i)),(d),(s)))
\r
88 #define Array_DeleteAt(a,i) \
\r
89 (memcpy (Array_ItemPtr((a),(i)),Array_ItemPtr((a),(i+1)),((a)->ItemsAllocated-(i)-1)*(a)->ItemSize))
\r