1 /*****************************************************************************/
5 /* Collection (dynamic array) */
9 /* (C) 2000-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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 void CollInsert (Collection* C, void* Item, unsigned Index)
101 /* Insert the data at the given position in the collection */
103 /* Check for invalid indices */
104 PRECONDITION (Index <= C->Count);
106 /* Grow the array if necessary */
107 if (C->Count >= C->Size) {
115 NewItems = xmalloc (C->Size * sizeof (void*));
116 memcpy (NewItems, C->Items, C->Count * sizeof (void*));
121 /* Move the existing elements if needed */
122 if (C->Count != Index) {
123 memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
127 /* Store the new item */
128 C->Items[Index] = Item;
133 int CollIndex (Collection* C, const void* Item)
134 /* Return the index of the given item in the collection. Return -1 if the
135 * item was not found in the collection.
140 for (I = 0; I < C->Count; ++I) {
141 if (Item == C->Items[I]) {
153 void CollDelete (Collection* C, unsigned Index)
154 /* Remove the item with the given index from the collection. This will not
155 * free the item itself, just the pointer. All items with higher indices
156 * will get moved to a lower position.
159 /* Check the index */
160 PRECONDITION (Index < C->Count);
162 /* Remove the item pointer */
164 memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
169 void CollDeleteItem (Collection* C, const void* Item)
170 /* Delete the item pointer from the collection. The item must be in the
171 * collection, otherwise FAIL will be called.
174 /* Get the index of the entry */
175 int Index = CollIndex (C, Item);
178 /* Delete the item with this index */
180 memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
185 void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex)
186 /* Move an item from one position in the collection to another. OldIndex
187 * is the current position of the item, NewIndex is the new index after
188 * the function has done it's work. Existing entries with indices NewIndex
189 * and up are moved one position upwards.
192 /* Get the item and remove it from the collection */
193 void* Item = CollAt (C, OldIndex);
194 CollDelete (C, OldIndex);
196 /* Correct NewIndex if needed */
197 if (NewIndex >= OldIndex) {
198 /* Position has changed with removal */
202 /* Now insert it at the new position */
203 CollInsert (C, Item, NewIndex);
208 static void QuickSort (Collection* C, int Lo, int Hi,
209 int (*Compare) (void*, const void*, const void*),
211 /* Internal recursive sort function. */
213 /* Get a pointer to the items */
214 void** Items = C->Items;
221 while (I <= J && Compare (Data, Items[Lo], Items[I]) >= 0) {
224 while (I <= J && Compare (Data, Items[Lo], Items[J]) < 0) {
229 void* Tmp = Items[I];
238 void* Tmp = Items[J];
239 Items[J] = Items[Lo];
242 if (J > (Hi + Lo) / 2) {
243 QuickSort (C, J + 1, Hi, Compare, Data);
246 QuickSort (C, Lo, J - 1, Compare, Data);
254 void CollSort (Collection* C,
255 int (*Compare) (void*, const void*, const void*),
257 /* Sort the collection using the given compare function. The data pointer is
258 * passed as *first* element to the compare function, it's not used by the
259 * sort function itself. The other two pointer passed to the Compare function
260 * are pointers to objects.
264 QuickSort (C, 0, C->Count-1, Compare, Data);