DOFIterator.h 13.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
24
25
26
27
28
29

/** \file DOFIterator.h */

#ifndef AMDIS_DOFITERATOR_H
#define AMDIS_DOFITERATOR_H

#include "DOFAdmin.h"
#include "FiniteElemSpace.h"
30
#include "AMDiS_fwd.h"
31
32
33

namespace AMDiS {

34
  /// Possible types of DOFIterator
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  typedef enum {
    USED_DOFS = 0, /**< iterate only used DOFs */
    FREE_DOFS = 1, /**< iterate only free DOFs */
    ALL_DOFS  = 2  /**< iterate all DOFs */
  } DOFIteratorType;


  /** \ingroup DOFAdministration
   * \brief
   * DOFIteratorBase can be the base class of Iterators for DOFIndexed objects
   * or it can be used stand alone. Than it iterates through DOFAdmin's dofFree
   * vector which stores whether a DOF is used or not. If it is used as base
   * class for another Iterator, it provides base functionality, to iterate
   * through the \ref iteratedObject of the sub class. All you have to do is to 
   * override the methods \ref goToBeginOfIteratedObject(), 
   * \ref goToEndOfIteratedObject() and \ref incObjectIterator(). 
   * Normally it is usefull to provide 
   * operator->() and operator*(), to dereference the iterator. But this is not 
   * possible in this base class, because it is template type independent.
   */
  class DOFIteratorBase
  {
57
58
59
  public:
    typedef std::bidirectional_iterator_tag	iterator_category;
  
60
61
62
63
64
65
66
67
68
  public:
    /** \brief
     * Constructs a DOFIteratorBase object of type t which can iterate through 
     * admin's dofFree vector
     */
    DOFIteratorBase(DOFAdmin* admin, DOFIteratorType t) 
      : dofAdmin(admin), 
	dofFree(&(dofAdmin->dofFree)),
	type(t)
69
    {}
70

71
    virtual ~DOFIteratorBase() {}
72
73
74
75
76
77

    /** \brief
     * Resets the iterator to the begin of the iterated object. 
     * Sub classes must
     * implement goToBeginOfIteratedObject() which resets the iterator.
     */
78
79
    virtual void reset() 
    {
80
81
      position = 0;
      dofFreeIterator = dofFree->begin();
82
      if (dofFreeIterator == dofFree->end())
83
	return;
84

85
      goToBeginOfIteratedObject();
86
87
      if (type != ALL_DOFS)
	if (*dofFreeIterator == (type == USED_DOFS))
88
	  operator++();
89
    }
90
91
92
93
94
95

    /** \brief
     * Resets the iterator to the begin of the iterated object. 
     * Sub classes must
     * implement goToBeginOfIteratedObject() which resets the iterator.
     */
96
97
    virtual void reset2() 
    {
98
99
      position = 0;
      dofFreeIterator = dofFree->begin();
Thomas Witkowski's avatar
Thomas Witkowski committed
100
      if (dofFreeIterator == dofFree->end()) 
101
	return;
Thomas Witkowski's avatar
Thomas Witkowski committed
102
      
103
      goToBeginOfIteratedObject();
Thomas Witkowski's avatar
Thomas Witkowski committed
104
      if (type != ALL_DOFS)
105
	if (*dofFreeIterator == (type == USED_DOFS))
106
	  operator++();
107
    }
108
109
110
111
112
113
114
115
116
117

