Copyright update for 2011
[bcusdk.git] / common / array.h
blob8838d5e379098a8e352e05ab0d51312ccb321792
1 /*
2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef ARRAY_H
21 #define ARRAY_H
23 /** implements a generic array */
24 template < class T > class Array
26 protected:
27 /** pointer to the data */
28 T * data;
29 /** element count */
30 unsigned count;
31 public:
32 /** create empty array */
33 Array ()
35 data = 0;
36 count = 0;
38 /** destructor */
39 virtual ~ Array ()
41 if (data)
42 delete[]data;
44 /** copy constructor */
45 Array (const Array < T > &a)
47 count = a.count;
48 data = 0;
49 if (count)
51 data = new T[count];
52 for (unsigned i = 0; i < count; i++)
53 data[i] = a.data[i];
57 /** create array from old style array
58 * @param elem pointer to elements
59 * @param cnt element count
61 Array (const T elem[], unsigned cnt)
63 data = 0;
64 count = 0;
65 set (elem, cnt);
68 /** set content to them of a old style array
69 * @param elem pointer to elements
70 * @param cnt element count
72 void set (const T elem[], unsigned cnt)
74 unsigned i;
75 resize (cnt);
76 for (i = 0; i < cnt; i++)
77 data[i] = elem[i];
80 /** copy content of an array */
81 void set (const Array & a)
83 set (a.array (), a ());
86 /** replace a part of the array and resize to fit
87 * @param elem pointer to new elements
88 * @param start start position
89 * @param cnt element count
91 void setpart (const T elem[], unsigned start, unsigned cnt)
93 unsigned i;
94 if (cnt + start > count)
95 resize (cnt + start);
96 for (i = 0; i < cnt; i++)
97 data[i + start] = elem[i];
100 /** replace a part of the array with the content of a and resize to fit
101 * @param start start index
102 * @param a new elements
104 void setpart (const Array < T > &a, unsigned start)
106 setpart (a.array (), start, a ());
109 /** delete content
110 * @param start start index
111 * @param cnt element count
113 void deletepart (unsigned start, unsigned cnt)
115 if (start >= count)
116 return;
117 if (cnt <= 0)
118 return;
119 if (start + cnt >= count)
120 cnt -= start + cnt - count;
121 for (unsigned i = start + cnt; i < count; i++)
122 data[i - cnt] = data[i];
123 resize (count - cnt);
126 /** assignement operator */
127 const Array & operator = (const Array & a)
129 if (data)
130 delete[]data;
131 count = a.count;
132 data = 0;
133 if (count)
135 data = new T[count];
136 for (unsigned i = 0; i < count; i++)
137 data[i] = a.data[i];
139 return a;
142 /** compare array elementwise for equal */
143 bool operator== (const Array & a)
145 if (a () != count)
146 return 0;
147 for (unsigned i = 0; i < count; i++)
148 if (a[i] != data[i])
149 return 0;
150 return 1;
153 /** compare array elementwise for not equal */
154 bool operator!= (const Array & a)
156 return !(*this == a);
159 /** returns pointer to the elements */
160 const T *array () const
162 return data;
165 /** returns pointer to the elements */
166 T *array ()
168 return data;
171 /** resize array to newcount elements */
172 void resize (unsigned newcount)
174 if (!newcount)
176 if (data)
177 delete[]data;
178 data = 0;
179 count = 0;
180 return;
182 T *d1 = new T[newcount];
183 for (unsigned i = 0; i < (count < newcount ? count : newcount); i++)
184 d1[i] = data[i];
185 if (data);
186 delete[]data;
187 data = d1;
188 count = newcount;
191 /** insert elem at pos */
192 void insert (unsigned pos, const T & elem)
194 if (pos >= count)
196 add (elem);
197 return;
200 T *d1 = new T[count + 1];
201 for (unsigned i = 0; i < pos; i++)
202 d1[i] = data[i];
203 data[pos] = elem;
204 for (unsigned i = pos; i < count; i++)
205 d1[i + 1] = data[i];
206 if (data);
207 delete[]data;
208 data = d1;
209 count = count + 1;
212 /** add element elem to the add */
213 void add (const T & elem)
215 resize (count + 1);
216 operator[](count - 1) = elem;
218 /** access random element in standart C notation */
219 T & operator[](unsigned elem)
221 //no boundcheck
222 return data[elem];
224 /** access random element in standart C notation */
225 const T & operator[] (unsigned elem) const
227 //kein Boundcheck
228 return data[elem];
230 /** returns element count */
231 unsigned operator () () const
233 return count;
235 /** return element count */
236 unsigned len () const
238 return count;
241 void sort ()
243 for (int i = 0; i < count; i++)
244 for (int j = i + 1; j < count; j++)
245 if (data[i] > data[j])
247 T x = data[i];
248 data[i] = data[j];
249 data[j] = x;
254 #endif