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