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 /* Initializer for static collections */
63 #define STATIC_COLLECTION_INITIALIZER { 0, 0, 0 }
67 /*****************************************************************************/
69 /*****************************************************************************/
73 Collection* InitCollection (Collection* C);
74 /* Initialize a collection and return it. */
76 void DoneCollection (Collection* C);
77 /* Free the data for a collection. This will not free the data contained in
81 Collection* NewCollection (void);
82 /* Create and return a new collection */
84 void FreeCollection (Collection* C);
85 /* Free a collection */
87 #if defined(HAVE_INLINE)
88 INLINE unsigned CollCount (const Collection* C)
89 /* Return the number of items in the collection */
94 # define CollCount(C) (C)->Count
97 void CollInsert (Collection* C, void* Item, unsigned Index);
98 /* Insert the data at the given position in the collection */
100 #if defined(HAVE_INLINE)
101 INLINE void CollAppend (Collection* C, void* Item)
102 /* Append an item to the end of the collection */
104 /* Insert the item at the end of the current list */
105 CollInsert (C, Item, C->Count);
108 # define CollAppend(C, Item) CollInsert (C, Item, (C)->Count)
111 #if defined(HAVE_INLINE)
112 INLINE void* CollAt (Collection* C, unsigned Index)
113 /* Return the item at the given index */
115 /* Check the index */
116 PRECONDITION (Index < C->Count);
118 /* Return the element */
119 return C->Items[Index];
122 # define CollAt(C, Index) \
123 (PRECONDITION ((Index) < (C)->Count), \
127 #if defined(HAVE_INLINE)
128 INLINE void* CollAtUnchecked (Collection* C, unsigned Index)
129 /* Return the item at the given index */
131 /* Return the element */
132 return C->Items[Index];
135 # define CollAtUnchecked(C, Index) ((C)->Items[(Index)])
138 #if defined(HAVE_INLINE)
139 INLINE const void* CollConstAt (const Collection* C, unsigned Index)
140 /* Return the item at the given index */
142 /* Check the index */
143 PRECONDITION (Index < C->Count);
145 /* Return the element */
146 return C->Items[Index];
149 # define CollConstAt(C, Index) \
150 (PRECONDITION ((Index) < (C)->Count), \
154 #if defined(HAVE_INLINE)
155 INLINE void* CollLast (Collection* C)
156 /* Return the last item in a collection */
158 /* We must have at least one entry */
159 PRECONDITION (C->Count > 0);
161 /* Return the element */
162 return C->Items[C->Count-1];
165 # define CollLast(C) \
166 (PRECONDITION ((C)->Count > 0), \
167 (C)->Items[(C)->Count-1])
170 #if defined(HAVE_INLINE)
171 INLINE const void* CollConstLast (const Collection* C)
172 /* Return the last item in a collection */
174 /* We must have at least one entry */
175 PRECONDITION (C->Count > 0);
177 /* Return the element */
178 return C->Items[C->Count-1];
181 # define CollConstLast(C) \
182 (PRECONDITION ((C)->Count > 0), \
183 (C)->Items[(C)->Count-1])
186 #if defined(HAVE_INLINE)
187 INLINE void* CollPop (Collection* C)
188 /* Remove the last segment from the stack and return it. Calls FAIL if the
189 * collection is empty.
192 /* We must have at least one entry */
193 PRECONDITION (C->Count > 0);
195 /* Return the element */
196 return C->Items[--C->Count];
199 # define CollPop(C) \
200 (PRECONDITION ((C)->Count > 0), \
201 (C)->Items[--(C)->Count])
204 int CollIndex (Collection* C, const void* Item);
205 /* Return the index of the given item in the collection. Return -1 if the
206 * item was not found in the collection.
209 void CollDelete (Collection* C, unsigned Index);
210 /* Remove the item with the given index from the collection. This will not
211 * free the item itself, just the pointer. All items with higher indices
212 * will get moved to a lower position.
215 void CollDeleteItem (Collection* C, const void* Item);
216 /* Delete the item pointer from the collection. The item must be in the
217 * collection, otherwise FAIL will be called.
220 #if defined(HAVE_INLINE)
221 INLINE void CollDeleteAll (Collection* C)
222 /* Delete all items from the given collection. This will not free the items
223 * itself, it will only remove the pointers.
226 /* This one is easy... */
230 # define CollDeleteAll(C) ((C)->Count = 0)
233 #if defined(HAVE_INLINE)
234 INLINE void CollReplace (Collection* C, void* Item, unsigned Index)
235 /* Replace the item at the given position. The old item will not be freed,
236 * just the pointer will et replaced.
239 /* Check the index */
240 PRECONDITION (Index < C->Count);
242 /* Replace the item pointer */
243 C->Items[Index] = Item;
246 # define CollReplace(C, Item, Index) \
247 (PRECONDITION ((Index) < (C)->Count), \
248 (C)->Items[(Index)] = (Item))
251 void CollSort (Collection* C,
252 int (*Compare) (void*, const void*, const void*),
254 /* Sort the collection using the given compare function. The data pointer is
255 * passed as *first* element to the compare function, it's not used by the
256 * sort function itself. The other two pointer passed to the Compare function
257 * are pointers to objects.
262 /* End of exprlist.h */