DOFIterator.h 7.85 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
57
58
59
60
61
62
63
64
65
  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
  {
  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)
66
    {}
67

68
    virtual ~DOFIteratorBase() {}
69
70
71
72
73
74

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

82
      goToBeginOfIteratedObject();
83
84
      if (type != ALL_DOFS)
	if (*dofFreeIterator == (type == USED_DOFS))
85
	  operator++();
86
    }
87
88
89
90
91
92

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

    /** \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.
     */
115
116
    inline const DOFIteratorBase& operator++() 
    {
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      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;      
141
    }
142

143
    /// Postfix operator++.
144
145
    inline DOFIteratorBase operator++(int) 
    { 
146
147
148
      DOFIteratorBase clone = *this;
      operator++();
      return clone;
149
    }
150

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

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

      return *this;
168
    }
169

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

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

192
193
194
195
    inline bool begin() 
    { 
      return (dofFreeIterator == dofFree->begin()); 
    }
196

197
    /// Returns the current position index of this iterator
198
199
200
201
    inline int getDOFIndex() 
    { 
      return position; 
    }
202
203

  protected:
204
205
    /// Override this to enable iteration through the object
    virtual void goToBeginOfIteratedObject() {}
206

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

210
211
    /// Override this to enable iteration through the object
    virtual void incObjectIterator() {}
212
    
213
    virtual void decObjectIterator() {}
214
215

  protected:
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    /// 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;
230
231
232
233
234
235
236
237
238
239
240
  };


  /** \ingroup DOFAdministration
   * \brief
   * Implements a DOFIterator for a DOFIndexed<T> object
   */
  template<typename T>
  class DOFIterator : public DOFIteratorBase
  {
  public:
241
    /// Constructs a DOFIterator for cont of type t
242
    DOFIterator(DOFIndexed<T> *obj, DOFIteratorType t) 
243
      : DOFIteratorBase(dynamic_cast<DOFAdmin*>(obj->getFeSpace()->getAdmin()), t),
244
	iteratedObject(obj)
245
    {}
246

247
    /// Constructs a DOFIterator for cont of type t
248
249
250
251
252
    DOFIterator(DOFAdmin *admin,
		DOFIndexed<T> *obj, 
		DOFIteratorType t) 
      : DOFIteratorBase(admin, t),
	iteratedObject(obj)
253
    {}
254

255
    /// Dereference operator
256
257
258
259
    inline T& operator*() 
    { 
      return *it; 
    }
260

261
    /// Dereference operator
262
263
264
265
266
267
268
269
270
271
272
273
    inline T* operator->() 
    { 
      return &(*it); 
    }

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

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

      return false;
276
    }
277

278
279
    inline bool operator==(const DOFIterator<T>& rhs) 
    {
280
281
282
283
      return !(this->operator==(rhs));
    }

  protected:
284
    /// Implementation of DOFIteratorBase::goToBeginOfIteratedObject()
285
286
287
288
    inline void goToBeginOfIteratedObject() 
    { 
      it = iteratedObject->begin(); 
    }
289

290
    /// Implementation of DOFIteratorBase::goToEndOfIteratedObject()
291
292
293
294
    inline void goToEndOfIteratedObject() 
    { 
      it = iteratedObject->end(); 
    }
295

296
    /// Implementation of DOFIteratorBase::incObjectIterator()
297
298
299
300
    inline void incObjectIterator() 
    { 
      ++it; 
    }
301

302
    /// Implementation of DOFIteratorBase::incObjectIterator()
303
304
305
306
    inline void decObjectIterator() 
    { 
      --it; 
    }
307
308

  protected:
309
    /// Object that is iterated
310
311
    DOFIndexed<T> *iteratedObject;

312
    /// Iterator for \ref iteratedObject
313
    typename std::vector<T>::iterator it;
314
315
316
317
318
  };

}

#endif // AMDIS_DOFITERATOR_H