]> git.sur5r.net Git - cc65/blob - src/common/coll.h
Convert the watcom makefiles for use with wine under Linux
[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 void CollAppend (Collection* C, void* Item);
115 /* Append an item to the end of the collection */
116 #endif
117
118 #if defined(HAVE_INLINE)
119 INLINE void* CollAt (Collection* C, unsigned Index)
120 /* Return the item at the given index */
121 {
122     /* Check the index */
123     PRECONDITION (Index < C->Count);
124
125     /* Return the element */
126     return C->Items[Index];
127 }
128 #else
129 void* CollAt (Collection* C, unsigned Index);
130 /* Return the item at the given 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 const void* CollConstAt (const Collection* C, unsigned Index);
156 /* Return the item at the given index */
157 #endif
158
159 #if defined(HAVE_INLINE)
160 INLINE void* CollLast (Collection* C)
161 /* Return the last item in a collection */
162 {
163     /* We must have at least one entry */
164     PRECONDITION (C->Count > 0);
165
166     /* Return the element */
167     return C->Items[C->Count-1];
168 }
169 #else
170 void* CollLast (Collection* C);
171 /* Return the last item in a collection */
172 #endif
173
174 #if defined(HAVE_INLINE)
175 INLINE const void* CollConstLast (const Collection* C)
176 /* Return the last item in a collection */
177 {
178     /* We must have at least one entry */
179     PRECONDITION (C->Count > 0);
180
181     /* Return the element */
182     return C->Items[C->Count-1];
183 }
184 #else
185 const void* CollConstLast (const Collection* C);
186 /* Return the last item in a collection */
187 #endif
188
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.
193  */
194 {
195     /* We must have at least one entry */
196     PRECONDITION (C->Count > 0);
197
198     /* Return the element */
199     return C->Items[--C->Count];
200 }
201 #else
202 void* CollPop (Collection* C);
203 /* Remove the last segment from the stack and return it. Calls FAIL if the
204  * collection is empty.
205  */
206 #endif
207
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.
211  */
212
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.
217  */
218
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.
222  */
223
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.
228  */
229 {
230     /* This one is easy... */
231     C->Count = 0;
232 }
233 #else
234 #  define CollDeleteAll(C)      ((C)->Count = 0)
235 #endif
236
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.
241  */
242 {
243     /* Check the index */
244     PRECONDITION (Index < C->Count);
245
246     /* Replace the item pointer */
247     C->Items[Index] = Item;
248 }
249 #else
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.
253  */
254 #endif
255
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.
261  */
262
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
268  * to higher indices.
269  */
270
271 void CollSort (Collection* C,
272                int (*Compare) (void*, const void*, const void*),
273                void* Data);
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.
278  */
279
280
281
282 /* End of coll.h */
283
284 #endif
285
286
287
288