]> git.sur5r.net Git - cc65/blob - src/common/coll.h
Use inline for better performance
[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 "cfeature.h"
44 #include "check.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 /* Initializer for static collections */
63 #define STATIC_COLLECTION_INITIALIZER   { 0, 0, 0 }
64
65
66
67 /*****************************************************************************/
68 /*                                   Code                                    */
69 /*****************************************************************************/
70
71
72
73 Collection* InitCollection (Collection* C);
74 /* Initialize a collection and return it. */
75
76 void DoneCollection (Collection* C);
77 /* Free the data for a collection. This will not free the data contained in
78  * the collection.
79  */
80
81 Collection* NewCollection (void);
82 /* Create and return a new collection */
83
84 void FreeCollection (Collection* C);
85 /* Free a collection */
86
87 #if defined(HAVE_INLINE)
88 INLINE unsigned CollCount (const Collection* C)
89 /* Return the number of items in the collection */
90 {
91     return C->Count;
92 }
93 #else
94 #  define CollCount(C)  (C)->Count
95 #endif
96
97 void CollInsert (Collection* C, void* Item, unsigned Index);
98 /* Insert the data at the given position in the collection */
99
100 #if defined(HAVE_INLINE)
101 INLINE void CollAppend (Collection* C, void* Item)
102 /* Append an item to the end of the collection */
103 {
104     /* Insert the item at the end of the current list */
105     CollInsert (C, Item, C->Count);
106 }
107 #else
108 #  define CollAppend(C, Item)   CollInsert (C, Item, (C)->Count)
109 #endif
110
111 #if defined(HAVE_INLINE)
112 INLINE void* CollAt (Collection* C, unsigned Index)
113 /* Return the item at the given index */
114 {
115     /* Check the index */
116     PRECONDITION (Index < C->Count);
117
118     /* Return the element */
119     return C->Items[Index];
120 }
121 #else
122 #  define CollAt(C, Index)                      \
123         (PRECONDITION ((Index) < (C)->Count),   \
124         (C)->Items[(Index)])
125 #endif
126
127 #if defined(HAVE_INLINE)
128 INLINE void* CollAtUnchecked (Collection* C, unsigned Index)
129 /* Return the item at the given index */
130 {
131     /* Return the element */
132     return C->Items[Index];
133 }
134 #else
135 #  define CollAtUnchecked(C, Index)     ((C)->Items[(Index)])
136 #endif
137
138 #if defined(HAVE_INLINE)
139 INLINE const void* CollConstAt (const Collection* C, unsigned Index)
140 /* Return the item at the given index */
141 {
142     /* Check the index */
143     PRECONDITION (Index < C->Count);
144
145     /* Return the element */
146     return C->Items[Index];
147 }
148 #else
149 #  define CollConstAt(C, Index)                 \
150         (PRECONDITION ((Index) < (C)->Count),   \
151         (C)->Items[(Index)])
152 #endif
153
154 #if defined(HAVE_INLINE)
155 INLINE void* CollLast (Collection* C)
156 /* Return the last item in a collection */
157 {
158     /* We must have at least one entry */
159     PRECONDITION (C->Count > 0);
160
161     /* Return the element */
162     return C->Items[C->Count-1];
163 }
164 #else
165 #  define CollLast(C)                           \
166         (PRECONDITION ((C)->Count > 0),         \
167         (C)->Items[(C)->Count-1])
168 #endif
169
170 #if defined(HAVE_INLINE)
171 INLINE const void* CollConstLast (const Collection* C)
172 /* Return the last item in a collection */
173 {
174     /* We must have at least one entry */
175     PRECONDITION (C->Count > 0);
176
177     /* Return the element */
178     return C->Items[C->Count-1];
179 }
180 #else
181 #  define CollConstLast(C)                      \
182         (PRECONDITION ((C)->Count > 0),         \
183         (C)->Items[(C)->Count-1])
184 #endif
185
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.
190  */
191 {
192     /* We must have at least one entry */
193     PRECONDITION (C->Count > 0);
194
195     /* Return the element */
196     return C->Items[--C->Count];
197 }
198 #else
199 #  define CollPop(C)                            \
200         (PRECONDITION ((C)->Count > 0),         \
201         (C)->Items[--(C)->Count])
202 #endif
203
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.
207  */
208
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.
213  */
214
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.
218  */
219
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.
224  */
225 {
226     /* This one is easy... */
227     C->Count = 0;
228 }
229 #else
230 #  define CollDeleteAll(C)      ((C)->Count = 0)
231 #endif
232
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.
237  */
238 {
239     /* Check the index */
240     PRECONDITION (Index < C->Count);
241
242     /* Replace the item pointer */
243     C->Items[Index] = Item;
244 }
245 #else
246 #  define CollReplace(C, Item, Index)           \
247         (PRECONDITION ((Index) < (C)->Count),   \
248         (C)->Items[(Index)] = (Item))
249 #endif
250
251 void CollSort (Collection* C,
252                int (*Compare) (void*, const void*, const void*),
253                void* Data);
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.
258  */
259
260
261
262 /* End of exprlist.h */
263
264 #endif
265
266
267
268