]> git.sur5r.net Git - cc65/blob - src/common/coll.c
465a65987a5d74644bd8ad58a0b23b88801eca51
[cc65] / src / common / coll.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  coll.c                                   */
4 /*                                                                           */
5 /*                        Collection (dynamic array)                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "check.h"
41 #include "xmalloc.h"
42
43 /* cc65 */
44 #include "coll.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Code                                    */
50 /*****************************************************************************/
51
52
53
54 Collection* InitCollection (Collection* C)
55 /* Initialize a collection and return it. */
56 {
57     /* Intialize the fields. */
58     C->Count = 0;
59     C->Size  = 0;
60     C->Items = 0;
61
62     /* Return the new struct */
63     return C;
64 }
65
66
67
68 void DoneCollection (Collection* C)
69 /* Free the data for a collection. This will not free the data contained in
70  * the collection.
71  */
72 {
73     /* Free the pointer array */
74     xfree (C->Items);
75 }
76
77
78
79 Collection* NewCollection (void)
80 /* Create and return a new collection with the given initial size */
81 {
82     /* Allocate memory, intialize the collection and return it */
83     return InitCollection (xmalloc (sizeof (Collection)));
84 }
85
86
87
88 void FreeCollection (Collection* C)
89 /* Free a collection */
90 {
91     /* Free the data */
92     DoneCollection (C);
93
94     /* Free the structure itself */
95     xfree (C);
96 }
97
98
99
100 void CollInsert (Collection* C, void* Item, unsigned Index)
101 /* Insert the data at the given position in the collection */
102 {
103     /* Check for invalid indices */
104     PRECONDITION (Index <= C->Count);
105
106     /* Grow the array if necessary */
107     if (C->Count >= C->Size) {
108         /* Must grow */
109         void** NewItems;
110         if (C->Size > 0) {
111             C->Size *= 2;
112         } else {
113             C->Size = 8;
114         }
115         NewItems = xmalloc (C->Size * sizeof (void*));
116         memcpy (NewItems, C->Items, C->Count * sizeof (void*));
117         xfree (C->Items);
118         C->Items = NewItems;
119     }
120
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*));
124     }
125     ++C->Count;
126
127     /* Store the new item */
128     C->Items[Index] = Item;
129 }
130
131
132
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.
136  */
137 {
138     /* Linear search */
139     unsigned I;
140     for (I = 0; I < C->Count; ++I) {
141         if (Item == C->Items[I]) {
142             /* Found */
143             return (int)I;
144         }
145     }
146
147     /* Not found */
148     return -1;
149 }
150
151
152
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.
157  */
158 {
159     /* Check the index */
160     PRECONDITION (Index < C->Count);
161
162     /* Remove the item pointer */
163     --C->Count;
164     memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
165 }
166
167
168
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.
172  */
173 {
174     /* Get the index of the entry */
175     int Index = CollIndex (C, Item);
176     CHECK (Index >= 0);
177
178     /* Delete the item with this index */
179     --C->Count;
180     memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
181 }
182
183
184
185 static void QuickSort (Collection* C, int Lo, int Hi,
186                        int (*Compare) (void*, const void*, const void*),
187                        void* Data)
188 /* Internal recursive sort function. */
189 {
190     /* Get a pointer to the items */
191     void** Items = C->Items;
192
193     /* Quicksort */
194     while (Hi > Lo) {
195         int I = Lo + 1;
196         int J = Hi;
197         while (I <= J) {
198             while (I <= J && Compare (Data, Items[Lo], Items[I]) >= 0) {
199                 ++I;
200             }
201             while (I <= J && Compare (Data, Items[Lo], Items[J]) < 0) {
202                 --J;
203             }
204             if (I <= J) {
205                 /* Swap I and J */
206                 void* Tmp = Items[I];
207                 Items[I]  = Items[J];
208                 Items[J]  = Tmp;
209                 ++I;
210                 --J;
211             }
212         }
213         if (J != Lo) {
214             /* Swap J and Lo */
215             void* Tmp = Items[J];
216             Items[J]  = Items[Lo];
217             Items[Lo] = Tmp;
218         }
219         if (J > (Hi + Lo) / 2) {
220             QuickSort (C, J + 1, Hi, Compare, Data);
221             Hi = J - 1;
222         } else {
223             QuickSort (C, Lo, J - 1, Compare, Data);
224             Lo = J + 1;
225         }
226     }
227 }
228
229
230
231 void CollSort (Collection* C,
232                int (*Compare) (void*, const void*, const void*),
233                void* Data)
234 /* Sort the collection using the given compare function. The data pointer is
235  * passed as *first* element to the compare function, it's not used by the
236  * sort function itself. The other two pointer passed to the Compare function
237  * are pointers to objects.
238  */
239 {
240     if (C->Count > 1) {
241         QuickSort (C, 0, C->Count-1, Compare, Data);
242     }
243 }
244
245
246