Reference documentation for deal.II version 8.5.1
tria_iterator.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 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 : public std::iterator<std::bidirectional_iterator_tag,Accessor>
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 = 0);
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 
467 
474  Accessor,
475  << "You tried to dereference a cell iterator for which this "
476  << "is not possible. More information on this iterator: "
477  << "level=" << arg1.level()
478  << ", index=" << arg1.index()
479  << ", state="
480  << (arg1.state() == IteratorState::valid ? "valid" :
481  (arg1.state() == IteratorState::past_the_end ?
482  "past_the_end" : "invalid")));
483 
489  Accessor,
490  << "You tried to dereference an iterator for which this "
491  << "is not possible. More information on this iterator: "
492  << "index=" << arg1.index()
493  << ", state="
494  << (arg1.state() == IteratorState::valid ? "valid" :
495  (arg1.state() == IteratorState::past_the_end ?
496  "past_the_end" : "invalid")));
497 
506 
508 protected:
512  Accessor accessor;
513 
514 
524  template <typename SomeAccessor> friend class TriaRawIterator;
525  template <typename SomeAccessor> friend class TriaIterator;
526  template <typename SomeAccessor> friend class TriaActiveIterator;
527 };
528 
529 
537 template <typename Accessor>
538 class TriaIterator : public TriaRawIterator<Accessor>
539 {
540 public:
544  TriaIterator ();
545 
550 
559 
569  const int level,
570  const int index,
571  const typename Accessor::AccessorData *local_data = 0);
572 
577  template <typename OtherAccessor>
578  explicit TriaIterator (const OtherAccessor &a);
579 
589  template <typename OtherAccessor>
591 
598  const typename Accessor::AccessorData *local_data);
599 
605  template <typename OtherAccessor>
607 
612  template <typename OtherAccessor>
614 
620 
627 
632  template <class OtherAccessor>
635 
641  template <class OtherAccessor>
644 
653 
664 
670 
681 };
682 
683 
693 template <typename Accessor>
694 class TriaActiveIterator : public TriaIterator<Accessor>
695 {
696 public:
701 
706 
715 
724 
734  const int level,
735  const int index,
736  const typename Accessor::AccessorData *local_data = 0);
737 
747  template <typename OtherAccessor>
749 
756  const typename Accessor::AccessorData *local_data);
757 
765  template <typename OtherAccessor>
767 
773 
780 
787 
792  template <class OtherAccessor>
795 
801  template <class OtherAccessor>
804 
810  template <class OtherAccessor>
813 
820 
833 
839 
850 };
851 
852 
853 /*----------------------- Inline functions -------------------*/
854 
855 
856 template <typename Accessor>
857 inline
859 TriaRawIterator (const Accessor &a)
860  :
861  accessor (a)
862 {}
863 
864 
865 
866 template <typename Accessor>
867 template <typename OtherAccessor>
868 inline
870 TriaRawIterator (const OtherAccessor &a)
871  :
872  accessor (a)
873 {}
874 
875 
876 
877 template <typename Accessor>
878 template <typename OtherAccessor>
879 inline
882  :
883  accessor (i.accessor)
884 {}
885 
886 
887 
888 template <typename Accessor>
889 template <typename OtherAccessor>
890 inline
893  :
894  accessor (i.accessor)
895 {}
896 
897 
898 
899 template <typename Accessor>
900 template <typename OtherAccessor>
901 inline
904  :
905  accessor (i.accessor)
906 {}
907 
908 
909 
910 template <typename Accessor>
911 inline
912 const Accessor &
914 {
915  Assert (Accessor::structure_dimension!=Accessor::dimension ||
916  state() == IteratorState::valid,
917  ExcDereferenceInvalidCell(accessor));
918  Assert (Accessor::structure_dimension==Accessor::dimension ||
919  state() == IteratorState::valid,
920  ExcDereferenceInvalidObject(accessor));
921 
922  return accessor;
923 }
924 
925 
926 
927 template <typename Accessor>
928 inline
929 Accessor &
931 {
932  Assert (Accessor::structure_dimension!=Accessor::dimension ||
933  state() == IteratorState::valid,
934  ExcDereferenceInvalidCell(accessor));
935  Assert (Accessor::structure_dimension==Accessor::dimension ||
936  state() == IteratorState::valid,
937  ExcDereferenceInvalidObject(accessor));
938 
939  return accessor;
940 }
941 
942 
943 
944 template <typename Accessor>
945 inline
946 const Accessor &
948 {
949  return accessor;
950 }
951 
952 
953 
954 template <typename Accessor>
955 inline
956 const Accessor *
958 {
959  return &(this->operator* ());
960 }
961 
962 
963 
964 template <typename Accessor>
965 inline
966 Accessor *
968 {
969  return &(this->operator* ());
970 }
971 
972 
973 
974 template <typename Accessor>
975 inline
978 {
979  return accessor.state ();
980 }
981 
982 
983 
984 template <typename Accessor>
985 inline
986 bool
988 {
991 
992  Assert (&accessor.get_triangulation() == &other.accessor.get_triangulation(),
993  ExcInvalidComparison());
994 
995  // Deal with iterators past end
996  if (state()==IteratorState::past_the_end)
997  return false;
998  if (other.state()==IteratorState::past_the_end)
999  return true;
1000 
1001  return ((**this) < (*other));
1002 }
1003 
1004 
1005 
1006 template <typename Accessor>
1007 inline
1008 bool
1010 {
1011  return (other < *this);
1012 }
1013 
1014 
1015 
1016 template <typename Accessor>
1017 inline
1020 {
1021  Assert (state() == IteratorState::valid, ExcAdvanceInvalidObject());
1022 
1023  ++accessor;
1024  return *this;
1025 }
1026 
1027 
1028 
1029 template <typename Accessor>
1030 inline
1033 {
1034  Assert (state() == IteratorState::valid, ExcAdvanceInvalidObject());
1035 
1036  --accessor;
1037  return *this;
1038 }
1039 
1040 
1041 
1042 template <typename Accessor>
1043 template <class StreamType>
1044 inline
1045 void
1046 TriaRawIterator<Accessor>::print (StreamType &out) const
1047 {
1048  if (Accessor::structure_dimension==Accessor::dimension)
1049  out << accessor.level() << "." << accessor.index();
1050  else
1051  out << accessor.index();
1052 }
1053 
1054 
1055 
1056 template <typename Accessor>
1057 inline
1058 std::size_t
1060 {
1061  return sizeof(TriaRawIterator<Accessor>);
1062 }
1063 
1064 
1065 
1066 template <typename Accessor>
1067 template <typename OtherAccessor>
1068 inline
1070  :
1071  TriaRawIterator<Accessor> (i.accessor)
1072 {}
1073 
1074 
1075 
1076 template <typename Accessor>
1077 template <typename OtherAccessor>
1078 inline
1080  :
1081  TriaRawIterator<Accessor> (i.accessor)
1082 {}
1083 
1084 
1085 
1086 template <typename Accessor>
1087 template <typename OtherAccessor>
1088 inline
1090  :
1091  TriaRawIterator<Accessor> (i.accessor)
1092 {
1093 #ifdef DEBUG
1094  // do this like this, because:
1095  // if we write
1096  // "Assert (IteratorState::past_the_end || used)"
1097  // used() is called anyway, even if
1098  // state==IteratorState::past_the_end, and will then
1099  // throw the exception!
1100  if (this->state() != IteratorState::past_the_end)
1101  Assert (this->accessor.used(),
1103 #endif
1104 }
1105 
1106 template <typename Accessor>
1107 template <typename OtherAccessor>
1109  :
1110  TriaRawIterator<Accessor> (a)
1111 {
1112 #ifdef DEBUG
1113  // do this like this, because:
1114  // if we write
1115  // "Assert (IteratorState::past_the_end || used)"
1116  // used() is called anyway, even if
1117  // state==IteratorState::past_the_end, and will then
1118  // throw the exception!
1119  if (this->state() != IteratorState::past_the_end)
1120  Assert (this->accessor.used(),
1122 #endif
1123 }
1124 
1125 template <typename Accessor>
1126 template <typename OtherAccessor>
1127 inline
1129  :
1130  TriaIterator<Accessor> (i.accessor)
1131 {}
1132 
1133 
1134 
1135 template <typename Accessor>
1136 template <typename OtherAccessor>
1137 inline
1139  :
1140  TriaIterator<Accessor> (i)
1141 {
1142 #ifdef DEBUG
1143  // do this like this, because:
1144  // if we write
1145  // "Assert (IteratorState::past_the_end || !has_children())"
1146  // has_children() is called anyway, even if
1147  // state==IteratorState::past_the_end, and will then
1148  // throw the exception!
1149  if (this->state() != IteratorState::past_the_end)
1150  Assert (this->accessor.has_children()==false,
1152 #endif
1153 }
1154 
1155 
1156 
1164 template <typename Accessor>
1165 inline
1166 std::ostream &operator << (std::ostream &out,
1167  const TriaRawIterator<Accessor> &i)
1168 {
1169  i.print(out);
1170  return out;
1171 }
1172 
1173 
1174 
1182 template <typename Accessor>
1183 inline
1184 std::ostream &operator << (std::ostream &out,
1185  const TriaIterator<Accessor> &i)
1186 {
1187  i.print(out);
1188  return out;
1189 }
1190 
1191 
1192 
1200 template <typename Accessor>
1201 inline
1202 std::ostream &operator << (std::ostream &out,
1204 {
1205  i.print(out);
1206  return out;
1207 }
1208 
1209 
1210 DEAL_II_NAMESPACE_CLOSE
1211 
1212 
1213 // if in optimized mode: include more templates
1214 #ifndef DEBUG
1215 # include "tria_iterator.templates.h"
1216 #endif
1217 
1218 
1219 /*---------------------------- tria-iterator.h ---------------------------*/
1220 #endif
1221 /*---------------------------- 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()
const Accessor & operator*() const
static ::ExceptionBase & ExcAssignmentOfInactiveObject()
bool operator>(const TriaRawIterator &) const
TriaIterator< Accessor > & operator++()
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:564
TriaActiveIterator< Accessor > & operator--()
#define Assert(cond, exc)
Definition: exceptions.h:313
TriaRawIterator & operator++()
#define DeclException0(Exception0)
Definition: exceptions.h:541
TriaIterator< Accessor > & operator--()
static ::ExceptionBase & ExcDereferenceInvalidObject(Accessor arg1)
bool operator==(const TriaRawIterator &) const
bool operator!=(const TriaRawIterator &) const
TriaRawIterator & operator--()
static ::ExceptionBase & ExcAssignmentOfUnusedObject()
TriaActiveIterator< Accessor > & operator=(const TriaActiveIterator< Accessor > &)
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
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
static ::ExceptionBase & ExcDereferenceInvalidObject()
bool operator<(const TriaRawIterator &) const
std::size_t memory_consumption() const