dime  0.9.1
Portable DXF file library
Loading...
Searching...
No Matches
Array.h
1/**************************************************************************\
2 * Copyright (c) Kongsberg Oil & Gas Technologies AS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31\**************************************************************************/
32
33#ifndef DIME_ARRAY_H
34#define DIME_ARRAY_H
35
36#include <stdlib.h>
37
38#include <dime/Basic.h>
39
40// FIXME: the #pragmas below is just a quick hack to avoid heaps of
41// irritating warning messages from the compiler for client code
42// compiled under MSVC++. Should try to find the real reason for the
43// warnings and fix the cause of the problem instead. 20020730 mortene.
44//
45// UPDATE 20030617 mortene: there is a Microsoft Knowledge Base
46// article at <URL:http://support.microsoft.com> which is related to
47// this problem. It's article number KB168958.
48//
49// In short, the general solution is that classes that exposes usage
50// of dimeArray<type> needs to declare the specific template instance
51// with "extern" and __declspec(dllimport/export).
52//
53// That is a lot of work to change, tho'. Another possibility which
54// might be better is to simply avoid using (exposing) dimeArray from
55// any of the other public classes. Judging from a quick look, this
56// seems feasible, and just a couple of hours or so of work.
57//
58#ifdef _MSC_VER // Microsoft Visual C++
59#pragma warning(disable:4251)
60#pragma warning(disable:4275)
61#endif // _MSC_VER
62
63
64template <class T>
66{
67public:
68 dimeArray(const int initsize = 4);
69 ~dimeArray();
70
71 void append(const T &value);
72 void append(const dimeArray<T> &array);
73 void prepend(const dimeArray<T> &array);
74 void insertElem(const int idx, const T &value);
75 void setElem(const int index, const T &value);
76 T getElem(const int index) const;
77 void getElem(const int index, T &elem) const;
78 T getLastElem() const;
79 void getLastElem(T &elem) const;
80 T &operator [](const int index);
81 T operator [](const int index) const;
82 void removeElem(const int index);
83 void removeElemFast(const int index);
84 void reverse();
85 void setCount(const int count);
86 void makeEmpty(const int initsize = 4);
87 void freeMemory();
88 int count() const;
89 int allocSize() const;
91 const T *constArrayPointer() const;
93
94private:
95 void growArray();
96 T *array;
97 int num;
98 int size;
99
100}; // class dimeArray<>
101
102template <class T> inline
103dimeArray<T>::dimeArray(const int size)
104{
105 this->array = new T[size];
106 this->size = size;
107 this->num = 0;
108}
109
110template <class T> inline
112{
113 delete [] this->array;
114}
115
116template <class T> inline void
118{
119 int oldsize = this->size;
120 T *oldarray = this->array;
121 this->size <<= 1;
122 this->array = new T[this->size];
123 for (int i = 0; i < oldsize; i++) this->array[i] = oldarray[i];
124 delete [] oldarray;
125}
126
127template <class T> inline void
128dimeArray<T>::append(const T &elem)
129{
130 if (this->num >= this->size) growArray();
131 this->array[this->num++] = elem;
132}
133
134
135template <class T> inline void
137{
138 while (this->size <= this->num+array.count()) growArray();
139 for (int i=0;i<array.count();i++)
140 this->array[this->num++] = array[i];
141}
142
143template <class T> inline void
145{
146 int newsize=this->num+array.count();
147 int i;
148 if (this->size<=newsize) {
149 T *oldarray=this->array;
150 this->array=new T[newsize];
151 this->size=newsize;
152 for (i=0;i<array.count(); i++) this->array[i] = array[i];
153 for (i=0;i<this->num;i++) this->array[i+array.count()] = oldarray[i];
154 delete[] oldarray;
155 }
156 else {
157 for (i=0;i<this->num;i++) this->array[array.count()+i]=this->array[i];
158 for (i=0;i<array.count();i++) this->array[i] = array[i];
159 }
160 this->num+=array.count();
161}
162
163template <class T> inline void
164dimeArray<T>::insertElem(const int idx, const T &elem)
165{
166 int n = this->num;
167 this->append(elem); // make room for one more
168 if (idx < n) {
169 for (int i = n; i > idx; i--) {
170 this->array[i] = this->array[i-1];
171 }
172 this->array[idx] = elem;
173 }
174}
175
176template <class T> inline void
177dimeArray<T>::setElem(const int index, const T &elem)
178{
179 while (index >= this->size) growArray();
180 if (this->num <= index) this->num = index+1;
181 this->array[index] = elem;
182}
183
184template <class T> inline T
185dimeArray<T>::getElem(const int index) const
186{
187 return this->array[index];
188}
189
190template <class T> inline void
191dimeArray<T>::getElem(const int index, T &elem) const
192{
193 elem = this->array[index];
194}
195
196template <class T> inline T
198{
199 return this->array[this->num-1];
200}
201
202template <class T> inline void
203dimeArray<T>::getLastElem(T &elem) const
204{
205 elem = this->array[this->num-1];
206}
207
208template <class T> inline T &
209dimeArray<T>::operator [](const int index)
210{
211 while (index >= this->size) growArray();
212 if (this->num <= index) this->num = index + 1;
213 return this->array[index];
214}
215
216template <class T> inline T
217dimeArray<T>::operator [](const int index) const
218{
219 return this->array[index];
220}
221
222template <class T> inline void
224{
225 if (this->num <= 0 || index >= this->num) return;
226 for (int i = index; i < this->num-1; i++)
227 this->array[i] = this->array[i+1];
228 this->num--;
229}
230
231template <class T> inline void
233{
234 this->array[index] = this->array[--this->num];
235}
236
237template <class T> inline void
239{
240 T tmp;
241 for (int i=0;i<this->num/2;i++) {
242 tmp=this->array[i];
243 this->array[i]=this->array[this->num-1-i];
244 this->array[this->num-1-i]=tmp;
245 }
246}
247
248template <class T> inline void
250{
251 if (count < this->num)
252 this->num = count;
253}
254
255template <class T> inline int
257{
258 return this->num;
259}
260
261template <class T> inline int
263{
264 return this->size;
265}
266
267template <class T> inline T *
269{
270 return this->array;
271}
272
273template <class T> inline const T *
275{
276 return this->array;
277}
278
279template <class T> inline void
280dimeArray<T>::makeEmpty(const int initsize)
281{
282 delete [] this->array;
283 this->array = new T[initsize];
284 this->size = initsize;
285 this->num = 0;
286}
287
288template <class T> inline void
290{
291 delete [] this->array;
292 this->array = NULL;
293 this->size = 0;
294 this->num = 0;
295}
296
297template <class T> inline void
299{
300 T *oldarray = this->array;
301 this->array = new T[this->num];
302 for (int i = 0; i < this->num; i++) this->array[i] = oldarray[i];
303 this->size = this->num;
304 delete [] oldarray;
305}
306
307#endif // ! DIME_ARRAY_H
308
The dimeArray class is internal / private.
Definition Array.h:66
void makeEmpty(const int initsize=4)
Definition Array.h:280
void removeElemFast(const int index)
Definition Array.h:232
int count() const
Definition Array.h:256
void freeMemory()
Definition Array.h:289
void setCount(const int count)
Definition Array.h:249
int allocSize() const
Definition Array.h:262
void removeElem(const int index)
Definition Array.h:223
const T * constArrayPointer() const
Definition Array.h:274
T * arrayPointer()
Definition Array.h:268
void shrinkToFit()
Definition Array.h:298