Array.h
1 /**************************************************************************\
2  *
3  * FILE: Array.h
4  *
5  * This source file is part of DIME.
6  * Copyright (C) 1998-1999 by Systems In Motion. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License, version 2, as
10  * published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License (the accompanying file named COPYING) for more
16  * details.
17  *
18  **************************************************************************
19  *
20  * If you need DIME for a non-GPL project, contact Systems In Motion
21  * to acquire a Professional Edition License:
22  *
23  * Systems In Motion http://www.sim.no/
24  * Prof. Brochs gate 6 sales@sim.no
25  * N-7030 Trondheim Voice: +47 22114160
26  * NORWAY Fax: +47 67172912
27  *
28 \**************************************************************************/
29 
30 #ifndef DIME_ARRAY_H
31 #define DIME_ARRAY_H
32 
33 #include <stdlib.h>
34 
35 #include <dime/Basic.h>
36 
37 // FIXME: the #pragmas below is just a quick hack to avoid heaps of
38 // irritating warning messages from the compiler for client code
39 // compiled under MSVC++. Should try to find the real reason for the
40 // warnings and fix the cause of the problem instead. 20020730 mortene.
41 //
42 // UPDATE 20030617 mortene: there is a Microsoft Knowledge Base
43 // article at <URL:http://support.microsoft.com> which is related to
44 // this problem. It's article number KB168958.
45 //
46 // In short, the general solution is that classes that exposes usage
47 // of dimeArray<type> needs to declare the specific template instance
48 // with "extern" and __declspec(dllimport/export).
49 //
50 // That is a lot of work to change, tho'. Another possibility which
51 // might be better is to simply avoid using (exposing) dimeArray from
52 // any of the other public classes. Judging from a quick look, this
53 // seems feasible, and just a couple of hours or so of work.
54 //
55 #ifdef _MSC_VER // Microsoft Visual C++
56 #pragma warning(disable:4251)
57 #pragma warning(disable:4275)
58 #endif // _MSC_VER
59 
60 
61 template <class T>
62 class dimeArray
63 {
64 public:
65  dimeArray(const int initsize = 4);
66  ~dimeArray();
67 
68  void append(const T &value);
69  void append(const dimeArray<T> &array);
70  void prepend(const dimeArray<T> &array);
71  void insertElem(const int idx, const T &value);
72  void setElem(const int index, const T &value);
73  T getElem(const int index) const;
74  void getElem(const int index, T &elem) const;
75  T getLastElem() const;
76  void getLastElem(T &elem) const;
77  T &operator [](const int index);
78  T operator [](const int index) const;
79  void removeElem(const int index);
80  void removeElemFast(const int index);
81  void reverse();
82  void setCount(const int count);
83  void makeEmpty(const int initsize = 4);
84  void freeMemory();
85  int count() const;
86  int allocSize() const;
87  T *arrayPointer();
88  const T *constArrayPointer() const;
89  void shrinkToFit();
90 
91 private:
92  void growArray();
93  T *array;
94  int num;
95  int size;
96 
97 }; // class dimeArray<>
98 
99 template <class T> inline
100 dimeArray<T>::dimeArray(const int size)
101 {
102  this->array = new T[size];
103  this->size = size;
104  this->num = 0;
105 }
106 
107 template <class T> inline
109 {
110  delete [] this->array;
111 }
112 
113 template <class T> inline void
115 {
116  int oldsize = this->size;
117  T *oldarray = this->array;
118  this->size <<= 1;
119  this->array = new T[this->size];
120  for (int i = 0; i < oldsize; i++) this->array[i] = oldarray[i];
121  delete [] oldarray;
122 }
123 
124 template <class T> inline void
125 dimeArray<T>::append(const T &elem)
126 {
127  if (this->num >= this->size) growArray();
128  this->array[this->num++] = elem;
129 }
130 
131 
132 template <class T> inline void
134 {
135  while (this->size <= this->num+array.count()) growArray();
136  for (int i=0;i<array.count();i++)
137  this->array[this->num++] = array[i];
138 }
139 
140 template <class T> inline void
142 {
143  int newsize=this->num+array.count();
144  int i;
145  if (this->size<=newsize) {
146  T *oldarray=this->array;
147  this->array=new T[newsize];
148  this->size=newsize;
149  for (i=0;i<array.count(); i++) this->array[i] = array[i];
150  for (i=0;i<this->num;i++) this->array[i+array.count()] = oldarray[i];
151  delete[] oldarray;
152  }
153  else {
154  for (i=0;i<this->num;i++) this->array[array.count()+i]=this->array[i];
155  for (i=0;i<array.count();i++) this->array[i] = array[i];
156  }
157  this->num+=array.count();
158 }
159 
160 template <class T> inline void
161 dimeArray<T>::insertElem(const int idx, const T &elem)
162 {
163  int n = this->num;
164  this->append(elem); // make room for one more
165  if (idx < n) {
166  for (int i = n; i > idx; i--) {
167  this->array[i] = this->array[i-1];
168  }
169  this->array[idx] = elem;
170  }
171 }
172 
173 template <class T> inline void
174 dimeArray<T>::setElem(const int index, const T &elem)
175 {
176  while (index >= this->size) growArray();
177  if (this->num <= index) this->num = index+1;
178  this->array[index] = elem;
179 }
180 
181 template <class T> inline T
182 dimeArray<T>::getElem(const int index) const
183 {
184  return this->array[index];
185 }
186 
187 template <class T> inline void
188 dimeArray<T>::getElem(const int index, T &elem) const
189 {
190  elem = this->array[index];
191 }
192 
193 template <class T> inline T
195 {
196  return this->array[this->num-1];
197 }
198 
199 template <class T> inline void
200 dimeArray<T>::getLastElem(T &elem) const
201 {
202  elem = this->array[this->num-1];
203 }
204 
205 template <class T> inline T &
206 dimeArray<T>::operator [](const int index)
207 {
208  while (index >= this->size) growArray();
209  if (this->num <= index) this->num = index + 1;
210  return this->array[index];
211 }
212 
213 template <class T> inline T
214 dimeArray<T>::operator [](const int index) const
215 {
216  return this->array[index];
217 }
218 
219 template <class T> inline void
220 dimeArray<T>::removeElem(const int index)
221 {
222  if (this->num <= 0 || index >= this->num) return;
223  for (int i = index; i < this->num-1; i++)
224  this->array[i] = this->array[i+1];
225  this->num--;
226 }
227 
228 template <class T> inline void
230 {
231  this->array[index] = this->array[--this->num];
232 }
233 
234 template <class T> inline void
236 {
237  T tmp;
238  for (int i=0;i<this->num/2;i++) {
239  tmp=this->array[i];
240  this->array[i]=this->array[this->num-1-i];
241  this->array[this->num-1-i]=tmp;
242  }
243 }
244 
245 template <class T> inline void
246 dimeArray<T>::setCount(const int count)
247 {
248  if (count < this->num)
249  this->num = count;
250 }
251 
252 template <class T> inline int
254 {
255  return this->num;
256 }
257 
258 template <class T> inline int
260 {
261  return this->size;
262 }
263 
264 template <class T> inline T *
266 {
267  return this->array;
268 }
269 
270 template <class T> inline const T *
272 {
273  return this->array;
274 }
275 
276 template <class T> inline void
277 dimeArray<T>::makeEmpty(const int initsize)
278 {
279  delete [] this->array;
280  this->array = new T[initsize];
281  this->size = initsize;
282  this->num = 0;
283 }
284 
285 template <class T> inline void
287 {
288  delete [] this->array;
289  this->array = NULL;
290  this->size = 0;
291  this->num = 0;
292 }
293 
294 template <class T> inline void
296 {
297  T *oldarray = this->array;
298  this->array = new T[this->num];
299  for (int i = 0; i < this->num; i++) this->array[i] = oldarray[i];
300  this->size = this->num;
301  delete [] oldarray;
302 }
303 
304 #endif // ! DIME_ARRAY_H
305 

Copyright © 1998-1999, Systems In Motion <sales@sim.no>. All rights reserved.
System documentation was generated using doxygen.