]> git.sur5r.net Git - cc65/blob - src/common/coll.c
Added a module that implements dynamic arrays.
[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         C->Size *= 2;
118         NewItems = xmalloc (C->Size * sizeof (void*));
119         memcpy (NewItems, C->Items, C->Count * sizeof (void*));
120         xfree (C->Items);
121         C->Items = NewItems;
122     }
123
124     /* Move the existing elements if needed */
125     if (C->Count != Index) {
126         memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*));
127     }
128     ++C->Count;
129
130     /* Store the new item */
131     C->Items[Index] = Item;
132 }
133
134
135
136 void CollAppend (Collection* C, void* Item)
137 /* Append an item to the end of the collection */
138 {
139     /* Insert the item at the end of the current list */
140     CollInsert (C, Item, C->Count);
141 }
142
143
144
145 void* CollAt (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
155
156
157 void CollDelete (Collection* C, unsigned Index)
158 /* Remove the item with the given index from the collection. This will not
159  * free the item itself, just the pointer. All items with higher indices
160  * will get moved to a lower position.
161  */
162 {
163     /* Check the index */
164     PRECONDITION (Index < C->Count);
165
166     /* Remove the item pointer */
167     --C->Count;
168     memmove (C->Items+Index, C->Items+Index+1, (C->Count-Index) * sizeof (void*));
169 }
170
171
172
173 void CollReplace (Collection* C, void* Item, unsigned Index)
174 /* Replace the item at the given position. The old item will not be freed,
175  * just the pointer will et replaced.
176  */
177 {
178     /* Check the index */
179     PRECONDITION (Index < C->Count);
180
181     /* Replace the item pointer */
182     C->Items[Index] = Item;
183 }
184
185
186
187