    /** \brief
     * Prefix operator++.
     * Incrementation depends of the type of the iterator. If type is USED_DOFS,
     * the iterator points to the next used DOF after operator call. If type is
     * FREE_DOFS, it points to the next free DOF and if type is ALL_DOFS, it will
     * point to the next DOF regardless whether it is used or not. Sub classes
     * must implement incObjectIterator() which increments the object
     * iterator.
     */
118
119
    inline const DOFIteratorBase& operator++() 
    {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      if (type == ALL_DOFS) {
	incObjectIterator();
	dofFreeIterator++;
	position++;
	return *this;
      }

      if (type == USED_DOFS) {
	if (position >= dofAdmin->getUsedSize()) {
	  position = dofAdmin->getSize();
	  goToEndOfIteratedObject();
	  dofFreeIterator = dofFree->end();
	  return *this;
	}
      }

      do {
	incObjectIterator();
	dofFreeIterator++;
	position++;
      } while ((dofFreeIterator != dofFree->end()) 
	       && (*dofFreeIterator == (type == USED_DOFS)));

      return *this;      
144
    }
145

146
    /// Postfix operator++.
147
148
    inline DOFIteratorBase operator++(int) 
    { 
149
150
151
      DOFIteratorBase clone = *this;
      operator++();
      return clone;
152
    }
153

154
155
    inline const DOFIteratorBase& operator--() 
    {
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
      if (type == ALL_DOFS) {
	decObjectIterator();
	dofFreeIterator--;
	position--;
	return *this;
      }

      do {
	decObjectIterator();
	dofFreeIterator--;
	position--;
      } while ((dofFreeIterator != dofFree->begin())
	       && (*dofFreeIterator == (type == USED_DOFS)));

      return *this;
171
    }
172

173
174
    inline DOFIteratorBase operator--(int) 
    {
175
176
177
178
179
      DOFIteratorBase clone = *this;
      operator--();
      return clone;
    }
  
180
    /// Dereferntiation of the \ref dofFreeIterator
181
    virtual bool isDofFree() 
182
    {
183
      return *dofFreeIterator;
184
    }
185
186
187
188
189

    /** \brief
     * Returns whether \ref dofFreeIterator already has reached the end of 
     * \ref dofFree
     */
190
191
192
193
    inline bool end() 
    { 
      return (dofFreeIterator == dofFree->end()); 
    }
194

195
196
197
198
    inline bool begin() 
    { 
      return (dofFreeIterator == dofFree->begin()); 
    }
199

200
    /// Returns the current position index of this iterator
201
202
203
204
    inline int getDOFIndex() 
    { 
      return position; 
    }
205
206

  protected:
207
208
    /// Override this to enable iteration through the object
    virtual void goToBeginOfIteratedObject() {}
209

210
211
    /// Override this to enable iteration through the object
    virtual void goToEndOfIteratedObject() {}
212

213
214
    /// Override this to enable iteration through the object
    virtual void incObjectIterator() {}
215
    
216
    virtual void decObjectIterator() {}
217
218

  protected:
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    /// DOFAdmin which contains the dofFree vector.
    DOFAdmin *dofAdmin;
    
    /// Current position index.
    int position;

    /// Stores which DOFs are used.
    std::vector<bool> *dofFree; 

    /// Iterator for dofFree.
    std::vector<bool>::iterator dofFreeIterator;

    /// Type of this iterator.
    const DOFIteratorType type;
233
234
235
236
237
238
239
240
241
242
  };


  /** \ingroup DOFAdministration
   * \brief
   * Implements a DOFIterator for a DOFIndexed<T> object
   */
  template<typename T>
  class DOFIterator : public DOFIteratorBase
  {
243
244
245
246
247
  public:
    typedef T 					value_type;
    typedef typename DOFIndexed<T>::reference	reference;
    typedef typename DOFIndexed<T>::pointer	pointer;
  
248
  public:
249
    /// Constructs a DOFIterator for cont of type t
250
    DOFIterator(DOFIndexed<T> *obj, DOFIteratorType t = USED_DOFS) 
251
      : DOFIteratorBase(dynamic_cast<DOFAdmin*>(obj->getFeSpace()->getAdmin()), t),
252
	iteratedObject(obj)
253
    {}
254
255
256
257
258
259
    
    /// Constructs a DOFIterator for cont of type t
    DOFIterator(DOFIndexed<T>& obj, DOFIteratorType t = USED_DOFS) 
      : DOFIteratorBase(dynamic_cast<DOFAdmin*>(obj.getFeSpace()->getAdmin()), t),
	iteratedObject(&obj)
    {}
260

261
    /// Constructs a DOFIterator for cont of type t
262
263
    DOFIterator(DOFAdmin *admin,
		DOFIndexed<T> *obj, 
264
		DOFIteratorType t = USED_DOFS) 
265
266
      : DOFIteratorBase(admin, t),
	iteratedObject(obj)
267
    {}
268

269
    /// Dereference operator
270
    inline reference operator*() 
271
272
273
    { 
      return *it; 
    }
274

275
    /// Dereference operator
276
    inline pointer operator->() 
277
278
279
280
281
282
283
284
285
286
287
    { 
      return &(*it); 
    }

