1 /* 2 3 W3C Sample Code Library libwww Dynamic Array Pointer Class 4 5 6 7 8 !Dynamic Array Pointer Class! 9 10 */ 11 12 /* 13 ** (c) COPYRIGHT MIT 1995. 14 ** Please first read the full copyright statement in the file COPYRIGH. 15 */ 16 17 /* 18 19 This module implements a flexible array of pointers. It is a general 20 utility module. An array is a structure which may be extended. These 21 routines create and append data to arrays, automatically reallocating 22 them as necessary. It is garanteed that the last entry in an array is 23 NULL 24 25 This module is implemented by HTArray.c, and 26 it is a part of the W3C 27 Sample Code Library. 28 29 */ 30 31 #ifndef HTARRAY_H 32 #define HTARRAY_H 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /* 39 40 .Private Data Structure. 41 42 This structure should not be referenced outside this module. If I find 43 out I'll make it private ;-) 44 45 */ 46 47 typedef struct { 48 int size; /* In numbers of elements */ 49 int growby; /* Allocation unit in elements */ 50 int allocated; /* Current size of *data */ 51 void ** data; /* Pointer to malloced area or 0 */ 52 } HTArray; 53 54 /* 55 56 .Create a new Array. 57 58 Create a new array and specify the number of bytes to allocate at a 59 time when the array is later extended. Arbitrary but normally a 60 trade-off time vs. memory 61 62 */ 63 64 extern HTArray * HTArray_new (int grow); 65 66 /* 67 68 .Delete an Array. 69 70 Delete an array created by HTArray_new 71 72 */ 73 74 extern BOOL HTArray_delete (HTArray * array); 75 76 /* 77 78 .Clear an Array. 79 80 Clears an array but keeps it around 81 82 */ 83 84 extern BOOL HTArray_clear (HTArray * array); 85 86 /* 87 88 .Append an element to the Array. 89 90 Add the element to the array. 91 92 */ 93 94 extern BOOL HTArray_addObject (HTArray * array, void * object); 95 96 /* 97 98 .Traverse an Array. 99 100 Fast macros to traverse a macro ending in a NULL element. 101 102 */ 103 104 #define HTArray_firstObject(me, dp) \ 105 ((me) && ((dp)=(me)->data) ? *(dp)++ : NULL) 106 #define HTArray_nextObject(me, dp) \ 107 ((me) && (dp) ? *(dp)++ : NULL) 108 109 /* 110 111 .Sort an Array. 112 113 An array can be sorted in any way you like, for example with 114 qsort(). This module provides an easy interface to the qsort() 115 function using where you can define you own comparison routine as a 116 function of the type: 117 118 */ 119 120 typedef int HTComparer (const void * a, const void * b); 121 122 /* 123 124 The sort function returns YES if sorting OK, else NO. 125 126 */ 127 128 extern BOOL HTArray_sort (HTArray * array, HTComparer * comp); 129 130 /* 131 132 .Returns Data Vector. 133 134 Returns a pointer to the actual data 135 136 */ 137 138 #define HTArray_data(me) ((me) ? (me)->data : NULL) 139 140 /* 141 142 143 .Return Current Size. 144 145 Returns the current size of the chunk 146 147 */ 148 149 #define HTArray_size(me) ((me) ? (me)->size : -1) 150 151 /* 152 153 */ 154 155 #ifdef __cplusplus 156 } 157 #endif 158 159 #endif 160 161 /* 162 163 164 165 @(#) $Id$ 166 167 168 */ 169