]> git.sur5r.net Git - cc65/blob - src/common/coll.c
Added CollLast
[cc65] / src / common / coll.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  coll.c                                   */
4 /*                                                                           */
5 /*                        Collection (dynamic array)                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* cc65 */
43 #include "coll.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 Collection* InitCollection (Collection* C)
54 /* Initialize a collection and return it. */
55 {
56     /* Intialize the fields. */
57     C->Count = 0;
58     C->Size  = 8;
59     C->Items = xmalloc (8 * sizeof (void*));
60
61     /* Return the new struct */
62     return C;
63 }
64
65
66
67 void DoneCollection (Collection* C)
68 /* Free the data for a collection. This will not free the data contained in
69  * the collection.
70  */
71 {
72     /* Free the pointer array */
73     xfree (C->Items);
74 }
75
76
77
78 Collection* NewCollection (void)
79 /* Create and return a new collection with the given initial size */
80 {
81     /* Allocate memory, intialize the collection and return it */
82     return InitCollection (xmalloc (sizeof (Collection)));
83 }
84
85
86
87 void FreeCollection (Collection* C)
88 /* Free a collection */
89 {
90     /* Free the data */
91     DoneCollection (C);
92
93     /* Free the structure itself */
94     xfree (C);
95 }
96
97
98
99 unsigned CollCount (Collection* C)
100 /* Return the number of items in the collection */
101 {
102     return C->Count;
103 }
104
105
106
107 void CollInsert (Collection* C, void* Item, unsigned Index)
108 /* Insert the data at the given position in the collection */
109 {
110     /* Check for invalid indices */
111     PRECONDITION (Index <= C->Count);
112
113     /* Grow the array if necessary */
114     if (C->Count >= C->Size) {
115         /* Must grow */
116         void** NewItems;
117         if (C->Size > 0) {
118             C->Size *= 2;
119         } else {
120             C->Size = 8;
121         }
122         NewItems = xmalloc (C->Size * sizeof (void*));
123         memcpy (NewItems, C->Items, C->Count * sizeof (void*));
124         xfree (C->Items);
125         C->Items = NewItems;
126     }
127
128     /* Move the existing elements if needed */
129     if (C->Count != Index) {
130         memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
131     }
132     ++C->Count;
133
134     /* Store the new item */
135     C->Items[Index] = Item;
136 }
137
138
139
140 void CollAppend (Collection* C, void* Item)
141 /* Append an item to the end of the collection */
142 {
143     /* Insert the item at the end of the current list */
144     CollInsert (C, Item, C->Count);
145 }
146
147
148
149 void* CollAt (Collection* C, unsigned Index)
150 /* Return the item at the given index */
151 {
152     /* Check the index */
153     PRECONDITION (Index < C->Count);
154
155     /* Return the element */
156     return C->Items[Index];
157 }
158
159
160
161 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
171
172
173 void CollDelete (Collection* C, unsigned Index)
174 /* Remove the item with the given index from the collection. This will not
175  * free the item itself, just the pointer. All items with higher indices
176  * will get moved to a lower position.
177  */
178 {
179     /* Check the index */
180     PRECONDITION (Index < C->Count);
181
182     /* Remove the item pointer */
183     --C->Count;
184     memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
185 }
186
187
188
189 void CollReplace (Collection* C, void* Item, unsigned Index)
190 /* Replace the item at the given position. The old item will not be freed,
191  * just the pointer will et replaced.
192  */
193 {
194     /* Check the index */
195     PRECONDITION (Index < C->Count);
196
197     /* Replace the item pointer */
198     C->Items[Index] = Item;
199 }
200
201
202
203