    inline bool operator!=(const DOFIterator<T>& rhs) 
    {
      if (this->iteratedObject != rhs.iteratedObject) 
	return true;

      if (this->it != rhs.it) 
	return true;
288
289

      return false;
290
    }
291

292
293
    inline bool operator==(const DOFIterator<T>& rhs) 
    {
294
295
296
297
      return !(this->operator==(rhs));
    }

  protected:
298
    /// Implementation of DOFIteratorBase::goToBeginOfIteratedObject()
299
300
301
302
    inline void goToBeginOfIteratedObject() 
    { 
      it = iteratedObject->begin(); 
    }
303

304
    /// Implementation of DOFIteratorBase::goToEndOfIteratedObject()
305
306
307
308
    inline void goToEndOfIteratedObject() 
    { 
      it = iteratedObject->end(); 
    }
309

310
    /// Implementation of DOFIteratorBase::incObjectIterator()
311
312
313
314
    inline void incObjectIterator() 
    { 
      ++it; 
    }
315

316
    /// Implementation of DOFIteratorBase::incObjectIterator()
317
318
319
320
    inline void decObjectIterator() 
    { 
      --it; 
    }
321
322

  protected:
323
    /// Object that is iterated
324
325
    DOFIndexed<T> *iteratedObject;

326
    /// Iterator for \ref iteratedObject
327
    typename std::vector<T>::iterator it;
328
  };
Praetorius, Simon's avatar
Praetorius, Simon committed
329
330
331
332
333
334
335
336
337
338
339
340
  
  

  /** \ingroup DOFAdministration
    * \brief
    * Implements a DOFIterator for a const DOFIndexed<T> object
    */
  template<typename T>
  class DOFConstIterator : public DOFIteratorBase
  {
  public:
    /// Constructs a DOFIterator for cont of type t
341
    DOFConstIterator(const DOFIndexed<T> *obj, DOFIteratorType t = USED_DOFS)
Praetorius, Simon's avatar
Praetorius, Simon committed
342
343
344
      : DOFIteratorBase(dynamic_cast<DOFAdmin*>(obj->getFeSpace()->getAdmin()), t),
	iteratedObject(obj)
    {}
345
346
347
348
349
350
    
    /// Constructs a DOFIterator for cont of type t
    DOFConstIterator(const DOFIndexed<T>& obj, DOFIteratorType t = USED_DOFS)
      : DOFIteratorBase(dynamic_cast<DOFAdmin*>(obj.getFeSpace()->getAdmin()), t),
	iteratedObject(&obj)
    {}
Praetorius, Simon's avatar
Praetorius, Simon committed
351
352
353
354

    /// Constructs a DOFIterator for cont of type t
    DOFConstIterator(DOFAdmin *admin,
		const DOFIndexed<T> *obj,
355
		DOFIteratorType t = USED_DOFS)
Praetorius, Simon's avatar
Praetorius, Simon committed
356
357
358
359
360
      : DOFIteratorBase(admin, t),
	iteratedObject(obj)
    {}

    /// Dereference operator
361
    inline const T& operator*() const
Praetorius, Simon's avatar
Praetorius, Simon committed
362
363
364
365
366
    {
      return *it;
    }

    /// Dereference operator
367
    inline const T* operator->() const
Praetorius, Simon's avatar
Praetorius, Simon committed
368
369
370
371
    {
      return &(*it);
    }

372
    inline bool operator!=(const DOFIterator<T>& rhs) const
Praetorius, Simon's avatar
Praetorius, Simon committed
373
374
375
376
377
378
379
380
381
382
    {
      if (this->iteratedObject != rhs.iteratedObject)
	return true;

      if (this->it != rhs.it)
	return true;

      return false;
    }

383
    inline bool operator==(const DOFIterator<T>& rhs) const
Praetorius, Simon's avatar
Praetorius, Simon committed
384
385
386
    {
      return !(this->operator==(rhs));
    }
387

Praetorius, Simon's avatar
Praetorius, Simon committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
  protected:
    /// Implementation of DOFIteratorBase::goToBeginOfIteratedObject()
    inline void goToBeginOfIteratedObject()
    {
      it = iteratedObject->begin();
    }

