Reference documentation for deal.II version 9.1.0-pre
tria_iterator.h
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_h
17 # define dealii_tria_iterator_h
18 
19 
20 /*---------------------------- tria-iterator.h ---------------------------*/
21 
22 
23 # include <deal.II/base/config.h>
24 
25 # include <deal.II/base/exceptions.h>
26 # include <deal.II/base/point.h>
27 
28 # include <deal.II/grid/tria_iterator_base.h>
29 
30 # include <iterator>
31 # include <ostream>
32 
33 DEAL_II_NAMESPACE_OPEN
34 
35 template <int dim, int spacedim>
36 class Triangulation;
37 template <int, int, int>
38 class TriaAccessorBase;
39 
40 template <typename>
41 class TriaIterator;
42 template <typename>
43 class TriaActiveIterator;
44 
45 
46 
47 // note: in non-debug mode, i.e. with optimizations, the file
48 // tria_iterator.templates.h is included at the end of this file.
49 // this includes a lot of templates and thus makes compilation
50 // slower, but at the same time allows for more aggressive
51 // inlining and thus faster code.
52 
53 
228 template <typename Accessor>
229 class TriaRawIterator
230 {
231 public:
237  using AccessorType = Accessor;
238 
242  TriaRawIterator();
243 
248 
261  explicit TriaRawIterator(const Accessor &a);
262 
267  template <typename OtherAccessor>
268  explicit TriaRawIterator(const OtherAccessor &a);
269 
277  const int level,
278  const int index,
279  const typename AccessorType::AccessorData *local_data = nullptr);
280 
290  template <typename OtherAccessor>
292 
299  const TriaAccessorBase<Accessor::structure_dimension,
300  Accessor::dimension,
301  Accessor::space_dimension> &tria_accessor,
302  const typename Accessor::AccessorData * local_data);
303 
308  template <typename OtherAccessor>
310 
315  template <typename OtherAccessor>
317 
334  const Accessor &operator*() const;
335 
339  Accessor &operator*();
340 
347  const Accessor *operator->() const;
348 
352  Accessor *operator->();
353 
354 
364  const Accessor &
365  access_any() const;
366 
373  operator=(const TriaRawIterator &);
374 
378  bool
379  operator==(const TriaRawIterator &) const;
380 
384  bool
385  operator!=(const TriaRawIterator &) const;
386 
412  bool
413  operator<(const TriaRawIterator &) const;
414 
419  bool
420  operator>(const TriaRawIterator &) const;
421 
430  operator++();
431 
444  operator++(int);
445 
451  operator--();
452 
462  operator--(int);
469  state() const;
470 
475  template <class StreamType>
476  void
477  print(StreamType &out) const;
478 
479 
484  std::size_t
485  memory_consumption() const;
486 
492  using iterator_category = std::bidirectional_iterator_tag;
493  using value_type = Accessor;
494  using difference_type = int;
495  using pointer = Accessor *;
496  using reference = Accessor &;
497 
504  Accessor,
505  << "You tried to dereference a cell iterator for which this "
506  << "is not possible. More information on this iterator: "
507  << "level=" << arg1.level() << ", index=" << arg1.index()
508  << ", state="
509  << (arg1.state() == IteratorState::valid ?
510  "valid" :
511  (arg1.state() == IteratorState::past_the_end ?
512  "past_the_end" :
513  "invalid")));
514 
519  Accessor,
520  << "You tried to dereference an iterator for which this "
521  << "is not possible. More information on this iterator: "
522  << "index=" << arg1.index() << ", state="
523  << (arg1.state() == IteratorState::valid ?
524  "valid" :
525  (arg1.state() == IteratorState::past_the_end ?
526  "past_the_end" :
527  "invalid")));
528 
537 
539 protected:
543  Accessor accessor;
544 
545 
555  template <typename SomeAccessor>
556  friend class TriaRawIterator;
557  template <typename SomeAccessor>
558  friend class TriaIterator;
559  template <typename SomeAccessor>
560  friend class TriaActiveIterator;
561 };
562 
563 
571 template <typename Accessor>
572 class TriaIterator : public TriaRawIterator<Accessor>
573 {
574 public:
578  TriaIterator();
579 
584 
593 
602  TriaIterator(
604  const int level,
605  const int index,
606  const typename Accessor::AccessorData *local_data = nullptr);
607 
612  template <typename OtherAccessor>
613  explicit TriaIterator(const OtherAccessor &a);
614 
624  template <typename OtherAccessor>
626 
632  TriaIterator(const TriaAccessorBase<Accessor::structure_dimension,
633  Accessor::dimension,
634  Accessor::space_dimension> &tria_accessor,
635  const typename Accessor::AccessorData * local_data);
636 
642  template <typename OtherAccessor>
644 
649  template <typename OtherAccessor>
651 
657 
664 
669  template <class OtherAccessor>
672 
678  template <class OtherAccessor>
681 
690  operator++();
691 
702  operator++(int);
703 
709  operator--();
710 
715  operator--(int);
723  using iterator_category =
725  using value_type = typename TriaRawIterator<Accessor>::value_type;
726  using pointer = typename TriaRawIterator<Accessor>::pointer;
727  using reference = typename TriaRawIterator<Accessor>::reference;
728  using difference_type = typename TriaRawIterator<Accessor>::difference_type;
729 
733  DeclException0(ExcAssignmentOfUnusedObject);
734 };
735 
736 
746 template <typename Accessor>
747 class TriaActiveIterator : public TriaIterator<Accessor>
748 {
749 public:
754 
759 
768 
777 
788  const int level,
789  const int index,
790  const typename Accessor::AccessorData *local_data = 0);
791 
801  template <typename OtherAccessor>
803 
810  const TriaAccessorBase<Accessor::structure_dimension,
811  Accessor::dimension,
812  Accessor::space_dimension> &tria_accessor,
813  const typename Accessor::AccessorData * local_data);
814 
822  template <typename OtherAccessor>
824 
830 
837 
844 
849  template <class OtherAccessor>
852 
858  template <class OtherAccessor>
861 
867  template <class OtherAccessor>
870 
877  operator++();
878 
891  operator++(int);
892 
898  operator--();
899 
904  operator--(int);
913  using value_type = typename TriaIterator<Accessor>::value_type;
914  using pointer = typename TriaIterator<Accessor>::pointer;
915  using reference = typename TriaIterator<Accessor>::reference;
916  using difference_type = typename TriaIterator<Accessor>::difference_type;
917 
921  DeclException0(ExcAssignmentOfInactiveObject);
922 };
923 
924 
925 /*----------------------- Inline functions -------------------*/
926 
927 
928 template <typename Accessor>
930  : accessor(a)
931 {}
932 
933 
934 
935 template <typename Accessor>
936 template <typename OtherAccessor>
937 inline TriaRawIterator<Accessor>::TriaRawIterator(const OtherAccessor &a)
938  : accessor(a)
939 {}
940 
941 
942 
943 template <typename Accessor>
944 template <typename OtherAccessor>
947  : accessor(i.accessor)
948 {}
949 
950 
951 
952 template <typename Accessor>
953 template <typename OtherAccessor>
956  : accessor(i.accessor)
957 {}
958 
959 
960 
961 template <typename Accessor>
962 template <typename OtherAccessor>
965  : accessor(i.accessor)
966 {}
967 
968 
969 
970 template <typename Accessor>
971 inline const Accessor &TriaRawIterator<Accessor>::operator*() const
972 {
973  Assert(Accessor::structure_dimension != Accessor::dimension ||
976  Assert(Accessor::structure_dimension == Accessor::dimension ||
979 
980  return accessor;
981 }
982 
983 
984 
985 template <typename Accessor>
987 {
988  Assert(Accessor::structure_dimension != Accessor::dimension ||
991  Assert(Accessor::structure_dimension == Accessor::dimension ||
994 
995  return accessor;
996 }
997 
998 
999 
1000 template <typename Accessor>
1001 inline const Accessor &
1003 {
1004  return accessor;
1005 }
1006 
1007 
1008 
1009 template <typename Accessor>
1010 inline const Accessor *TriaRawIterator<Accessor>::operator->() const
1011 {
1012  return &(this->operator*());
1013 }
1014 
1015 
1016 
1017 template <typename Accessor>
1019 {
1020  return &(this->operator*());
1021 }
1022 
1023 
1024 
1025 template <typename Accessor>
1028 {
1029  return accessor.state();
1030 }
1031 
1032 
1033 
1034 template <typename Accessor>
1035 inline bool
1038 {
1041  Assert(other.state() != IteratorState::invalid,
1043 
1044  Assert(&accessor.get_triangulation() == &other.accessor.get_triangulation(),
1046 
1047  // Deal with iterators past end
1049  return false;
1050  if (other.state() == IteratorState::past_the_end)
1051  return true;
1052 
1053  return ((**this) < (*other));
1054 }
1055 
1056 
1057 
1058 template <typename Accessor>
1059 inline bool
1062 {
1063  return (other < *this);
1064 }
1065 
1066 
1067 
1068 template <typename Accessor>
1071 {
1073 
1074  ++accessor;
1075  return *this;
1076 }
1077 
1078 
1079 
1080 template <typename Accessor>
1083 {
1085 
1086  --accessor;
1087  return *this;
1088 }
1089 
1090 
1091 
1092 template <typename Accessor>
1093 template <class StreamType>
1094 inline void
1095 TriaRawIterator<Accessor>::print(StreamType &out) const
1096 {
1097  if (Accessor::structure_dimension == Accessor::dimension)
1098  out << accessor.level() << "." << accessor.index();
1099  else
1100  out << accessor.index();
1101 }
1102 
1103 
1104 
1105 template <typename Accessor>
1106 inline std::size_t
1108 {
1109  return sizeof(TriaRawIterator<Accessor>);
1110 }
1111 
1112 
1113 
1114 template <typename Accessor>
1115 template <typename OtherAccessor>
1117  const TriaIterator<OtherAccessor> &i)
1118  : TriaRawIterator<Accessor>(i.accessor)
1119 {}
1120 
1121 
1122 
1123 template <typename Accessor>
1124 template <typename OtherAccessor>
1127  : TriaRawIterator<Accessor>(i.accessor)
1128 {}
1129 
1130 
1131 
1132 template <typename Accessor>
1133 template <typename OtherAccessor>
1136  : TriaRawIterator<Accessor>(i.accessor)
1137 {
1138 # ifdef DEBUG
1139  // do this like this, because:
1140  // if we write
1141  // "Assert (IteratorState::past_the_end || used)"
1142  // used() is called anyway, even if
1143  // state==IteratorState::past_the_end, and will then
1144  // throw the exception!
1145  if (this->state() != IteratorState::past_the_end)
1146  Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
1147 # endif
1148 }
1149 
1150 template <typename Accessor>
1151 template <typename OtherAccessor>
1153  : TriaRawIterator<Accessor>(a)
1154 {
1155 # ifdef DEBUG
1156  // do this like this, because:
1157  // if we write
1158  // "Assert (IteratorState::past_the_end || used)"
1159  // used() is called anyway, even if
1160  // state==IteratorState::past_the_end, and will then
1161  // throw the exception!
1162  if (this->state() != IteratorState::past_the_end)
1163  Assert(this->accessor.used(), ExcAssignmentOfUnusedObject());
1164 # endif
1165 }
1166 
1167 template <typename Accessor>
1168 template <typename OtherAccessor>
1171  : TriaIterator<Accessor>(i.accessor)
1172 {}
1173 
1174 
1175 
1176 template <typename Accessor>
1177 template <typename OtherAccessor>
1180  : TriaIterator<Accessor>(i)
1181 {
1182 # ifdef DEBUG
1183  // do this like this, because:
1184  // if we write
1185  // "Assert (IteratorState::past_the_end || !has_children())"
1186  // has_children() is called anyway, even if
1187  // state==IteratorState::past_the_end, and will then
1188  // throw the exception!
1189  if (this->state() != IteratorState::past_the_end)
1190  Assert(this->accessor.has_children() == false,
1192 # endif
1193 }
1194 
1195 
1196 
1204 template <typename Accessor>
1205 inline std::ostream &
1206 operator<<(std::ostream &out, const TriaRawIterator<Accessor> &i)
1207 {
1208  i.print(out);
1209  return out;
1210 }
1211 
1212 
1213 
1221 template <typename Accessor>
1222 inline std::ostream &
1223 operator<<(std::ostream &out, const TriaIterator<Accessor> &i)
1224 {
1225  i.print(out);
1226  return out;
1227 }
1228 
1229 
1230 
1238 template <typename Accessor>
1239 inline std::ostream &
1240 operator<<(std::ostream &out, const TriaActiveIterator<Accessor> &i)
1241 {
1242  i.print(out);
1243  return out;
1244 }
1245 
1246 
1247 DEAL_II_NAMESPACE_CLOSE
1248 
1249 
1250 // if in optimized mode: include more templates
1251 # ifndef DEBUG
1252 # include "tria_iterator.templates.h"
1253 # endif
1254 
1255 
1256 /*---------------------------- tria-iterator.h ---------------------------*/
1257 #endif
1258 /*---------------------------- tria-iterator.h ---------------------------*/
Iterator is invalid, probably due to an error.
static::ExceptionBase & ExcInvalidComparison()
static::ExceptionBase & ExcAdvanceInvalidObject()
static::ExceptionBase & ExcAssignmentOfInactiveObject()
void print(StreamType &out) const
const Accessor * operator->() const
const Accessor & operator*() const
std::size_t memory_consumption() const
typename TriaRawIterator< Accessor >::iterator_category iterator_category
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:408
Accessor AccessorType
#define Assert(cond, exc)
Definition: exceptions.h:1227
TriaRawIterator & operator++()
#define DeclException0(Exception0)
Definition: exceptions.h:385
static::ExceptionBase & ExcDereferenceInvalidObject(Accessor arg1)
IteratorState::IteratorStates state() const
TriaRawIterator & operator--()
static::ExceptionBase & ExcAssignmentOfUnusedObject()
static::ExceptionBase & ExcDereferenceInvalidCell(Accessor arg1)
bool operator!=(const TriaRawIterator &) const
TriaRawIterator & operator=(const TriaRawIterator &)
Iterator reached end of container.
Iterator points to a valid object.
bool operator==(const TriaRawIterator &) const
const Accessor & access_any() const
std::bidirectional_iterator_tag iterator_category
bool operator>(const TriaRawIterator &) const
bool operator<(const TriaRawIterator &) const