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 /*****************************************************************************/
45 /*****************************************************************************/
47 /*****************************************************************************/
51 /* An array of pointers that grows if needed */
52 typedef struct Collection Collection;
54 unsigned Count; /* Number of items in the list */
55 unsigned Size; /* Size of allocated array */
56 void** Items; /* Array with dynamic size */
59 /* Initializer for static collections */
60 #define STATIC_COLLECTION_INITIALIZER { 0, 0, 0 }
64 /*****************************************************************************/
66 /*****************************************************************************/
70 Collection* InitCollection (Collection* C);
71 /* Initialize a collection and return it. */
73 void DoneCollection (Collection* C);
74 /* Free the data for a collection. This will not free the data contained in
78 Collection* NewCollection (void);
79 /* Create and return a new collection */
81 void FreeCollection (Collection* C);
82 /* Free a collection */
84 unsigned CollCount (const Collection* C);
85 /* Return the number of items in the collection */
87 void CollInsert (Collection* C, void* Item, unsigned Index);
88 /* Insert the data at the given position in the collection */
90 void CollAppend (Collection* C, void* Item);
91 /* Append an item to the end of the collection */
93 void* CollAt (Collection* C, unsigned Index) attribute ((const));
94 /* Return the item at the given index */
96 const void* CollConstAt (const Collection* C, unsigned Index) attribute ((const));
97 /* Return the item at the given index */
99 void* CollLast (Collection* C);
100 /* Return the last item in a collection */
102 const void* CollConstLast (const Collection* C);
103 /* Return the last item in a collection */
105 int CollIndex (Collection* C, const void* Item);
106 /* Return the index of the given item in the collection. Return -1 if the
107 * item was not found in the collection.
110 void CollDelete (Collection* C, unsigned Index);
111 /* Remove the item with the given index from the collection. This will not
112 * free the item itself, just the pointer. All items with higher indices
113 * will get moved to a lower position.
116 void CollDeleteItem (Collection* C, const void* Item);
117 /* Delete the item pointer from the collection. The item must be in the
118 * collection, otherwise FAIL will be called.
121 void CollDeleteAll (Collection* C);
122 /* Delete all items from the given collection. This will not free the items
123 * itself, it will only remove the pointers.
126 void CollReplace (Collection* C, void* Item, unsigned Index);
127 /* Replace the item at the given position. The old item will not be freed,
128 * just the pointer will et replaced.
131 void CollSort (Collection* C,
132 int (*Compare) (void*, const void*, const void*),
134 /* Sort the collection using the given compare function. The data pointer is
135 * passed as *first* element to the compare function, it's not used by the
136 * sort function itself. The other two pointer passed to the Compare function
137 * are pointers to objects.
142 /* End of exprlist.h */