1 /*****************************************************************************/
5 /* Collection (dynamic array) */
9 /* (C) 2000 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 /*****************************************************************************/
47 /*****************************************************************************/
49 /*****************************************************************************/
53 Collection* InitCollection (Collection* C)
54 /* Initialize a collection and return it. */
56 /* Intialize the fields. */
59 C->Items = xmalloc (8 * sizeof (void*));
61 /* Return the new struct */
67 void DoneCollection (Collection* C)
68 /* Free the data for a collection. This will not free the data contained in
72 /* Free the pointer array */
78 Collection* NewCollection (void)
79 /* Create and return a new collection with the given initial size */
81 /* Allocate memory, intialize the collection and return it */
82 return InitCollection (xmalloc (sizeof (Collection)));
87 void FreeCollection (Collection* C)
88 /* Free a collection */
93 /* Free the structure itself */
99 unsigned CollCount (Collection* C)
100 /* Return the number of items in the collection */
107 void CollInsert (Collection* C, void* Item, unsigned Index)
108 /* Insert the data at the given position in the collection */
110 /* Check for invalid indices */
111 PRECONDITION (Index <= C->Count);
113 /* Grow the array if necessary */
114 if (C->Count >= C->Size) {
122 NewItems = xmalloc (C->Size * sizeof (void*));
123 memcpy (NewItems, C->Items, C->Count * sizeof (void*));
128 /* Move the existing elements if needed */
129 if (C->Count != Index) {
130 memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
134 /* Store the new item */
135 C->Items[Index] = Item;
140 void CollAppend (Collection* C, void* Item)
141 /* Append an item to the end of the collection */
143 /* Insert the item at the end of the current list */
144 CollInsert (C, Item, C->Count);
149 void* CollAt (Collection* C, unsigned Index)
150 /* Return the item at the given index */
152 /* Check the index */
153 PRECONDITION (Index < C->Count);
155 /* Return the element */
156 return C->Items[Index];
161 void CollDelete (Collection* C, unsigned Index)
162 /* Remove the item with the given index from the collection. This will not
163 * free the item itself, just the pointer. All items with higher indices
164 * will get moved to a lower position.
167 /* Check the index */
168 PRECONDITION (Index < C->Count);
170 /* Remove the item pointer */
172 memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
177 void CollReplace (Collection* C, void* Item, unsigned Index)
178 /* Replace the item at the given position. The old item will not be freed,
179 * just the pointer will et replaced.
182 /* Check the index */
183 PRECONDITION (Index < C->Count);
185 /* Replace the item pointer */
186 C->Items[Index] = Item;