    /// Implementation of DOFIteratorBase::goToEndOfIteratedObject()
    inline void goToEndOfIteratedObject()
    {
      it = iteratedObject->end();
    }

    /// Implementation of DOFIteratorBase::incObjectIterator()
    inline void incObjectIterator()
    {
      ++it;
    }

    /// Implementation of DOFIteratorBase::incObjectIterator()
    inline void decObjectIterator()
    {
      --it;
    }

  protected:
    /// Object that is iterated
    const DOFIndexed<T> *iteratedObject;

    /// Iterator for \ref iteratedObject
    typename std::vector<T>::const_iterator it;
  };



  /** \ingroup DOFAdministration
    * \brief
    * Implements a DOFIterator for a vector of DOFVector<T> objects
    */
  template<typename T>
  class DOFVectorIterator : public DOFIteratorBase
  {
  public:
    typedef typename std::vector<T>::iterator VectorIterator;
    
    /// Constructs a DOFIterator for cont of type t
434
    DOFVectorIterator(std::vector<DOFVector<T>*> &obj, DOFIteratorType t = USED_DOFS) 
Praetorius, Simon's avatar
Praetorius, Simon committed
435
436
437
438
439
440
441
442
443
444
      : DOFIteratorBase(dynamic_cast<DOFAdmin*>(obj[0]->getFeSpace()->getAdmin()), t),
	iteratedObject(obj)
    {
      for (size_t i = 0; i < obj.size(); i++)
	it.push_back(new VectorIterator);
    }

    /// Constructs a DOFIterator for cont of type t
    DOFVectorIterator(DOFAdmin *admin,
		std::vector<DOFVector<T>*> &obj, 
445
		DOFIteratorType t = USED_DOFS) 
Praetorius, Simon's avatar
Praetorius, Simon committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
      : DOFIteratorBase(admin, t),
	iteratedObject(obj)
    {
      for (size_t i = 0; i < obj.size(); i++)
	it.push_back(new VectorIterator);
    }
    
    ~DOFVectorIterator()
    {
      for (size_t i = 0; i < it.size(); i++)
	delete it[i];
    }

    /// Dereference operator
    inline std::vector<T> operator*() 
    { 
      std::vector<T> result(it.size());
      for (size_t i = 0; i < it.size(); i++)
	result[i] = *(*it[i]);
      return result;
    }

    /// Dereference operator
    inline T* operator->() 
    { 
      throw std::runtime_error("operator-> not available for DOFVectorIterator!");
      return &(*(*it[0])); 
    }

    inline bool operator!=(const DOFVectorIterator<T>& rhs) 
    {
      if (this->iteratedObject != rhs.iteratedObject) 
	return true;

      if (this->it != rhs.it) 
	return true;

      return false;
    }

    inline bool operator==(const DOFVectorIterator<T>& rhs) 
    {
      return !(this->operator==(rhs));
    }

  protected:
    /// Implementation of DOFIteratorBase::goToBeginOfIteratedObject()
    inline void goToBeginOfIteratedObject() 
    { 
      for (size_t i = 0; i < it.size(); i++)
	*(it[i]) = iteratedObject[i]->begin(); 
    }

    /// Implementation of DOFIteratorBase::goToEndOfIteratedObject()
    inline void goToEndOfIteratedObject() 
    { 
      for (size_t i = 0; i < it.size(); i++)
	*(it[i]) = iteratedObject[i]->end(); 
    }

    /// Implementation of DOFIteratorBase::incObjectIterator()
    inline void incObjectIterator() 
    { 
      for (size_t i = 0; i < it.size(); i++)
	++(*(it[i])); 
    }

    /// Implementation of DOFIteratorBase::incObjectIterator()
    inline void decObjectIterator() 
    { 
      for (size_t i = 0; i < it.size(); i++)
	--(*(it[i]));
    }

  protected:
    /// Object that is iterated
    std::vector<DOFVector<T>*> iteratedObject;

    /// Iterator for \ref iteratedObject
    std::vector<typename std::vector<T>::iterator*> it;
  };
  
528
529
530
}

#endif // AMDIS_DOFITERATOR_H