array.h

changeset 63
0557babc8675
child 70
fc257920ac00
equal deleted inserted replaced
62:824ab7b28e3c 63:0557babc8675
1 /*
2 * botc source code
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of the developer nor the names of its contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 * 4. Redistributions in any form must be accompanied by information on how to
18 * obtain complete source code for the software and any accompanying
19 * software that uses the software. The source code must either be included
20 * in the distribution or be available for no more than the cost of
21 * distribution plus a nominal fee, and must be freely redistributable
22 * under reasonable conditions. For an executable file, complete source
23 * code means the source code for all modules it contains. It does not
24 * include source code for modules or files that typically accompany the
25 * major components of the operating system on which the executable file
26 * runs.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include "common.h"
45
46 #define ITERATE_SUBSCRIPTS(link) \
47 for (link = data; link; link = link->next)
48
49 // Single element of an array
50 template <class T> class arrayElement {
51 public:
52 T value;
53 arrayElement<T>* next;
54
55 arrayElement () {
56 next = NULL;
57 }
58 };
59
60 // Dynamic array
61 template <class T> class array {
62 public:
63 array () {
64 data = NULL;
65 }
66
67 array (T* stuff, unsigned int c) {
68 printf ("%d elements\n", c);
69 data = NULL;
70
71 for (unsigned int i = 0; i < c; i++)
72 push (stuff[c]);
73 }
74
75 ~array () {
76 if (data)
77 deleteElement (data);
78 }
79
80 void push (T stuff) {
81 arrayElement<T>* e = new arrayElement<T>;
82 e->value = stuff;
83 e->next = NULL;
84
85 if (!data) {
86 data = e;
87 return;
88 }
89
90 arrayElement<T>* link;
91 for (link = data; link && link->next; link = link->next);
92 link->next = e;
93 }
94
95 T pop () {
96 int pos = size() - 1;
97 if (pos == -1)
98 error ("array::pop: tried to pop an array with no elements\n");
99 T res = subscript (pos);
100 remove (pos);
101 return res;
102 }
103
104 void remove (unsigned int pos) {
105 if (!data)
106 error ("tried to use remove on an array with no elements");
107
108 if (pos == 0) {
109 // special case for first element
110 arrayElement<T>* first = data;
111 data = data->next;
112 delete first;
113 return;
114 }
115
116 arrayElement<T>* link = data;
117 unsigned int x = 0;
118 while (link->next) {
119 if (x == pos - 1)
120 break;
121 link = link->next;
122 x++;
123 }
124 if (!link)
125 error ("no such element in array\n");
126
127 arrayElement<T>* nextlink = link->next->next;
128 delete link->next;
129 link->next = nextlink;
130 }
131
132 unsigned int size () {
133 unsigned int x = 0;
134 arrayElement<T>* link;
135 ITERATE_SUBSCRIPTS(link)
136 x++;
137 return x;
138 }
139
140 T& subscript (unsigned int i) {
141 arrayElement<T>* link;
142 unsigned int x = 0;
143 ITERATE_SUBSCRIPTS(link) {
144 if (x == i)
145 return link->value;
146 x++;
147 }
148
149 error ("array: tried to access subscript %u in an array of %u elements\n", i, x);
150 return data->value;
151 }
152
153 T* out () {
154 int s = size();
155 T* out = new T[s];
156 unsigned int x = 0;
157
158 arrayElement<T>* link;
159 ITERATE_SUBSCRIPTS(link) {
160 out[x] = link->value;
161 x++;
162 }
163
164 return out;
165 }
166
167 T& operator [] (const unsigned int i) {
168 return subscript (i);
169 }
170
171 void operator << (const T stuff) {
172 push (stuff);
173 }
174
175 private:
176 void deleteElement (arrayElement<T>* e) {
177 if (e->next)
178 deleteElement (e->next);
179 delete e;
180 }
181
182 arrayElement<T>* data;
183 };

mercurial