1 /*****************************************************************************/
5 /* Collection (dynamic array) */
9 /* (C) 2000-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 Collection* InitCollection (Collection* C)
55 /* Initialize a collection and return it. */
57 /* Intialize the fields. */
62 /* Return the new struct */
68 void DoneCollection (Collection* C)
69 /* Free the data for a collection. This will not free the data contained in
73 /* Free the pointer array */
79 Collection* NewCollection (void)
80 /* Create and return a new collection with the given initial size */
82 /* Allocate memory, intialize the collection and return it */
83 return InitCollection (xmalloc (sizeof (Collection)));
88 void FreeCollection (Collection* C)
89 /* Free a collection */
94 /* Free the structure itself */
100 unsigned CollCount (const Collection* C)
101 /* Return the number of items in the collection */
108 void CollInsert (Collection* C, void* Item, unsigned Index)
109 /* Insert the data at the given position in the collection */
111 /* Check for invalid indices */
112 PRECONDITION (Index <= C->Count);
114 /* Grow the array if necessary */
115 if (C->Count >= C->Size) {
123 NewItems = xmalloc (C->Size * sizeof (void*));
124 memcpy (NewItems, C->Items, C->Count * sizeof (void*));
129 /* Move the existing elements if needed */
130 if (C->Count != Index) {
131 memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
135 /* Store the new item */
136 C->Items[Index] = Item;
141 void CollAppend (Collection* C, void* Item)
142 /* Append an item to the end of the collection */
144 /* Insert the item at the end of the current list */
145 CollInsert (C, Item, C->Count);
150 void* CollAt (Collection* C, unsigned Index)
151 /* Return the item at the given index */
153 /* Check the index */
154 PRECONDITION (Index < C->Count);
156 /* Return the element */
157 return C->Items[Index];
162 const void* CollConstAt (const Collection* C, unsigned Index)
163 /* Return the item at the given index */
165 /* Check the index */
166 PRECONDITION (Index < C->Count);
168 /* Return the element */
169 return C->Items[Index];
174 void* CollLast (Collection* C)
175 /* Return the last item in a collection */
177 /* We must have at least one entry */
178 PRECONDITION (C->Count > 0);
180 /* Return the element */
181 return C->Items[C->Count-1];
186 const void* CollConstLast (const Collection* C)
187 /* Return the last item in a collection */
189 /* We must have at least one entry */
190 PRECONDITION (C->Count > 0);
192 /* Return the element */
193 return C->Items[C->Count-1];
198 void CollDelete (Collection* C, unsigned Index)
199 /* Remove the item with the given index from the collection. This will not
200 * free the item itself, just the pointer. All items with higher indices
201 * will get moved to a lower position.
204 /* Check the index */
205 PRECONDITION (Index < C->Count);
207 /* Remove the item pointer */
209 memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
214 void CollDeleteAll (Collection* C)
215 /* Delete all items from the given collection. This will not free the items
216 * itself, it will only remove the pointers.
219 /* This one is easy... */
225 void CollReplace (Collection* C, void* Item, unsigned Index)
226 /* Replace the item at the given position. The old item will not be freed,
227 * just the pointer will et replaced.
230 /* Check the index */
231 PRECONDITION (Index < C->Count);
233 /* Replace the item pointer */
234 C->Items[Index] = Item;
239 static void QuickSort (Collection* C, int Lo, int Hi,
240 int (*Compare) (void*, const void*, const void*),
242 /* Internal recursive sort function. */
244 /* Get a pointer to the items */
245 void** Items = C->Items;
252 while (I <= J && Compare (Data, Items[Lo], Items[I]) >= 0) {
255 while (I <= J && Compare (Data, Items[Lo], Items[J]) < 0) {
260 void* Tmp = Items[I];
269 void* Tmp = Items[J];
270 Items[J] = Items[Lo];
273 if (J > (Hi + Lo) / 2) {
274 QuickSort (C, J + 1, Hi, Compare, Data);
277 QuickSort (C, Lo, J - 1, Compare, Data);
285 void CollSort (Collection* C,
286 int (*Compare) (void*, const void*, const void*),
288 /* Sort the collection using the given compare function. The data pointer is
289 * passed as *first* element to the compare function, it's not used by the
290 * sort function itself. The other two pointer passed to the Compare function
291 * are pointers to objects.
295 QuickSort (C, 0, C->Count-1, Compare, Data);