OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
alist.h
Go to the documentation of this file.
1 /*
2  *
3  * ##### ##### ###### ###### ### ###
4  * ## ## ## ## ## ## ## ### ##
5  * ## ## ## ## #### #### ## # ##
6  * ## ## ## ## ## ## ## ##
7  * ## ## ## ## ## ## ## ##
8  * ##### ##### ## ###### ## ##
9  *
10  *
11  * OOFEM : Object Oriented Finite Element Code
12  *
13  * Copyright (C) 1993 - 2013 Borek Patzak
14  *
15  *
16  *
17  * Czech Technical University, Faculty of Civil Engineering,
18  * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19  *
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33  */
34 
35 #ifndef alist_h
36 #define alist_h
37 
38 #include "error.h"
39 
40 #include <cstddef>
41 
42 
43 namespace oofem {
57 template< class T > class AList
58 {
59 protected:
61  int size;
67  T **values;
68 
69 public:
75  AList(int s = 0, int sizeIncrement = 0);
77  ~AList();
78 
83  inline T *at(int i) const;
88  void growTo(int newSize);
95  void resize(int newSize);
101  bool includes(int i) const;
103  int giveSize() const { return size; }
105  int isEmpty() const { return ( size == 0 ); }
107  int isNotEmpty() const { return ( size != 0 ); }
109  void printYourself() const;
116  void put(int i, T *anObject);
122  void clear(bool deleteObjectFlag = true);
124  void remove(int i);
133  T *unlink(int i);
134 };
135 
136 template< class T >AList< T > :: AList(int s, int sizeIncrement)
137 // Constructor : creates a list of size s.
138 {
139  int i;
140  T **p;
141 
142  allocatedSize = size = s;
143  if ( size ) {
144  values = new T * [ size ];
145  p = values;
146  i = size;
147  while ( i-- ) {
148  * p++ = NULL;
149  }
150  } // initialize 'values'
151  else {
152  values = NULL;
153  }
154 
155  this->sizeIncrement = sizeIncrement;
156 }
157 
158 
159 template< class T > AList< T > :: ~AList()
160 {
161  this->clear(true);
162 }
163 
164 template< class T > void
165 AList< T > :: clear(bool deleteObjectFlag)
166 {
167  int i = size;
168 
169  if ( size ) {
170  if ( deleteObjectFlag ) {
171  while ( i-- ) {
172  delete(values [ i ]);
173  }
174  }
175 
176  delete[] values;
177  }
178 
179  allocatedSize = size = 0;
180  values = NULL;
181 }
182 
183 template< class T > void
185 // Expands the receiver from its current size to newSize, in order to accommodate new entries.
186 {
187  int i;
188  T **newValues, **p1, **p2;
189 
190 
191  if ( newSize < size ) {
192 #ifdef DEBUG
193  OOFEM_ERROR("new list size (%d) not larger than current size (%d)", newSize, size);
194 #endif
195  // delete entities in indexes in the range (newSize, size)
196  i = size;
197  if ( size ) {
198  while ( ( --i ) >= newSize ) {
199  delete(values [ i ]);
200  values [ i ] = NULL;
201  }
202  }
203  } else if ( newSize > allocatedSize ) {
204  this->allocatedSize = newSize + this->sizeIncrement;
205  newValues = new T * [ this->allocatedSize ];
206  p1 = values;
207  p2 = newValues;
208  for ( i = 0; i < size; i++ ) {
209  * p2++ = * p1++;
210  }
211 
212  for ( i = size; i < this->allocatedSize; i++ ) {
213  * p2++ = NULL;
214  }
215 
216  if ( values ) {
217  delete[] values;
218  }
219 
220  values = newValues;
221  this->allocatedSize = newSize + this->sizeIncrement;
222  }
223 
224  size = newSize;
225 }
226 
227 template< class T > void
229 // Expands the receiver from its current size to newSize, in order to accommodate new entries.
230 {
231  int i;
232  T **newValues, **p1, **p2;
233 
234 
235  if ( newSize < size ) {
236  // delete entities in indexes in the range (newSize, size)
237  i = size;
238  if ( size ) {
239  while ( ( --i ) >= newSize ) {
240  delete(values [ i ]);
241  values [ i ] = NULL;
242  }
243  }
244  } else if ( newSize > allocatedSize ) {
245  this->allocatedSize = newSize + this->sizeIncrement;
246  newValues = new T * [ this->allocatedSize ];
247  p1 = values;
248  p2 = newValues;
249  for ( i = 0; i < size; i++ ) {
250  * p2++ = * p1++;
251  }
252 
253  for ( i = size; i < this->allocatedSize; i++ ) {
254  * p2++ = NULL;
255  }
256 
257  if ( values ) {
258  // delete [size] values ;
259  delete[] values;
260  }
261 
262  values = newValues;
263  this->allocatedSize = newSize + this->sizeIncrement;
264  }
265 
266  size = newSize;
267 }
268 
269 template< class T > T *
270 AList< T > :: at(int i) const
271 {
272 #ifdef DEBUG
273  if ( i <= 0 ) {
274  OOFEM_ERROR("Asking for negative or zero indices (%d)", i);
275  }
276 #endif
277  return values [ i - 1 ];
278 }
279 
280 template< class T > bool
282 // Returns True if the receiver has a non-null i-th entry, else returns
283 // False.
284 {
285 #ifdef DEBUG
286  if ( i <= 0 ) {
287  OOFEM_ERROR("Asking for negative or zero indices (%d)", i);
288  }
289 #endif
290  if ( i > size ) {
291  return false;
292  } else {
293  return ( values [ i - 1 ] != NULL );
294  }
295 }
296 
297 template< class T > void
299 // Prints the receiver on screen.
300 {
301  printf("List of components of size %d\n", size);
302  for ( int i = 1; i <= size; i++ ) {
303  printf("%d : %p\n", i, values [ i - 1 ]);
304  }
305 }
306 
307 template< class T > void AList< T > :: put(int i, T *anObject)
308 // Stores anObject at position i. Enlarge the receiver if too small.
309 {
310 #ifdef DEBUG
311  if ( i <= 0 ) {
312  OOFEM_ERROR("Trying to write to zero or negative indices (%d)", i);
313  }
314 #endif
315  if ( size < i ) {
316  this->growTo(i);
317  }
318 
319  // delete old value
320  if ( values [ i - 1 ] ) {
321  delete values [ i - 1 ];
322  }
323 
324  values [ i - 1 ] = anObject;
325 }
326 
327 template< class T > void AList< T > :: remove(int i)
328 {
329 #ifdef DEBUG
330  if ( i < 0 ) {
331  OOFEM_ERROR("Trying to remove at zero or negative indices (%d)", i);
332  }
333 #endif
334 
335  if ( size < i ) {
336  return;
337  }
338 
339  if ( values [ i - 1 ] ) {
340  delete values [ i - 1 ];
341  values [ i - 1 ] = NULL;
342  }
343 }
344 
345 
346 template< class T > T *AList< T > :: unlink(int i)
347 {
348 #ifdef DEBUG
349  if ( i <= 0 ) {
350  OOFEM_ERROR("Trying to unlink at zero or negative indices (%d)", i);
351  }
352 #endif
353  if ( size < i ) {
354  return NULL;
355  }
356 
357  T *answer = values [ i - 1 ];
358  values [ i - 1 ] = NULL;
359  return answer;
360 }
361 } // end namespace oofem
362 #endif // alist_h
Class implementing generic list (or more precisely array).
Definition: alist.h:57
int giveSize() const
Definition: alist.h:103
int isNotEmpty() const
Definition: alist.h:107
int isEmpty() const
Definition: alist.h:105
int allocatedSize
Real allocated size (may be larger than size, to prevent often reallocation).
Definition: alist.h:63
~AList()
Destructor.
Definition: alist.h:159
T ** values
Array of pointers to particular components.
Definition: alist.h:67
int sizeIncrement
List size allocation increment.
Definition: alist.h:65
void printYourself() const
Prints the receiver on screen.
Definition: alist.h:298
int size
Array or list size (number of components to store).
Definition: alist.h:61
#define OOFEM_ERROR(...)
Definition: error.h:61
bool includes(int i) const
Checks if receiver includes object as given position.
Definition: alist.h:281
void put(int i, T *anObject)
Stores anObject at position i.
Definition: alist.h:307
void resize(int newSize)
Resize the receiver from its current size to newSize, in order to accommodate new entries...
Definition: alist.h:228
T * unlink(int i)
Unlinks the object a i-th position.
Definition: alist.h:346
T * at(int i) const
Definition: alist.h:270
void clear(bool deleteObjectFlag=true)
Clears receiver.
Definition: alist.h:165
AList(int s=0, int sizeIncrement=0)
Creates list of size s.
Definition: alist.h:136
void growTo(int newSize)
Expands the receiver from its current size to newSize, in order to accommodate new entries...
Definition: alist.h:184
void remove(int i)
Deletes the object at i-th position.
Definition: alist.h:327
the oofem namespace is to define a context or scope in which all oofem names are defined.

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:27 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011