Reference documentation for deal.II version 9.0.0
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 at
12 // the top level of the deal.II distribution.
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 #include <deal.II/base/exceptions.h>
25 
26 #include <deal.II/base/point.h>
27 #include <deal.II/grid/tria_iterator_base.h>
28 
29 #include <iterator>
30 
31 #include <ostream>
32 
33 DEAL_II_NAMESPACE_OPEN
34 
35 template <int dim, int spacedim> class Triangulation;
36 template <int, int, int> class TriaAccessorBase;
37 
38 template <typename> class TriaIterator;
39 template <typename> class TriaActiveIterator;
40 
41 
42 
43 // note: in non-debug mode, i.e. with optimizations, the file
44 // tria_iterator.templates.h is included at the end of this file.
45 // this includes a lot of templates and thus makes compilation
46 // slower, but at the same time allows for more aggressive
47 // inlining and thus faster code.
48 
49 
224 template <typename Accessor>
225 class TriaRawIterator
226 {
227 public:
233  typedef Accessor AccessorType;
234 
238  TriaRawIterator ();
239 
244 
258  explicit TriaRawIterator (const Accessor &a);
259 
264  template <typename OtherAccessor>
265  explicit TriaRawIterator (const OtherAccessor &a);
266 
273  const int level,
274  const int index,
275  const typename AccessorType::AccessorData *local_data = nullptr);
276 
286  template <typename OtherAccessor>
288 
295  const typename Accessor::AccessorData *local_data);
296 
301  template <typename OtherAccessor>
303 
308  template <typename OtherAccessor>
310 
327  const Accessor &operator * () const;
328 
332  Accessor &operator * ();
333 
340  const Accessor *operator -> () const;
341 
345  Accessor *operator -> ();
346 
347 
357  const Accessor &access_any () const;
358 
365 
369  bool operator == (const TriaRawIterator &) const;
370 
374  bool operator != (const TriaRawIterator &) const;
375 
401  bool operator < (const TriaRawIterator &) const;
402 
407  bool operator > (const TriaRawIterator &) const;
408 
417 
430 
436 
452 
457  template <class StreamType>
458  void print (StreamType &out) const;
459 
460 
465  std::size_t memory_consumption () const;
466 
472  typedef std::bidirectional_iterator_tag iterator_category;
473  typedef Accessor value_type;
474  typedef int difference_type;
475  typedef Accessor *pointer;
476  typedef Accessor &reference;
477 
484  Accessor,
485  << "You tried to dereference a cell iterator for which this "
486  << "is not possible. More information on this iterator: "
487  << "level=" << arg1.level()
488  << ", index=" << arg1.index()
489  << ", state="
490  << (arg1.state() == IteratorState::valid ? "valid" :
491  (arg1.state() == IteratorState::past_the_end ?
492  "past_the_end" : "invalid")));
493 
498  Accessor,
499  << "You tried to dereference an iterator for which this "
500  << "is not possible. More information on this iterator: "
501  << "index=" << arg1.index()
502  << ", state="
503  << (arg1.state() == IteratorState::valid ? "valid" :
504  (arg1.state() == IteratorState::past_the_end ?
505  "past_the_end" : "invalid")));
506 
515 
517 protected:
521  Accessor accessor;
522 
523 
533  template <typename SomeAccessor> friend class TriaRawIterator;
534  template <typename SomeAccessor> friend class TriaIterator;
535  template <typename SomeAccessor> friend class TriaActiveIterator;
536 };
537 
538 
546 template <typename Accessor>
547 class TriaIterator : public TriaRawIterator<Accessor>
548 {
549 public:
553  TriaIterator ();
554 
559 
568 
578  const int level,
579  const int index,
580  const typename Accessor::AccessorData *local_data = nullptr);
581 
586  template <typename OtherAccessor>
587  explicit TriaIterator (const OtherAccessor &a);
588 
598  template <typename OtherAccessor>
600 
607  const typename Accessor::AccessorData *local_data);
608 
614  template <typename OtherAccessor>
616 
621  template <typename OtherAccessor>
623 
629 
636 
641  template <class OtherAccessor>
644 
650  template <class OtherAccessor>
653 
662 
673 
679 
692  typedef typename TriaRawIterator<Accessor>::value_type value_type;
693  typedef typename TriaRawIterator<Accessor>::pointer pointer;
694  typedef typename TriaRawIterator<Accessor>::reference reference;
695  typedef typename TriaRawIterator<Accessor>::difference_type difference_type;
696 
701 };
702 
703 
713 template <typename Accessor>
714 class TriaActiveIterator : public TriaIterator<Accessor>
715 {
716 public:
721 
726 
735 
744 
754  const int level,
755  const int index,
756  const typename Accessor::AccessorData *local_data = 0);
757 
767  template <typename OtherAccessor>
769 
776  const typename Accessor::AccessorData *local_data);
777 
785  template <typename OtherAccessor>
787 
793 
800 
807 
812  template <class OtherAccessor>
815 
821  template <class OtherAccessor>
824 
830  template <class OtherAccessor>
833 
840 
853 
859 
872  typedef typename TriaIterator<Accessor>::value_type value_type;
873  typedef typename TriaIterator<Accessor>::pointer pointer;
874  typedef typename TriaIterator<Accessor>::reference reference;
875  typedef typename TriaIterator<Accessor>::difference_type difference_type;
876 
881 };
882 
883 
884 /*----------------------- Inline functions -------------------*/
885 
886 
887 template <typename Accessor>
888 inline
890 TriaRawIterator (const Accessor &a)
891  :
892  accessor (a)
893 {}
894 
895 
896 
897 template <typename Accessor>
898 template <typename OtherAccessor>
899 inline
901 TriaRawIterator (const OtherAccessor &a)
902  :
903  accessor (a)
904 {}
905 
906 
907 
908 template <typename Accessor>
909 template <typename OtherAccessor>
910 inline
913  :
914  accessor (i.accessor)
915 {}
916 
917 
918 
919 template <typename Accessor>
920 template <typename OtherAccessor>
921 inline
924  :
925  accessor (i.accessor)
926 {}
927 
928 
929 
930 template <typename Accessor>
931 template <typename OtherAccessor>
932 inline
935  :
936  accessor (i.accessor)
937 {}
938 
939 
940 
941 template <typename Accessor>
942 inline
943 const Accessor &
945 {
946  Assert (Accessor::structure_dimension!=Accessor::dimension ||
947  state() == IteratorState::valid,
948  ExcDereferenceInvalidCell(accessor));
949  Assert (Accessor::structure_dimension==Accessor::dimension ||
950  state() == IteratorState::valid,
951  ExcDereferenceInvalidObject(accessor));
952 
953  return accessor;
954 }
955 
956 
957 
958 template <typename Accessor>
959 inline
960 Accessor &
962 {
963  Assert (Accessor::structure_dimension!=Accessor::dimension ||
964  state() == IteratorState::valid,
965  ExcDereferenceInvalidCell(accessor));
966  Assert (Accessor::structure_dimension==Accessor::dimension ||
967  state() == IteratorState::valid,
968  ExcDereferenceInvalidObject(accessor));
969 
970  return accessor;
971 }
972 
973 
974 
975 template <typename Accessor>
976 inline
977 const Accessor &
979 {
980  return accessor;
981 }
982 
983 
984 
985 template <typename Accessor>
986 inline
987 const Accessor *
989 {
990  return &(this->operator* ());
991 }
992 
993 
994 
995 template <typename Accessor>
996 inline
997 Accessor *
999 {
1000  return &(this->operator* ());
1001 }
1002 
1003 
1004 
1005 template <typename Accessor>
1006 inline
1009 {
1010  return accessor.state ();
1011 }
1012 
1013 
1014 
1015 template <typename Accessor>
1016 inline
1017 bool
1019 {
1022 
1023  Assert (&accessor.get_triangulation() == &other.accessor.get_triangulation(),
1024  ExcInvalidComparison());
1025 
1026  // Deal with iterators past end
1027  if (state()==IteratorState::past_the_end)
1028  return false;
1029  if (other.state()==IteratorState::past_the_end)
1030  return true;
1031 
1032  return ((**this) < (*other));
1033 }
1034 
1035 
1036 
1037 template <typename Accessor>
1038 inline
1039 bool
1041 {
1042  return (other < *this);
1043 }
1044 
1045 
1046 
1047 template <typename Accessor>
1048 inline
1051 {
1052  Assert (state() == IteratorState::valid, ExcAdvanceInvalidObject());
1053 
1054  ++accessor;
1055  return *this;
1056 }
1057 
1058 
1059 
1060 template <typename Accessor>
1061 inline
1064 {
1065  Assert (state() == IteratorState::valid, ExcAdvanceInvalidObject());
1066 
1067  --accessor;
1068  return *this;
1069 }
1070 
1071 
1072 
1073 template <typename Accessor>
1074 template <class StreamType>
1075 inline
1076 void
1077 TriaRawIterator<Accessor>::print (StreamType &out) const
1078 {
1079  if (Accessor::structure_dimension==Accessor::dimension)
1080  out << accessor.level() << "." << accessor.index();
1081  else
1082  out << accessor.index();
1083 }
1084 
1085 
1086 
1087 template <typename Accessor>
1088 inline
1089 std::size_t
1091 {
1092  return sizeof(TriaRawIterator<Accessor>);
1093 }
1094 
1095 
1096 
1097 template <typename Accessor>
1098 template <typename OtherAccessor>
1099 inline
1101  :
1102  TriaRawIterator<Accessor> (i.accessor)
1103 {}
1104 
1105 
1106 
1107 template <typename Accessor>
1108 template <typename OtherAccessor>
1109 inline
1111  :
1112  TriaRawIterator<Accessor> (i.accessor)
1113 {}
1114 
1115 
1116 
1117 template <typename Accessor>
1118 template <typename OtherAccessor>
1119 inline
1121  :
1122  TriaRawIterator<Accessor> (i.accessor)
1123 {
1124 #ifdef DEBUG
1125  // do this like this, because:
1126  // if we write
1127  // "Assert (IteratorState::past_the_end || used)"
1128  // used() is called anyway, even if
1129  // state==IteratorState::past_the_end, and will then
1130  // throw the exception!
1131  if (this->state() != IteratorState::past_the_end)
1132  Assert (this->accessor.used(),
1134 #endif
1135 }
1136 
1137 template <typename Accessor>
1138 template <typename OtherAccessor>
1140  :
1141  TriaRawIterator<Accessor> (a)
1142 {
1143 #ifdef DEBUG
1144  // do this like this, because:
1145  // if we write
1146  // "Assert (IteratorState::past_the_end || used)"
1147  // used() is called anyway, even if
1148  // state==IteratorState::past_the_end, and will then
1149  // throw the exception!
1150  if (this->state() != IteratorState::past_the_end)
1151  Assert (this->accessor.used(),
1153 #endif
1154 }
1155 
1156 template <typename Accessor>
1157 template <typename OtherAccessor>
1158 inline
1160  :
1161  TriaIterator<Accessor> (i.accessor)
1162 {}
1163 
1164 
1165 
1166 template <typename Accessor>
1167 template <typename OtherAccessor>
1168 inline
1170  :
1171  TriaIterator<Accessor> (i)
1172 {
1173 #ifdef DEBUG
1174  // do this like this, because:
1175  // if we write
1176  // "Assert (IteratorState::past_the_end || !has_children())"
1177  // has_children() is called anyway, even if
1178  // state==IteratorState::past_the_end, and will then
1179  // throw the exception!
1180  if (this->state() != IteratorState::past_the_end)
1181  Assert (this->accessor.has_children()==false,
1183 #endif
1184 }
1185 
1186 
1187 
1195 template <typename Accessor>
1196 inline
1197 std::ostream &operator << (std::ostream &out,
1198  const TriaRawIterator<Accessor> &i)
1199 {
1200  i.print(out);
1201  return out;
1202 }
1203 
1204 
1205 
1213 template <typename Accessor>
1214 inline
1215 std::ostream &operator << (std::ostream &out,
1216  const TriaIterator<Accessor> &i)
1217 {
1218  i.print(out);
1219  return out;
1220 }
1221 
1222 
1223 
1231 template <typename Accessor>
1232 inline
1233 std::ostream &operator << (std::ostream &out,
1235 {
1236  i.print(out);
1237  return out;
1238 }
1239 
1240 
1241 DEAL_II_NAMESPACE_CLOSE
1242 
1243 
1244 // if in optimized mode: include more templates
1245 #ifndef DEBUG
1246 # include "tria_iterator.templates.h"
1247 #endif
1248 
1249 
1250 /*---------------------------- tria-iterator.h ---------------------------*/
1251 #endif
1252 /*---------------------------- tria-iterator.h ---------------------------*/
Accessor AccessorType
Iterator is invalid, probably due to an error.
TriaActiveIterator< Accessor > & operator++()
const Accessor * operator->() const
static ::ExceptionBase & ExcInvalidComparison()
static ::ExceptionBase & ExcAdvanceInvalidObject()
static ::ExceptionBase & ExcDereferenceInvalidObject(AccessorType arg1)
const Accessor & operator*() const
static ::ExceptionBase & ExcAssignmentOfInactiveObject()
bool operator>(const TriaRawIterator &) const
std::bidirectional_iterator_tag iterator_category
TriaIterator< Accessor > & operator++()
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:346
TriaActiveIterator< Accessor > & operator--()
#define Assert(cond, exc)
Definition: exceptions.h:1142
TriaRawIterator & operator++()
#define DeclException0(Exception0)
Definition: exceptions.h:323
TriaIterator< Accessor > & operator--()
static ::ExceptionBase & ExcDereferenceInvalidObject(Accessor arg1)
TriaRawIterator< Accessor >::iterator_category iterator_category
bool operator==(const TriaRawIterator &) const
bool operator!=(const TriaRawIterator &) const
TriaRawIterator & operator--()
static ::ExceptionBase & ExcAssignmentOfUnusedObject()
TriaActiveIterator< Accessor > & operator=(const TriaActiveIterator< Accessor > &)
TriaIterator< Accessor >::iterator_category iterator_category
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:163
static ::ExceptionBase & ExcDereferenceInvalidCell(Accessor arg1)
TriaRawIterator & operator=(const TriaRawIterator &)
Iterator reached end of container.
const Accessor & access_any() const
Iterator points to a valid object.
TriaIterator< Accessor > & operator=(const TriaIterator< Accessor > &)
void print(StreamType &out) const
IteratorState::IteratorStates state() const
Point< dim, typename ProductType< Number, typename EnableIfScalar< OtherNumber >::type >::type > operator*(const OtherNumber) const
bool operator<(const TriaRawIterator &) const
std::size_t memory_consumption() const