]> git.sur5r.net Git - cc65/blob - src/common/coll.h
Added CollIndex and CollDeleteItem
[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 #include "attrib.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   Data                                    */
47 /*****************************************************************************/
48
49
50
51 /* An array of pointers that grows if needed */
52 typedef struct Collection Collection;
53 struct Collection {
54     unsigned            Count;          /* Number of items in the list */
55     unsigned            Size;           /* Size of allocated array */
56     void**              Items;          /* Array with dynamic size */
57 };
58
59 /* Initializer for static collections */
60 #define STATIC_COLLECTION_INITIALIZER   { 0, 0, 0 }
61
62
63
64 /*****************************************************************************/
65 /*                                   Code                                    */
66 /*****************************************************************************/
67
68
69
70 Collection* InitCollection (Collection* C);
71 /* Initialize a collection and return it. */
72
73 void DoneCollection (Collection* C);
74 /* Free the data for a collection. This will not free the data contained in
75  * the collection.
76  */
77
78 Collection* NewCollection (void);
79 /* Create and return a new collection */
80
81 void FreeCollection (Collection* C);
82 /* Free a collection */
83
84 unsigned CollCount (const Collection* C);
85 /* Return the number of items in the collection */
86
87 void CollInsert (Collection* C, void* Item, unsigned Index);
88 /* Insert the data at the given position in the collection */
89
90 void CollAppend (Collection* C, void* Item);
91 /* Append an item to the end of the collection */
92
93 void* CollAt (Collection* C, unsigned Index) attribute ((const));
94 /* Return the item at the given index */
95
96 const void* CollConstAt (const Collection* C, unsigned Index) attribute ((const));
97 /* Return the item at the given index */
98
99 void* CollLast (Collection* C);
100 /* Return the last item in a collection */
101
102 const void* CollConstLast (const Collection* C);
103 /* Return the last item in a collection */
104
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.
108  */
109
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.
114  */
115
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.
119  */
120
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.
124  */
125
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.
129  */
130
131 void CollSort (Collection* C,
132                int (*Compare) (void*, const void*, const void*),
133                void* Data);
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.
138  */
139
140
141
142 /* End of exprlist.h */
143
144 #endif
145
146
147
148