comparison src/dynarr.c @ 185:3d6bfa290dbd r20-3b19

Import from CVS: tag r20-3b19
author cvs
date Mon, 13 Aug 2007 09:55:28 +0200
parents 376386a54a3c
children c5d627a313b1
comparison
equal deleted inserted replaced
184:bcd2674570bf 185:3d6bfa290dbd
42 until the next operation that changes the length of the array. 42 until the next operation that changes the length of the array.
43 43
44 This is a container object. Declare a dynamic array of a specific type 44 This is a container object. Declare a dynamic array of a specific type
45 as follows: 45 as follows:
46 46
47 struct mytype_dynarr 47 typdef struct
48 { 48 {
49 Dynarr_declare (mytype); 49 Dynarr_declare (mytype);
50 }; 50 } mytype_dynarr;
51 51
52 Use the following functions/macros: 52 Use the following functions/macros:
53 53
54 void *Dynarr_new(type) 54 void *Dynarr_new(type)
55 [MACRO] Create a new dynamic-array object, with each element of the 55 [MACRO] Create a new dynamic-array object, with each element of the
56 specified type. The return value is a void * and must be cast to the 56 specified type. The return value is cast to (type##_dynarr).
57 proper dynamic array type. 57 This requires following the convention that types are declared in
58 such a way that this type concatenation works. In particular, TYPE
59 must be a symbol, not an arbitrary C type.
58 60
59 Dynarr_add(d, el) 61 Dynarr_add(d, el)
60 [MACRO] Add an element to the end of a dynamic array. EL is a pointer 62 [MACRO] Add an element to the end of a dynamic array. EL is a pointer
61 to the element; the element itself is stored in the array, however. 63 to the element; the element itself is stored in the array, however.
62 No function call is performed unless the array needs to be resized. 64 No function call is performed unless the array needs to be resized.
74 START. The elements should be contiguous in memory, starting at BASE. 76 START. The elements should be contiguous in memory, starting at BASE.
75 77
76 int Dynarr_length(d) 78 int Dynarr_length(d)
77 [MACRO] Return the number of elements currently in a dynamic array. 79 [MACRO] Return the number of elements currently in a dynamic array.
78 80
81 int Dynarr_largest(d)
82 [MACRO] Return the maximum value that Dynarr_length(d) would
83 ever have returned.
84
79 type Dynarr_at(d, i) 85 type Dynarr_at(d, i)
80 [MACRO] Return the element at the specified index (no bounds checking 86 [MACRO] Return the element at the specified index (no bounds checking
81 done on the index). The element itself is returned, not a pointer 87 done on the index). The element itself is returned, not a pointer
82 to it. 88 to it.
83 89
106 int Dynarr_min_size = 1; 112 int Dynarr_min_size = 1;
107 113
108 void * 114 void *
109 Dynarr_newf (int elsize) 115 Dynarr_newf (int elsize)
110 { 116 {
111 Dynarr *d = (Dynarr *) xmalloc (sizeof (Dynarr)); 117 Dynarr *d = xnew_and_zero (Dynarr);
112
113 memset (d, 0, sizeof (*d));
114 d->elsize = elsize; 118 d->elsize = elsize;
115 119
116 return d; 120 return d;
117 } 121 }
118 122
227 total += malloced_storage_size (d, sizeof (*dy), stats); 231 total += malloced_storage_size (d, sizeof (*dy), stats);
228 232
229 return total; 233 return total;
230 } 234 }
231 235
232 #endif 236 #endif /* MEMORY_USAGE_STATS */