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
moArray.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moArray.cpp
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 #include "moArray.h"
33 
34 // *****************************************************************************
35 // * Purpose: implements methods of "template" class declared in *
36 // * DECLARE_OBJARRAY macro and which couldn't be implemented inline *
37 // * (because they need the full definition of type T in scope) *
38 // * *
39 // * Usage: 1) #include dynarray.h *
40 // * 2) WX_DECLARE_OBJARRAY *
41 // * 3) #include arrimpl.cpp *
42 // * 4) WX_DEFINE_OBJARRAY *
43 // *****************************************************************************
44 
45 // macro implements remaining (not inline) methods of template list
46 // (it's private to this file)
47 #undef _moDefineDynamicArray
48 #define _moDefineDynamicArray(T, name) \
49 \
50 name::name() {\
51  array = NULL;\
52  n = 0;\
53 }\
54 \
55 name::name(int N) {\
56  \
57  if (N>0) {\
58  array = new T [N];\
59  n = N;\
60  } else {\
61  n=0;\
62  array = NULL;\
63  }\
64  \
65 }\
66 \
67 name::name(const name& src) {\
68  \
69  Copy( src);\
70  \
71 }\
72 \
73 name& name::operator=(const name& src) {\
74 \
75  \
76  Copy( src);\
77  return (*this);\
78 }\
79 \
80 name::~name() {\
81  if ( array != NULL ) {\
82  delete[] array;\
83  array = NULL;\
84  n = 0;\
85  }\
86 }\
87 \
88 MOboolean name::Init( int N, T initvalue ) {\
89  m_NULL = initvalue;\
90 \
91  if (n>0) Empty();\
92 \
93  if (N>0) {\
94  array = new T [N];\
95  n = N;\
96  for( MOuint i=0; i<n; i++) array[i] = initvalue;\
97  } else {\
98  n=0;\
99  array = NULL;\
100  }\
101  return (array!=NULL);\
102 }\
103 \
104 MOboolean name::Finish() {\
105  Empty();\
106  return true;\
107 }\
108 \
109 void name::Empty() {\
110 \
111  n = 0;\
112  if (array!=NULL) {\
113  delete[] array;\
114  array = NULL;\
115  }\
116 \
117 }\
118 \
119 void name::Set(int x, const T &value) {\
120 \
121  if ( 0<=x && x<(MOint)n && array!=NULL) array[x]=value;\
122 \
123 }\
124 \
125 void name::Insert(int x, const T &value) {\
126 \
127  if ( 0<=x && x<(MOint)n && array!=NULL) {\
128 \
129  MOuint i,j;\
130  T* arrayaux;\
131 \
132  arrayaux = new T [n+1];\
133 \
134  for( i=0,j=0 ; i < (n+1) ; i++,j++ ) { \
135  ((int)i==x) ? arrayaux[j--] = value : arrayaux[i] = array[j];\
136  }\
137  arrayaux[n] = value;\
138  n++;\
139 \
140  if (array!=NULL) delete[] array;\
141  array = arrayaux;\
142  }\
143 \
144 }\
145 \
146 T& name::Get(int x) {\
147 \
148  if ( 0<=x && x<(MOint)n && array!=NULL) {\
149 \
150  return array[x]; \
151 \
152  } else return m_NULL;\
153 }\
154 \
155 MOuint name::Count() const {\
156  return n;\
157 }\
158 \
159 void name::Add( const T& value ) {\
160 \
161  MOuint i;\
162  T* arrayaux;\
163 \
164  arrayaux = new T [n+1];\
165 \
166  if (array!=NULL)\
167  for( i=0 ; i < n ; i++ ) { \
168  arrayaux[i] = array[i];\
169  }\
170  arrayaux[n] = value;\
171  n++;\
172 \
173  if (array!=NULL) delete[] array;\
174  array = arrayaux;\
175 }\
176 \
177 void name::Remove(int x) {\
178 \
179  MOuint i,j;\
180  T* arrayaux;\
181  bool founded = false;\
182 \
183  if ( (MOint)0<=x && x<(MOint)n && array!=NULL) {\
184  \
185  if ( n > 1 ) {\
186  arrayaux = new T [n-1];\
187  for( i=0, j=0; j < (n-1); i++, j++) {\
188  if ( x == (MOint)i ) {\
189  i++;\
190  }\
191  arrayaux[j] = array[i];\
192  }\
193  n--;\
194  } else {\
195  arrayaux = NULL;\
196  n = 0;\
197  }\
198 \
199  delete[] array;\
200  array = arrayaux;\
201  }\
202 }\
203 \
204 \
205 void name::Copy( const name &A) {\
206  Empty();\
207  for(MOuint i=0; i< A.n; i++) {\
208  Add( A.array[i] );\
209  }\
210 }\
211 \
212 void name::Copy( const name &A, int x0, int x1) {\
213  Empty();\
214  for(int i=x0; i<= x1; i++) {\
215  Add( A.array[i] );\
216  }\
217 }
218 
219 // redefine the macro so that now it will generate the class implementation
220 // old value would provoke a compile-time error if this file is not included
221 #undef moDefineDynamicArray
222 #define moDefineDynamicArray(name) _moDefineDynamicArray( _moObjArray##name, name)
223 
224 
225