libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moArrayH.h
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moArrayH.h
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code 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 for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30 *******************************************************************************/
31 
32 #ifndef __MO_ARRAYH_H__
33 #define __MO_ARRAYH_H__
34 
35 #include "moTypes.h"
36 
37 template <class T, int N>
38 class moArray {
39 
40 private:
41  T array[N];
42  int n;
43 
44 public:
45  /*
46  moArray();
47  void Set(int x, T value);
48  T Get(int x);
49  */
50  moArray() {
51  n = N;
52  }
53 
54  void Set(int x, T value) {
55  array[x]=value;
56  }
57 
58  T Get(int x) {
59  return array[x];
60  }
61 
62 };
63 
64 template <class T>
65 class moDynamicArray {
66 
67 private:
68  T *array;
69  MOuint n;
70  T m_NULL;
71 
72 public:
73 
75  array = NULL;
76  n = 0;
77  }
78 
80  *this= src ;
81  }
82 
84  Empty();
85  Init( src.n, src.m_NULL );
86  for(MOuint i=0; i< src.n; i++) {
87  array[i] = src.array[i];
88  }
89  return *this;
90  }
91 
92  moDynamicArray(int N) {
93 
94  if (N>0) {
95  array = new T [N];
96  n = N;
97  } else {
98  n=0;
99  array = NULL;
100  }
101 
102  }
103 
104 
105  MOboolean Init( int N, T initvalue ) {
106 
107  m_NULL = initvalue;
108 
109  if (n>0) Empty();
110 
111  if (N>0) {
112  array = new T [N];
113  n = N;
114  for( MOuint i=0; i<n; i++) array[i] = initvalue;
115  } else {
116  n=0;
117  array = NULL;
118  }
119  return (array!=NULL);
120  }
121 
123  Empty();
124  return true;
125  }
126 
127  virtual ~moDynamicArray() {
128  if ( array != NULL ) {
129  delete[] array;
130  array = NULL;
131  n = 0;
132  }
133  }
134 
135  void Empty() {
136 
137  n = 0;
138  if (array!=NULL) {
139  delete[] array;
140  array = NULL;
141  }
142 
143  }
144 
145  void Set(int x, const T &value) {
146 
147  if ( 0<=x && x<(MOint)n && array!=NULL) array[x]=value;
148 
149  }
150 
151  const T& Get(int x) const {
152 
153  if ( 0<=x && x<(MOint)n && array!=NULL) {
154 
155  return array[x];
156 
157  } else return m_NULL;
158  }
159 
160  T& operator [] (int x) {
161  if ( 0<=x && x<(MOint)n && array!=NULL) {
162 
163  return array[x];
164 
165  } else return m_NULL;
166  }
167 
169  return n;
170  }
171 
172  void Add( const T &value ) {
173 
174  MOuint i;
175  T* arrayaux;
176 
177  arrayaux = new T [n+1];
178 
179  for( i=0 ; i < n ; i++ ) {
180  arrayaux[i] = array[i];
181  }
182  if (array!=NULL) {
183  delete[] array;
184  }
185  arrayaux[n] = value;
186  n++;
187  array = arrayaux;
188  }
189 
190  void Remove(int x) {
191 
192  MOuint i,j;
193  T* arrayaux;
194 
195  if ( 0<=x && x<(MOint)n && array!=NULL) {
196 
197  if ( n > 1 ) {
198  arrayaux = new T [n-1];
199  } else arrayaux = NULL;
200 
201  for( i=0, j=0; j < (n-1); i++, j++) {
202  if ( x == (int)i ) i++;
203  arrayaux[j] = array[i];
204  }
205  n--;
206 
207  delete[] array;
208  array = arrayaux;
209  }
210  }
211 
213  Copy(A, 0, A.Count()-1);
214  }
215 
216  void Copy( moDynamicArray &A, int x0, int x1) {
217  Empty();
218  T cpy;
219  for(int i=x0; i<= x1; i++) {
220  cpy = A[i];
221  Add( cpy );
222  }
223  }
224 
225 };
226 
227 /*
228 Ejemplo:
229 
230 Header:
231 #include "moArray.h"
232 class MyClass;
233 moDeclareDynamicArray( MyClass, ArrayOfMyClass);
234 
235 Source:
236 #include "moArray.cpp" // this is a magic incantation which must be done!
237 moDefineDynamicArray( ArrayOfMyClass );
238 */
239 
240 #define _moDeclareDynamicArray( T, name, classexp) \
241 classexp name \
242 { \
243 public: \
244  name(); \
245  name(const name& src); \
246  name(int N); \
247  name& operator=(const name& src); \
248  \
249  virtual ~name(); \
250  \
251  MOboolean Init( int N, T initvalue ); \
252  MOboolean Finish(); \
253  void Empty(); \
254  void Set(int x, const T &value); \
255  void Insert(int x, const T &value); \
256  const T& Get(int x) const; \
257  T& GetRef(int x); \
258  T& Item(int x); \
259  T& operator[] (int x); \
260  MOuint Count() const; \
261  void Add( const T &value ); \
262  void Remove(int x); \
263  void Copy( const name &A); \
264  void Copy( const name &A, int x0, int x1); \
265  \
266 private: \
267  T *array; \
268  MOuint n; \
269  T m_NULL; \
270  \
271 };
272 
273 
274 
275 #define moDeclareDynamicArray(T, name) \
276  moDeclareDynamicArrayDecl(T, name, class )
277 
278 #define moDeclareExportedDynamicArray(T, name) \
279  moDeclareUserExportedDynamicArray(T, name, LIBMOLDEO_API)
280 
281 
282 #define moDeclareDynamicArrayDecl(T, name, decl) \
283  typedef T _moObjArray##name; \
284  _moDeclareDynamicArray(_moObjArray##name, name, decl)
285 
286 #define moDeclareUserExportedDynamicArray(T, name, expmode) \
287  moDeclareDynamicArrayDecl(T, name, class expmode)
288 
289 // moDefineDynamicArray is going to be redefined when arrimpl.cpp is included,
290 // try to provoke a human-understandable error if it used incorrectly.
291 //
292 // there is no real need for 3 different macros in the DEFINE case but do it
293 // anyhow for consistency
294 #define moDefineDynamicArray(name) DidYouIncludeArrayCpp
295 #define moDefineExportedDynamicArray(name) moDefineDynamicArray(name)
296 #define moDefineUserExportedDynamicArray(name) moDefineDynamicArray(name)
297 
298 #endif /* __MO_ARRAYH_H__ */
moDynamicArray & operator=(const moDynamicArray &src)
Definition: moArrayH.h:83
void Copy(moDynamicArray &A)
Definition: moArrayH.h:212
virtual ~moDynamicArray()
Definition: moArrayH.h:127
#define MOboolean
Definition: moTypes.h:385
void Remove(int x)
Definition: moArrayH.h:190
void Empty()
Definition: moArrayH.h:135
moDynamicArray(const moDynamicArray &src)
Definition: moArrayH.h:79
T & operator[](int x)
Definition: moArrayH.h:160
MOboolean Init(int N, T initvalue)
Definition: moArrayH.h:105
moArray()
Definition: moArrayH.h:50
MOboolean Finish()
Definition: moArrayH.h:122
#define MOint
Definition: moTypes.h:388
T Get(int x)
Definition: moArrayH.h:58
void Set(int x, T value)
Definition: moArrayH.h:54
The Alpha (or transparency) of a color.
Definition: moOGLFT.h:89
void Add(const T &value)
Definition: moArrayH.h:172
moDynamicArray(int N)
Definition: moArrayH.h:92
const T & Get(int x) const
Definition: moArrayH.h:151
MOuint Count()
Definition: moArrayH.h:168
void Copy(moDynamicArray &A, int x0, int x1)
Definition: moArrayH.h:216
void Set(int x, const T &value)
Definition: moArrayH.h:145
#define MOuint
Definition: moTypes.h:387
MOuint Count() const
Definition: moArrayH.h:168