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 /* An array of pointers that grows if needed */
55 typedef struct Collection Collection;
57 unsigned Count; /* Number of items in the list */
58 unsigned Size; /* Size of allocated array */
59 void** Items; /* Array with dynamic size */
62 /* An empty collection */
63 extern const Collection EmptyCollection;
65 /* Initializer for static collections */
66 #define STATIC_COLLECTION_INITIALIZER { 0, 0, 0 }
68 /* Initializer for auto collections */
69 #define AUTO_COLLECTION_INITIALIZER EmptyCollection
73 /*****************************************************************************/
75 /*****************************************************************************/
79 Collection* InitCollection (Collection* C);
80 /* Initialize a collection and return it. */
82 void DoneCollection (Collection* C);
83 /* Free the data for a collection. This will not free the data contained in
87 Collection* NewCollection (void);
88 /* Create and return a new collection */
90 void FreeCollection (Collection* C);
91 /* Free a collection */
93 #if defined(HAVE_INLINE)
94 INLINE unsigned CollCount (const Collection* C)
95 /* Return the number of items in the collection */
100 # define CollCount(C) (C)->Count
103 void CollInsert (Collection* C, void* Item, unsigned Index);
104 /* Insert the data at the given position in the collection */
106 #if defined(HAVE_INLINE)
107 INLINE void CollAppend (Collection* C, void* Item)
108 /* Append an item to the end of the collection */
110 /* Insert the item at the end of the current list */
111 CollInsert (C, Item, C->Count);
114 void CollAppend (Collection* C, void* Item);
115 /* Append an item to the end of the collection */
118 #if defined(HAVE_INLINE)
119 INLINE void* CollAt (Collection* C, unsigned Index)
120 /* Return the item at the given index */
122 /* Check the index */
123 PRECONDITION (Index < C->Count);
125 /* Return the element */
126 return C->Items[Index];
129 void* CollAt (Collection* C, unsigned Index);
130 /* Return the item at the given index */
133 #if defined(HAVE_INLINE)
134 INLINE void* CollAtUnchecked (Collection* C, unsigned Index)
135 /* Return the item at the given index */
137 /* Return the element */
138 return C->Items[Index];
141 # define CollAtUnchecked(C, Index) ((C)->Items[(Index)])
144 #if defined(HAVE_INLINE)
145 INLINE const void* CollConstAt (const Collection* C, unsigned Index)
146 /* Return the item at the given index */
148 /* Check the index */
149 PRECONDITION (Index < C->Count);
151 /* Return the element */
152 return C->Items[Index];
155 const void* CollConstAt (const Collection* C, unsigned Index);
156 /* Return the item at the given index */
159 #if defined(HAVE_INLINE)
160 INLINE void* CollLast (Collection* C)
161 /* Return the last item in a collection */
163 /* We must have at least one entry */
164 PRECONDITION (C->Count > 0);
166 /* Return the element */
167 return C->Items[C->Count-1];
170 void* CollLast (Collection* C);
171 /* Return the last item in a collection */
174 #if defined(HAVE_INLINE)
175 INLINE const void* CollConstLast (const Collection* C)
176 /* Return the last item in a collection */
178 /* We must have at least one entry */
179 PRECONDITION (C->Count > 0);
181 /* Return the element */
182 return C->Items[C->Count-1];
185 const void* CollConstLast (const Collection* C);
186 /* Return the last item in a collection */
189 #if defined(HAVE_INLINE)
190 INLINE void* CollPop (Collection* C)
191 /* Remove the last segment from the stack and return it. Calls FAIL if the
192 * collection is empty.
195 /* We must have at least one entry */
196 PRECONDITION (C->Count > 0);
198 /* Return the element */
199 return C->Items[--C->Count];
202 void* CollPop (Collection* C);
203 /* Remove the last segment from the stack and return it. Calls FAIL if the
204 * collection is empty.
208 int CollIndex (Collection* C, const void* Item);
209 /* Return the index of the given item in the collection. Return -1 if the
210 * item was not found in the collection.
213 void CollDelete (Collection* C, unsigned Index);
214 /* Remove the item with the given index from the collection. This will not
215 * free the item itself, just the pointer. All items with higher indices
216 * will get moved to a lower position.
219 void CollDeleteItem (Collection* C, const void* Item);
220 /* Delete the item pointer from the collection. The item must be in the
221 * collection, otherwise FAIL will be called.
224 #if defined(HAVE_INLINE)
225 INLINE void CollDeleteAll (Collection* C)
226 /* Delete all items from the given collection. This will not free the items
227 * itself, it will only remove the pointers.
230 /* This one is easy... */
234 # define CollDeleteAll(C) ((C)->Count = 0)
237 #if defined(HAVE_INLINE)
238 INLINE void CollReplace (Collection* C, void* Item, unsigned Index)
239 /* Replace the item at the given position. The old item will not be freed,
240 * just the pointer will get replaced.
243 /* Check the index */
244 PRECONDITION (Index < C->Count);
246 /* Replace the item pointer */
247 C->Items[Index] = Item;
250 void CollReplace (Collection* C, void* Item, unsigned Index);
251 /* Replace the item at the given position. The old item will not be freed,
252 * just the pointer will get replaced.
256 void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex);
257 /* Move an item from one position in the collection to another. OldIndex
258 * is the current position of the item, NewIndex is the new index after
259 * the function has done it's work. Existing entries with indices NewIndex
260 * and up are moved one position upwards.
263 void CollMoveMultiple (Collection* C, unsigned Start, unsigned Count, unsigned Target);
264 /* Move a range of items from one position to another. Start is the index
265 * of the first item to move, Count is the number of items and Target is
266 * the index of the target item. The item with the index Start will later
267 * have the index Target. All items with indices Target and above are moved
271 void CollSort (Collection* C,
272 int (*Compare) (void*, const void*, const void*),
274 /* Sort the collection using the given compare function. The data pointer is
275 * passed as *first* element to the compare function, it's not used by the
276 * sort function itself. The other two pointer passed to the Compare function
277 * are pointers to objects.