DOFIterator.h 8.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file DOFIterator.h */

#ifndef AMDIS_DOFITERATOR_H
#define AMDIS_DOFITERATOR_H

#include "DOFAdmin.h"
#include "FiniteElemSpace.h"
27
#include "AMDiS_fwd.h"
28
29
30

namespace AMDiS {

31
  /// Possible types of DOFIterator
32
33
34
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
  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)
63
    {}
64

65
    virtual ~DOFIteratorBase() {}
66
67
68
69
70
71

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

79
      goToBeginOfIteratedObject();
80
81
      if (type != ALL_DOFS)
	if (*dofFreeIterator == (type == USED_DOFS))
82
	  operator++();
83
    }
84
85
86
87
88
89

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

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

140
    /// Postfix operator++.
141
142
    inline DOFIteratorBase operator++(int) 
    { 
143
144
145
      DOFIteratorBase clone = *this;
      operator++();
      return clone;
146
    }
147

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

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

      return *this;
165
    }
166

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

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

189
190
191
192
    inline bool begin() 
    { 
      return (dofFreeIterator == dofFree->begin()); 
    }
193

194
    /// Returns the current position index of this iterator
195
196
197
198
    inline int getDOFIndex() 
    { 
      return position; 
    }
199
200

  protected:
201
202
    /// Override this to enable iteration through the object
    virtual void goToBeginOfIteratedObject() {}
203

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

207
208
    /// Override this to enable iteration through the object
    virtual void incObjectIterator() {}
209
    
210
    virtual void decObjectIterator() {}
211
212

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


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

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

252
    /// Dereference operator
253
254
255
256
    inline T& operator*() 
    { 
      return *it; 
    }
257

258
    /// Dereference operator
259
260
261
262
263
264
265
266
267
268
269
270
    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;
271
272

      return false;
273
    }
274

275
276
    inline bool operator==(const DOFIterator<T>& rhs) 
    {
277
278
279
280
      return !(this->operator==(rhs));
    }

  protected:
281
    /// Implementation of DOFIteratorBase::goToBeginOfIteratedObject()
282
283
284
285
    inline void goToBeginOfIteratedObject() 
    { 
      it = iteratedObject->begin(); 
    }
286

287
    /// Implementation of DOFIteratorBase::goToEndOfIteratedObject()
288
289
290
291
    inline void goToEndOfIteratedObject() 
    { 
      it = iteratedObject->end(); 
    }
292

293
    /// Implementation of DOFIteratorBase::incObjectIterator()
294
295
296
297
    inline void incObjectIterator() 
    { 
      ++it; 
    }
298

299
    /// Implementation of DOFIteratorBase::incObjectIterator()
300
301
302
303
    inline void decObjectIterator() 
    { 
      --it; 
    }
304
305

  protected:
306
    /// Object that is iterated
307
308
    DOFIndexed<T> *iteratedObject;

309
    /// Iterator for \ref iteratedObject
310
    typename std::vector<T>::iterator it;
311
312
313
314
315
  };

}

#endif // AMDIS_DOFITERATOR_H