]> git.sur5r.net Git - cc65/blob - src/common/coll.h
Added AUTO_COLLECTION_INITIALIZER
[cc65] / src / common / coll.h
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  coll.h                                   */
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 #ifndef COLL_H
37 #define COLL_H
38
39
40
41 /* common */
42 #include "attrib.h"
43 #include "check.h"
44 #include "inline.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* An array of pointers that grows if needed */
55 typedef struct Collection Collection;
56 struct Collection {
57     unsigned            Count;          /* Number of items in the list */
58     unsigned            Size;           /* Size of allocated array */
59     void**              Items;          /* Array with dynamic size */
60 };
61
62 /* An empty collection */
63 extern const Collection EmptyCollection;
64
65 /* Initializer for static collections */
66 #define STATIC_COLLECTION_INITIALIZER   { 0, 0, 0 }
67
68 /* Initializer for auto collections */
69 #define AUTO_COLLECTION_INITIALIZER     EmptyCollection;
70
71
72
73 /*****************************************************************************/
74 /*                                   Code                                    */
75 /*****************************************************************************/
76
77
78
79 Collection* InitCollection (Collection* C);
80 /* Initialize a collection and return it. */
81
82 void DoneCollection (Collection* C);
83 /* Free the data for a collection. This will not free the data contained in
84  * the collection.
85  */
86
87 Collection* NewCollection (void);
88 /* Create and return a new collection */
89
90 void FreeCollection (Collection* C);
91 /* Free a collection */
92
93 #if defined(HAVE_INLINE)
94 INLINE unsigned CollCount (const Collection* C)
95 /* Return the number of items in the collection */
96 {
97     return C->Count;
98 }
99 #else
100 #  define CollCount(C)  (C)->Count
101 #endif
102
103 void CollInsert (Collection* C, void* Item, unsigned Index);
104 /* Insert the data at the given position in the collection */
105
106 #if defined(HAVE_INLINE)
107 INLINE void CollAppend (Collection* C, void* Item)
108 /* Append an item to the end of the collection */
109 {
110     /* Insert the item at the end of the current list */
111     CollInsert (C, Item, C->Count);
112 }
113 #else
114 #  define CollAppend(C, Item)   CollInsert (C, Item, (C)->Count)
115 #endif
116
117 #if defined(HAVE_INLINE)
118 INLINE void* CollAt (Collection* C, unsigned Index)
119 /* Return the item at the given index */
120 {
121     /* Check the index */
122     PRECONDITION (Index < C->Count);
123
124     /* Return the element */
125     return C->Items[Index];
126 }
127 #else
128 #  define CollAt(C, Index)                      \
129         (PRECONDITION ((Index) < (C)->Count),   \
130         (C)->Items[(Index)])
131 #endif
132
133 #if defined(HAVE_INLINE)
134 INLINE void* CollAtUnchecked (Collection* C, unsigned Index)
135 /* Return the item at the given index */
136 {
137     /* Return the element */
138     return C->Items[Index];
139 }
140 #else
141 #  define CollAtUnchecked(C, Index)     ((C)->Items[(Index)])
142 #endif
143
144 #if defined(HAVE_INLINE)
145 INLINE const void* CollConstAt (const Collection* C, unsigned Index)
146 /* Return the item at the given index */
147 {
148     /* Check the index */
149     PRECONDITION (Index < C->Count);
150
151     /* Return the element */
152     return C->Items[Index];
153 }
154 #else
155 #  define CollConstAt(C, Index)                 \
156         (PRECONDITION ((Index) < (C)->Count),   \
157         (C)->Items[(Index)])
158 #endif
159
160 #if defined(HAVE_INLINE)
161 INLINE void* CollLast (Collection* C)
162 /* Return the last item in a collection */
163 {
164     /* We must have at least one entry */
165     PRECONDITION (C->Count > 0);
166
167     /* Return the element */
168     return C->Items[C->Count-1];
169 }
170 #else
171 #  define CollLast(C)                           \
172         (PRECONDITION ((C)->Count > 0),         \
173         (C)->Items[(C)->Count-1])
174 #endif
175
176 #if defined(HAVE_INLINE)
177 INLINE const void* CollConstLast (const Collection* C)
178 /* Return the last item in a collection */
179 {
180     /* We must have at least one entry */
181     PRECONDITION (C->Count > 0);
182
183     /* Return the element */
184     return C->Items[C->Count-1];
185 }
186 #else
187 #  define CollConstLast(C)                      \
188         (PRECONDITION ((C)->Count > 0),         \
189         (C)->Items[(C)->Count-1])
190 #endif
191
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.
196  */
197 {
198     /* We must have at least one entry */
199     PRECONDITION (C->Count > 0);
200
201     /* Return the element */
202     return C->Items[--C->Count];
203 }
204 #else
205 #  define CollPop(C)                            \
206         (PRECONDITION ((C)->Count > 0),         \
207         (C)->Items[--(C)->Count])
208 #endif
209
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.
213  */
214
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.
219  */
220
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.
224  */
225
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.
230  */
231 {
232     /* This one is easy... */
233     C->Count = 0;
234 }
235 #else
236 #  define CollDeleteAll(C)      ((C)->Count = 0)
237 #endif
238
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.
243  */
244 {
245     /* Check the index */
246     PRECONDITION (Index < C->Count);
247
248     /* Replace the item pointer */
249     C->Items[Index] = Item;
250 }
251 #else
252 #  define CollReplace(C, Item, Index)           \
253         (PRECONDITION ((Index) < (C)->Count),   \
254         (C)->Items[(Index)] = (Item))
255 #endif
256
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.
262  */
263
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
269  * to higher indices.
270  */
271
272 void CollSort (Collection* C,
273                int (*Compare) (void*, const void*, const void*),
274                void* Data);
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.
279  */
280
281
282
283 /* End of exprlist.h */
284
285 #endif
286
287
288
289