1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_tria_iterator_templates_h
17 #define dealii_tria_iterator_templates_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/grid/tria.h>
23 #include <deal.II/grid/tria_iterator.h>
24 
25 DEAL_II_NAMESPACE_OPEN
26 
27 
28 /* Note: This file only contains template definitions and will thus
29    not produce an object file. It is rather thought to be included
30    into the *_accessor.cc files.
31 */
32 
33 
34 /*------------------------ Functions: TriaRawIterator ------------------*/
35 
36 
37 template <typename Accessor>
TriaRawIterator()38 inline TriaRawIterator<Accessor>::TriaRawIterator()
39   : accessor(nullptr, -2, -2, nullptr)
40 {}
41 
42 
43 template <typename Accessor>
TriaRawIterator(const TriaRawIterator<Accessor> & i)44 inline TriaRawIterator<Accessor>::TriaRawIterator(
45   const TriaRawIterator<Accessor> &i)
46   : accessor(i.accessor)
47 {}
48 
49 
50 
51 template <typename Accessor>
TriaRawIterator(const Triangulation<Accessor::dimension,Accessor::space_dimension> * parent,const int level,const int index,const typename Accessor::AccessorData * local_data)52 inline TriaRawIterator<Accessor>::TriaRawIterator(
53   const Triangulation<Accessor::dimension, Accessor::space_dimension> *parent,
54   const int                                                            level,
55   const int                                                            index,
56   const typename Accessor::AccessorData *local_data)
57   : accessor(parent, level, index, local_data)
58 {}
59 
60 
61 template <typename Accessor>
TriaRawIterator(const TriaAccessorBase<Accessor::structure_dimension,Accessor::dimension,Accessor::space_dimension> & tria_accessor,const typename Accessor::AccessorData * local_data)62 inline TriaRawIterator<Accessor>::TriaRawIterator(
63   const TriaAccessorBase<Accessor::structure_dimension,
64                          Accessor::dimension,
65                          Accessor::space_dimension> &tria_accessor,
66   const typename Accessor::AccessorData *            local_data)
67   : accessor(nullptr, -2, -2, local_data)
68 {
69   accessor.copy_from(tria_accessor);
70 }
71 
72 
73 template <typename Accessor>
74 inline TriaRawIterator<Accessor> &
75 TriaRawIterator<Accessor>::operator=(const TriaRawIterator<Accessor> &i)
76 {
77   accessor.copy_from(i.accessor);
78 
79   return *this;
80 }
81 
82 
83 
84 template <typename Accessor>
85 template <typename OtherAccessor>
86 inline
87   typename std::enable_if<std::is_convertible<OtherAccessor, Accessor>::value,
88                           bool>::type
89   TriaRawIterator<Accessor>::
90   operator==(const TriaRawIterator<OtherAccessor> &other) const
91 {
92   return accessor == other.accessor;
93 }
94 
95 
96 template <typename Accessor>
97 inline bool
98 TriaRawIterator<Accessor>::
99 operator!=(const TriaRawIterator<Accessor> &other) const
100 {
101   return !(*this == other);
102 }
103 
104 
105 template <typename Accessor>
106 inline TriaRawIterator<Accessor>
107 TriaRawIterator<Accessor>::operator++(int)
108 {
109   TriaRawIterator<Accessor> tmp(*this);
110                             operator++();
111 
112   return tmp;
113 }
114 
115 
116 template <typename Accessor>
117 inline TriaRawIterator<Accessor>
118 TriaRawIterator<Accessor>::operator--(int)
119 {
120   TriaRawIterator<Accessor> tmp(*this);
121                             operator--();
122 
123   return tmp;
124 }
125 
126 
127 /*-----------------------  functions: TriaIterator ---------------*/
128 
129 
130 template <typename Accessor>
TriaIterator()131 inline TriaIterator<Accessor>::TriaIterator()
132   : TriaRawIterator<Accessor>()
133 {}
134 
135 
136 template <typename Accessor>
TriaIterator(const TriaIterator<Accessor> & i)137 inline TriaIterator<Accessor>::TriaIterator(const TriaIterator<Accessor> &i)
138   : TriaRawIterator<Accessor>(i.accessor)
139 {}
140 
141 
142 template <typename Accessor>
TriaIterator(const TriaRawIterator<Accessor> & i)143 inline TriaIterator<Accessor>::TriaIterator(const TriaRawIterator<Accessor> &i)
144   : TriaRawIterator<Accessor>(i.accessor)
145 {
146 #ifdef DEBUG
147   // do this like this, because:
148   // if we write
149   // "Assert (IteratorState::past_the_end || used)"
150   // used() is called anyway, even if
151   // state==IteratorState::past_the_end, and will then
152   // throw the exception!
153   if (this->state() != IteratorState::past_the_end)
154     Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
155 #endif
156 }
157 
158 
159 template <typename Accessor>
TriaIterator(const Triangulation<Accessor::dimension,Accessor::space_dimension> * parent,const int level,const int index,const typename Accessor::AccessorData * local_data)160 inline TriaIterator<Accessor>::TriaIterator(
161   const Triangulation<Accessor::dimension, Accessor::space_dimension> *parent,
162   const int                                                            level,
163   const int                                                            index,
164   const typename Accessor::AccessorData *local_data)
165   : TriaRawIterator<Accessor>(parent, level, index, local_data)
166 {
167 #ifdef DEBUG
168   // do this like this, because:
169   // if we write
170   // "Assert (IteratorState::past_the_end || used)"
171   // used() is called anyway, even if
172   // state==IteratorState::past_the_end, and will then
173   // throw the exception!
174   if (this->state() != IteratorState::past_the_end)
175     Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
176 #endif
177 }
178 
179 
180 template <typename Accessor>
TriaIterator(const TriaAccessorBase<Accessor::structure_dimension,Accessor::dimension,Accessor::space_dimension> & tria_accessor,const typename Accessor::AccessorData * local_data)181 inline TriaIterator<Accessor>::TriaIterator(
182   const TriaAccessorBase<Accessor::structure_dimension,
183                          Accessor::dimension,
184                          Accessor::space_dimension> &tria_accessor,
185   const typename Accessor::AccessorData *            local_data)
186   : TriaRawIterator<Accessor>(tria_accessor, local_data)
187 {
188 #ifdef DEBUG
189   // do this like this, because:
190   // if we write
191   // "Assert (IteratorState::past_the_end || used)"
192   // used() is called anyway, even if
193   // state==IteratorState::past_the_end, and will then
194   // throw the exception!
195   if (this->state() != IteratorState::past_the_end)
196     Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
197 #endif
198 }
199 
200 
201 template <typename Accessor>
202 inline TriaIterator<Accessor> &
203 TriaIterator<Accessor>::operator=(const TriaIterator<Accessor> &i)
204 {
205   this->accessor.copy_from(i.accessor);
206   return *this;
207 }
208 
209 
210 template <typename Accessor>
211 template <typename OtherAccessor>
212 inline TriaIterator<Accessor> &
213 TriaIterator<Accessor>::operator=(const TriaIterator<OtherAccessor> &i)
214 {
215   this->accessor.copy_from(i.accessor);
216   return *this;
217 }
218 
219 
220 template <typename Accessor>
221 inline TriaIterator<Accessor> &
222 TriaIterator<Accessor>::operator=(const TriaRawIterator<Accessor> &i)
223 {
224   this->accessor.copy_from(i.accessor);
225 #ifdef DEBUG
226   // do this like this, because:
227   // if we write
228   // "Assert (IteratorState::past_the_end || used)"
229   // used() is called anyway, even if
230   // state==IteratorState::past_the_end, and will then
231   // throw the exception!
232   if (this->state() != IteratorState::past_the_end)
233     Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
234 #endif
235   return *this;
236 }
237 
238 
239 template <typename Accessor>
240 template <typename OtherAccessor>
241 inline TriaIterator<Accessor> &
242 TriaIterator<Accessor>::operator=(const TriaRawIterator<OtherAccessor> &i)
243 {
244   this->accessor.copy_from(i.accessor);
245 #ifdef DEBUG
246   // do this like this, because:
247   // if we write
248   // "Assert (IteratorState::past_the_end || used)"
249   // used() is called anyway, even if
250   // state==IteratorState::past_the_end, and will then
251   // throw the exception!
252   if (this->state() != IteratorState::past_the_end)
253     Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
254 #endif
255   return *this;
256 }
257 
258 
259 template <typename Accessor>
260 inline TriaIterator<Accessor> &
261 TriaIterator<Accessor>::operator++()
262 {
263   while (TriaRawIterator<Accessor>::operator++(),
264          (this->state() == IteratorState::valid))
265     if (this->accessor.used() == true)
266       return *this;
267   return *this;
268 }
269 
270 
271 template <typename Accessor>
272 inline TriaIterator<Accessor>
273 TriaIterator<Accessor>::operator++(int)
274 {
275   TriaIterator<Accessor> tmp(*this);
276                          operator++();
277 
278   return tmp;
279 }
280 
281 
282 template <typename Accessor>
283 inline TriaIterator<Accessor> &
284 TriaIterator<Accessor>::operator--()
285 {
286   while (TriaRawIterator<Accessor>::operator--(),
287          (this->state() == IteratorState::valid))
288     if (this->accessor.used() == true)
289       return *this;
290   return *this;
291 }
292 
293 
294 template <typename Accessor>
295 inline TriaIterator<Accessor>
296 TriaIterator<Accessor>::operator--(int)
297 {
298   TriaIterator<Accessor> tmp(*this);
299                          operator--();
300 
301   return tmp;
302 }
303 
304 
305 /*-----------------------  functions: TriaActiveIterator ---------------*/
306 
307 
308 template <typename Accessor>
TriaActiveIterator()309 inline TriaActiveIterator<Accessor>::TriaActiveIterator()
310   : TriaIterator<Accessor>()
311 {}
312 
313 
314 template <typename Accessor>
TriaActiveIterator(const TriaActiveIterator<Accessor> & i)315 inline TriaActiveIterator<Accessor>::TriaActiveIterator(
316   const TriaActiveIterator<Accessor> &i)
317   : TriaIterator<Accessor>(static_cast<TriaIterator<Accessor>>(i))
318 {}
319 
320 
321 template <typename Accessor>
TriaActiveIterator(const TriaRawIterator<Accessor> & i)322 inline TriaActiveIterator<Accessor>::TriaActiveIterator(
323   const TriaRawIterator<Accessor> &i)
324   : TriaIterator<Accessor>(i)
325 {
326 #ifdef DEBUG
327   // do this like this, because:
328   // if we write
329   // "Assert (IteratorState::past_the_end || !has_children())"
330   // has_children() is called anyway, even if
331   // state==IteratorState::past_the_end, and will then
332   // throw the exception!
333   if (this->state() != IteratorState::past_the_end)
334     Assert(this->accessor.has_children() == false,
335            ExcAssignmentOfInactiveObject());
336 #endif
337 }
338 
339 
340 template <typename Accessor>
TriaActiveIterator(const TriaIterator<Accessor> & i)341 inline TriaActiveIterator<Accessor>::TriaActiveIterator(
342   const TriaIterator<Accessor> &i)
343   : TriaIterator<Accessor>(i)
344 {
345 #ifdef DEBUG
346   // do this like this, because:
347   // if we write
348   // "Assert (IteratorState::past_the_end || !has_children())"
349   // has_children() is called anyway, even if
350   // state==IteratorState::past_the_end, and will then
351   // throw the exception!
352   if (this->state() != IteratorState::past_the_end)
353     Assert(this->accessor.has_children() == false,
354            ExcAssignmentOfInactiveObject());
355 #endif
356 }
357 
358 
359 template <typename Accessor>
TriaActiveIterator(const Triangulation<Accessor::dimension,Accessor::space_dimension> * parent,const int level,const int index,const typename Accessor::AccessorData * local_data)360 inline TriaActiveIterator<Accessor>::TriaActiveIterator(
361   const Triangulation<Accessor::dimension, Accessor::space_dimension> *parent,
362   const int                                                            level,
363   const int                                                            index,
364   const typename Accessor::AccessorData *local_data)
365   : TriaIterator<Accessor>(parent, level, index, local_data)
366 {
367 #ifdef DEBUG
368   // do this like this, because:
369   // if we write
370   // "Assert (IteratorState::past_the_end || !has_children())"
371   // has_children() is called anyway, even if
372   // state==IteratorState::past_the_end, and will then
373   // throw the exception!
374   if (this->state() != IteratorState::past_the_end)
375     Assert(this->accessor.has_children() == false,
376            ExcAssignmentOfInactiveObject());
377 #endif
378 }
379 
380 
381 template <typename Accessor>
TriaActiveIterator(const TriaAccessorBase<Accessor::structure_dimension,Accessor::dimension,Accessor::space_dimension> & tria_accessor,const typename Accessor::AccessorData * local_data)382 inline TriaActiveIterator<Accessor>::TriaActiveIterator(
383   const TriaAccessorBase<Accessor::structure_dimension,
384                          Accessor::dimension,
385                          Accessor::space_dimension> &tria_accessor,
386   const typename Accessor::AccessorData *            local_data)
387   : TriaIterator<Accessor>(tria_accessor, local_data)
388 {
389 #ifdef DEBUG
390   // do this like this, because:
391   // if we write
392   // "Assert (IteratorState::past_the_end || !has_children())"
393   // has_children() is called anyway, even if
394   // state==IteratorState::past_the_end, and will then
395   // throw the exception!
396   if (this->state() != IteratorState::past_the_end)
397     Assert(this->accessor.has_children() == false,
398            ExcAssignmentOfInactiveObject());
399 #endif
400 }
401 
402 
403 template <typename Accessor>
404 inline TriaActiveIterator<Accessor> &
405 TriaActiveIterator<Accessor>::operator=(const TriaActiveIterator<Accessor> &i)
406 {
407   this->accessor.copy_from(i.accessor);
408   return *this;
409 }
410 
411 
412 template <typename Accessor>
413 template <class OtherAccessor>
414 inline TriaActiveIterator<Accessor> &
415 TriaActiveIterator<Accessor>::
416 operator=(const TriaActiveIterator<OtherAccessor> &i)
417 {
418   this->accessor.copy_from(i.accessor);
419   return *this;
420 }
421 
422 
423 template <typename Accessor>
424 inline TriaActiveIterator<Accessor> &
425 TriaActiveIterator<Accessor>::operator=(const TriaRawIterator<Accessor> &i)
426 {
427   this->accessor.copy_from(i.accessor);
428 #ifdef DEBUG
429   // do this like this, because:
430   // if we write
431   // "Assert (IteratorState::past_the_end || !has_children())"
432   // has_children() is called anyway, even if
433   // state==IteratorState::past_the_end, and will then
434   // throw the exception!
435   if (this->state() != IteratorState::past_the_end)
436     Assert(this->accessor.used() && this->accessor.has_children() == false,
437            ExcAssignmentOfInactiveObject());
438 #endif
439   return *this;
440 }
441 
442 
443 template <typename Accessor>
444 template <class OtherAccessor>
445 inline TriaActiveIterator<Accessor> &
446 TriaActiveIterator<Accessor>::operator=(const TriaRawIterator<OtherAccessor> &i)
447 {
448   this->accessor.copy_from(i.accessor);
449 #ifdef DEBUG
450   // do this like this, because:
451   // if we write
452   // "Assert (IteratorState::past_the_end || !has_children())"
453   // has_children() is called anyway, even if
454   // state==IteratorState::past_the_end, and will then
455   // throw the exception!
456   if (this->state() != IteratorState::past_the_end)
457     Assert(this->accessor.used() && this->accessor.has_children() == false,
458            ExcAssignmentOfInactiveObject());
459 #endif
460   return *this;
461 }
462 
463 
464 template <typename Accessor>
465 template <class OtherAccessor>
466 inline TriaActiveIterator<Accessor> &
467 TriaActiveIterator<Accessor>::operator=(const TriaIterator<OtherAccessor> &i)
468 {
469   this->accessor.copy_from(i.accessor);
470 #ifdef DEBUG
471   // do this like this, because:
472   // if we write
473   // "Assert (IteratorState::past_the_end || !has_children())"
474   // has_children() is called anyway, even if
475   // state==IteratorState::past_the_end, and will then
476   // throw the exception!
477   if (this->state() != IteratorState::past_the_end)
478     Assert(this->accessor.has_children() == false,
479            ExcAssignmentOfInactiveObject());
480 #endif
481   return *this;
482 }
483 
484 
485 template <typename Accessor>
486 inline TriaActiveIterator<Accessor> &
487 TriaActiveIterator<Accessor>::operator=(const TriaIterator<Accessor> &i)
488 {
489   this->accessor.copy_from(i.accessor);
490 #ifdef DEBUG
491   // do this like this, because:
492   // if we write
493   // "Assert (IteratorState::past_the_end || !has_children())"
494   // has_children() is called anyway, even if
495   // state==IteratorState::past_the_end, and will then
496   // throw the exception!
497   if (this->state() != IteratorState::past_the_end)
498     Assert(this->accessor.has_children() == false,
499            ExcAssignmentOfInactiveObject());
500 #endif
501   return *this;
502 }
503 
504 
505 template <typename Accessor>
506 inline TriaActiveIterator<Accessor> &
507 TriaActiveIterator<Accessor>::operator++()
508 {
509   while (TriaIterator<Accessor>::operator++(),
510          (this->state() == IteratorState::valid))
511     if (this->accessor.has_children() == false)
512       return *this;
513   return *this;
514 }
515 
516 
517 template <typename Accessor>
518 inline TriaActiveIterator<Accessor>
519 TriaActiveIterator<Accessor>::operator++(int)
520 {
521   TriaActiveIterator<Accessor> tmp(*this);
522                                operator++();
523 
524   return tmp;
525 }
526 
527 
528 template <typename Accessor>
529 inline TriaActiveIterator<Accessor> &
530 TriaActiveIterator<Accessor>::operator--()
531 {
532   while (TriaIterator<Accessor>::operator--(),
533          (this->state() == IteratorState::valid))
534     if (this->accessor.has_children() == false)
535       return *this;
536   return *this;
537 }
538 
539 
540 template <typename Accessor>
541 inline TriaActiveIterator<Accessor>
542 TriaActiveIterator<Accessor>::operator--(int)
543 {
544   TriaActiveIterator<Accessor> tmp(*this);
545                                operator--();
546 
547   return tmp;
548 }
549 
550 DEAL_II_NAMESPACE_CLOSE
551 
552 #endif
553