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 # define CollAppend(C, Item) CollInsert (C, Item, (C)->Count)
117 #if defined(HAVE_INLINE)
118 INLINE void* CollAt (Collection* C, unsigned Index)
119 /* Return the item at the given index */
121 /* Check the index */
122 PRECONDITION (Index < C->Count);
124 /* Return the element */
125 return C->Items[Index];
128 # define CollAt(C, Index) \
129 (PRECONDITION ((Index) < (C)->Count), \
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 # define CollConstAt(C, Index) \
156 (PRECONDITION ((Index) < (C)->Count), \
160 #if defined(HAVE_INLINE)
161 INLINE void* CollLast (Collection* C)
162 /* Return the last item in a collection */
164 /* We must have at least one entry */
165 PRECONDITION (C->Count > 0);
167 /* Return the element */
168 return C->Items[C->Count-1];
171 # define CollLast(C) \
172 (PRECONDITION ((C)->Count > 0), \
173 (C)->Items[(C)->Count-1])
176 #if defined(HAVE_INLINE)
177 INLINE const void* CollConstLast (const Collection* C)
178 /* Return the last item in a collection */
180 /* We must have at least one entry */
181 PRECONDITION (C->Count > 0);
183 /* Return the element */
184 return C->Items[C->Count-1];
187 # define CollConstLast(C) \
188 (PRECONDITION ((C)->Count > 0), \
189 (C)->Items[(C)->Count-1])
192 #if defined(HAVE_INLINE)
193 INLINE void* CollPop (Collection* C)
194 /* Remove the last segment from the stack and return it. Calls FAIL if the
195 * collection is empty.
198 /* We must have at least one entry */
199 PRECONDITION (C->Count > 0);
201 /* Return the element */
202 return C->Items[--C->Count];
205 # define CollPop(C) \
206 (PRECONDITION ((C)->Count > 0), \
207 (C)->Items[--(C)->Count])
210 int CollIndex (Collection* C, const void* Item);
211 /* Return the index of the given item in the collection. Return -1 if the
212 * item was not found in the collection.
215 void CollDelete (Collection* C, unsigned Index);
216 /* Remove the item with the given index from the collection. This will not
217 * free the item itself, just the pointer. All items with higher indices
218 * will get moved to a lower position.
221 void CollDeleteItem (Collection* C, const void* Item);
222 /* Delete the item pointer from the collection. The item must be in the
223 * collection, otherwise FAIL will be called.
226 #if defined(HAVE_INLINE)
227 INLINE void CollDeleteAll (Collection* C)
228 /* Delete all items from the given collection. This will not free the items
229 * itself, it will only remove the pointers.
232 /* This one is easy... */
236 # define CollDeleteAll(C) ((C)->Count = 0)
239 #if defined(HAVE_INLINE)
240 INLINE void CollReplace (Collection* C, void* Item, unsigned Index)
241 /* Replace the item at the given position. The old item will not be freed,
242 * just the pointer will et replaced.
245 /* Check the index */
246 PRECONDITION (Index < C->Count);
248 /* Replace the item pointer */
249 C->Items[Index] = Item;
252 # define CollReplace(C, Item, Index) \
253 (PRECONDITION ((Index) < (C)->Count), \
254 (C)->Items[(Index)] = (Item))
257 void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex);
258 /* Move an item from one position in the collection to another. OldIndex
259 * is the current position of the item, NewIndex is the new index after
260 * the function has done it's work. Existing entries with indices NewIndex
261 * and up are moved one position upwards.
264 void CollMoveMultiple (Collection* C, unsigned Start, unsigned Count, unsigned Target);
265 /* Move a range of items from one position to another. Start is the index
266 * of the first item to move, Count is the number of items and Target is
267 * the index of the target item. The item with the index Start will later
268 * have the index Target. All items with indices Target and above are moved
272 void CollSort (Collection* C,
273 int (*Compare) (void*, const void*, const void*),
275 /* Sort the collection using the given compare function. The data pointer is
276 * passed as *first* element to the compare function, it's not used by the
277 * sort function itself. The other two pointer passed to the Compare function
278 * are pointers to objects.
283 /* End of exprlist.h */