Reference documentation for deal.II version 8.4.2
filtered_iterator.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 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__filtered_iterator_h
17 #define dealii__filtered_iterator_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/grid/tria_iterator_base.h>
23 
24 #include <set>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 
45 namespace IteratorFilters
46 {
53  class Active
54  {
55  public:
60  template <class Iterator>
61  bool operator () (const Iterator &i) const;
62  };
63 
73  {
74  public:
79  template <class Iterator>
80  bool operator () (const Iterator &i) const;
81  };
82 
83 
92  {
93  public:
98  template <class Iterator>
99  bool operator () (const Iterator &i) const;
100  };
101 
102 
111  {
112  public:
117  LevelEqualTo (const unsigned int level);
118 
124  template <class Iterator>
125  bool operator () (const Iterator &i) const;
126 
127  protected:
131  const unsigned int level;
132  };
133 
134 
135 
145  {
146  public:
151  SubdomainEqualTo (const types::subdomain_id subdomain_id);
152 
158  template <class Iterator>
159  bool operator () (const Iterator &i) const;
160 
161  protected:
166  };
167 
168 
169 
182  {
183  public:
187  template <class Iterator>
188  bool operator () (const Iterator &i) const;
189  };
190 
191 
192 
200  {
201  public:
206  template <class Iterator>
207  bool operator () (const Iterator &i) const;
208  };
209 
210 
221  {
222  public:
227  MaterialIdEqualTo (const types::material_id material_id,
228  const bool only_locally_owned = false);
229 
235  MaterialIdEqualTo (const std::set<types::material_id> material_ids,
236  const bool only_locally_owned = false);
237 
243  template <class Iterator>
244  bool operator () (const Iterator &i) const;
245 
246  protected:
250  const std::set<types::material_id> material_ids;
254  const bool only_locally_owned;
255  };
256 
267  {
268  public:
273  ActiveFEIndexEqualTo (const unsigned int active_fe_index,
274  const bool only_locally_owned = false);
275 
281  ActiveFEIndexEqualTo (const std::set<unsigned int> active_fe_indices,
282  const bool only_locally_owned = false);
283 
289  template <class Iterator>
290  bool operator () (const Iterator &i) const;
291 
292  protected:
296  const std::set<unsigned int> active_fe_indices;
300  const bool only_locally_owned;
301  };
302 }
303 
304 
490 template <typename BaseIterator>
491 class FilteredIterator : public BaseIterator
492 {
493 public:
497  typedef typename BaseIterator::AccessorType AccessorType;
498 
503  template <typename Predicate>
504  FilteredIterator (Predicate p);
505 
523  template <typename Predicate>
524  FilteredIterator (Predicate p,
525  const BaseIterator &bi);
526 
532 
536  ~FilteredIterator ();
537 
544  FilteredIterator &operator = (const FilteredIterator &fi);
545 
551  FilteredIterator &operator = (const BaseIterator &fi);
552 
562  set_to_next_positive (const BaseIterator &bi);
563 
573  set_to_previous_positive (const BaseIterator &bi);
574 
581  bool operator == (const FilteredIterator &fi) const;
582 
589  bool operator == (const BaseIterator &fi) const;
590 
597  bool operator != (const FilteredIterator &fi) const;
598 
605  bool operator != (const BaseIterator &fi) const;
606 
613  bool operator < (const FilteredIterator &fi) const;
614 
621  bool operator < (const BaseIterator &fi) const;
622 
627  FilteredIterator &operator ++ ();
628 
633  FilteredIterator operator ++ (int);
634 
639  FilteredIterator &operator -- ();
640 
645  FilteredIterator operator -- (int);
646 
650  DeclException1 (ExcInvalidElement,
651  BaseIterator,
652  << "The element " << arg1
653  << " with which you want to compare or which you want to"
654  << " assign from is invalid since it does not satisfy the predicate.");
655 
656 private:
657 
668  {
669  public:
674  virtual ~PredicateBase () {}
675 
680  virtual bool operator () (const BaseIterator &bi) const = 0;
681 
686  virtual PredicateBase *clone () const = 0;
687  };
688 
689 
698  template <typename Predicate>
700  {
701  public:
705  PredicateTemplate (const Predicate &predicate);
706 
710  virtual bool operator () (const BaseIterator &bi) const;
711 
716  virtual PredicateBase *clone () const;
717 
718  private:
722  const Predicate predicate;
723  };
724 
730 
731 };
732 
733 
734 
744 template <typename BaseIterator, typename Predicate>
746 make_filtered_iterator (const BaseIterator &i,
747  const Predicate &p)
748 {
750  fi.set_to_next_positive (i);
751  return fi;
752 }
753 
754 
755 
756 /* ------------------ Inline functions and templates ------------ */
757 
758 
759 template <typename BaseIterator>
760 template <typename Predicate>
761 inline
763 FilteredIterator (Predicate p)
764  :
765  predicate (new PredicateTemplate<Predicate>(p))
766 {}
767 
768 
769 
770 template <typename BaseIterator>
771 template <typename Predicate>
772 inline
774 FilteredIterator (Predicate p,
775  const BaseIterator &bi)
776  :
777  BaseIterator (bi),
778  predicate (new PredicateTemplate<Predicate>(p))
779 {
780  if ((this->state() == IteratorState::valid) &&
781  ! (*predicate) (*this))
783 }
784 
785 
786 
787 template <typename BaseIterator>
788 inline
791  :
792 // this construction looks strange, but without going through the
793 // address of fi, GCC would not cast fi to the base class of type
794 // BaseIterator but tries to go through constructing a new
795 // BaseIterator with an Accessor.
796  BaseIterator (*(BaseIterator *)(&fi)),
797  predicate (fi.predicate->clone ())
798 {}
799 
800 
801 
802 template <typename BaseIterator>
803 inline
806 {
807  delete predicate;
808  predicate = 0;
809 }
810 
811 
812 
813 template <typename BaseIterator>
814 inline
818 {
819  Assert ((fi.state() != IteratorState::valid) || (*predicate)(fi),
820  ExcInvalidElement(fi));
821  BaseIterator::operator = (fi);
822  return *this;
823 }
824 
825 
826 
827 template <typename BaseIterator>
828 inline
831 operator = (const BaseIterator &bi)
832 {
833  Assert ((bi.state() != IteratorState::valid) || (*predicate)(bi),
834  ExcInvalidElement(bi));
835  BaseIterator::operator = (bi);
836  return *this;
837 }
838 
839 
840 
841 template <typename BaseIterator>
842 inline
845 set_to_next_positive (const BaseIterator &bi)
846 {
847  BaseIterator::operator = (bi);
848  while ((this->state() == IteratorState::valid) &&
849  ( ! (*predicate)(*this)))
850  BaseIterator::operator++ ();
851 
852  return *this;
853 }
854 
855 
856 
857 template <typename BaseIterator>
858 inline
861 set_to_previous_positive (const BaseIterator &bi)
862 {
863  BaseIterator::operator = (bi);
864  while ((this->state() == IteratorState::valid) &&
865  ( ! (*predicate)(*this)))
866  BaseIterator::operator-- ();
867 
868  return *this;
869 }
870 
871 
872 
873 template <typename BaseIterator>
874 inline
875 bool
878 {
879  return (static_cast<const BaseIterator &>(*this)
880  ==
881  static_cast<const BaseIterator &>(fi));
882 }
883 
884 
885 
886 template <typename BaseIterator>
887 inline
888 bool
891 {
892  return (static_cast<const BaseIterator &>(*this)
893  !=
894  static_cast<const BaseIterator &>(fi));
895 }
896 
897 
898 
899 template <typename BaseIterator>
900 inline
901 bool
904 {
905  return (static_cast<const BaseIterator &>(*this)
906  <
907  static_cast<const BaseIterator &>(fi));
908 }
909 
910 
911 
912 
913 template <typename BaseIterator>
914 inline
915 bool
917 operator == (const BaseIterator &bi) const
918 {
919  return (static_cast<const BaseIterator &>(*this) == bi);
920 }
921 
922 
923 
924 template <typename BaseIterator>
925 inline
926 bool
928 operator != (const BaseIterator &bi) const
929 {
930  return (static_cast<const BaseIterator &>(*this) != bi);
931 }
932 
933 
934 
935 template <typename BaseIterator>
936 inline
937 bool
939 operator < (const BaseIterator &bi) const
940 {
941  return (static_cast<const BaseIterator &>(*this) < bi);
942 }
943 
944 
945 template <typename BaseIterator>
946 inline
950 {
951  if (this->state() == IteratorState::valid)
952  do
953  BaseIterator::operator++ ();
954  while ((this->state() == IteratorState::valid) &&
955  !(*predicate) (*this));
956  return *this;
957 }
958 
959 
960 
961 template <typename BaseIterator>
962 inline
966 {
967  const FilteredIterator old_state = *this;
968 
969  if (this->state() == IteratorState::valid)
970  do
971  BaseIterator::operator++ ();
972  while ((this->state() == IteratorState::valid) &&
973  !(*predicate) (*this));
974  return old_state;
975 }
976 
977 
978 
979 
980 template <typename BaseIterator>
981 inline
985 {
986  if (this->state() == IteratorState::valid)
987  do
988  BaseIterator::operator-- ();
989  while ((this->state() == IteratorState::valid) &&
990  !(*predicate) (*this));
991  return *this;
992 }
993 
994 
995 
996 template <typename BaseIterator>
997 inline
1001 {
1002  const FilteredIterator old_state = *this;
1003 
1004  if (this->state() == IteratorState::valid)
1005  do
1006  BaseIterator::operator-- ();
1007  while ((this->state() == IteratorState::valid) &&
1008  !(*predicate) (*this));
1009  return old_state;
1010 }
1011 
1012 
1013 
1014 template <typename BaseIterator>
1015 template <typename Predicate>
1016 inline
1018 PredicateTemplate (const Predicate &predicate)
1019  :
1020  predicate (predicate)
1021 {}
1022 
1023 
1024 
1025 template <typename BaseIterator>
1026 template <typename Predicate>
1027 bool
1029 operator () (const BaseIterator &bi) const
1030 {
1031  return predicate(bi);
1032 }
1033 
1034 
1035 
1036 template <typename BaseIterator>
1037 template <typename Predicate>
1040 clone () const
1041 {
1042  return new PredicateTemplate (predicate);
1043 }
1044 
1045 
1046 
1047 namespace IteratorFilters
1048 {
1049 
1050 // ---------------- IteratorFilters::Active ---------
1051 
1052  template <class Iterator>
1053  inline
1054  bool
1055  Active::operator () (const Iterator &i) const
1056  {
1057  return (i->active());
1058  }
1059 
1060 
1061 // ---------------- IteratorFilters::UserFlagSet ---------
1062 
1063  template <class Iterator>
1064  inline
1065  bool
1066  UserFlagSet::operator () (const Iterator &i) const
1067  {
1068  return (i->user_flag_set());
1069  }
1070 
1071 
1072 // ---------------- IteratorFilters::UserFlagNotSet ---------
1073 
1074  template <class Iterator>
1075  inline
1076  bool
1077  UserFlagNotSet::operator () (const Iterator &i) const
1078  {
1079  return (! i->user_flag_set());
1080  }
1081 
1082 
1083 // ---------------- IteratorFilters::LevelEqualTo ---------
1084  inline
1085  LevelEqualTo::LevelEqualTo (const unsigned int level)
1086  :
1087  level (level)
1088  {}
1089 
1090 
1091 
1092  template <class Iterator>
1093  inline
1094  bool
1095  LevelEqualTo::operator () (const Iterator &i) const
1096  {
1097  return (static_cast<unsigned int>(i->level()) == level);
1098  }
1099 
1100 
1101 
1102 // ---------------- IteratorFilters::SubdomainEqualTo ---------
1103  inline
1105  :
1106  subdomain_id (subdomain_id)
1107  {}
1108 
1109 
1110 
1111  template <class Iterator>
1112  inline
1113  bool
1114  SubdomainEqualTo::operator () (const Iterator &i) const
1115  {
1116  return (i->subdomain_id() == subdomain_id);
1117  }
1118 
1119 
1120 
1121 // ---------------- IteratorFilters::LocallyOwnedCell ---------
1122 
1123  template <class Iterator>
1124  inline
1125  bool
1126  LocallyOwnedCell::operator () (const Iterator &i) const
1127  {
1128  return (i->is_locally_owned());
1129  }
1130 
1131 
1132 // ---------------- IteratorFilters::LocallyOwnedLevelCell ---------
1133 
1134  template <class Iterator>
1135  inline
1136  bool
1137  LocallyOwnedLevelCell::operator () (const Iterator &i) const
1138  {
1139  return (i->is_locally_owned_on_level());
1140  }
1141 
1142 
1143 
1144 // ---------------- IteratorFilters::MaterialIdEqualTo ---------
1145  inline
1147  const bool only_locally_owned)
1148  :
1149  // Note: matrial_ids is a const member and has to be populated with a
1150  // constructor. Unfortunately, C++98/03 does not allow the use of an
1151  // initializer list. Therefore, treat material_id as an array of one
1152  // element.
1153  // This is well defined according to [expr.add].4 (ISO 14882).
1154  material_ids (&material_id, &material_id+1),
1155  only_locally_owned (only_locally_owned)
1156  {}
1157 
1158 
1159 
1160  inline
1161  MaterialIdEqualTo::MaterialIdEqualTo (const std::set<types::material_id> material_ids,
1162  const bool only_locally_owned)
1163  :
1164  material_ids (material_ids),
1165  only_locally_owned (only_locally_owned)
1166  {}
1167 
1168 
1169 
1170  template <class Iterator>
1171  inline
1172  bool
1173  MaterialIdEqualTo::operator () (const Iterator &i) const
1174  {
1175  return only_locally_owned == true ?
1176  (material_ids.find(i->material_id()) != material_ids.end() && i->is_locally_owned()):
1177  material_ids.find(i->material_id()) != material_ids.end();
1178  }
1179 
1180 
1181 
1182 // ---------------- IteratorFilters::ActiveFEIndexEqualTo ---------
1183  inline
1184  ActiveFEIndexEqualTo::ActiveFEIndexEqualTo (const unsigned int active_fe_index,
1185  const bool only_locally_owned)
1186  :
1187  // Note: active_fe_indices is a const member and has to be populated
1188  // with a constructor. Unfortunately, C++98/03 does not allow the use
1189  // of an initializer list. Therefore, treat active_fe_index as an array
1190  // of one element.
1191  // This is well defined according to [expr.add].4 (ISO 14882).
1192  active_fe_indices (&active_fe_index, &active_fe_index+1),
1193  only_locally_owned (only_locally_owned)
1194  {}
1195 
1196 
1197 
1198  inline
1200  const bool only_locally_owned)
1201  :
1202  active_fe_indices (active_fe_indices),
1203  only_locally_owned (only_locally_owned)
1204  {}
1205 
1206 
1207 
1208  template <class Iterator>
1209  inline
1210  bool
1211  ActiveFEIndexEqualTo::operator () (const Iterator &i) const
1212  {
1213  return only_locally_owned == true ?
1214  (active_fe_indices.find(i->active_fe_index()) != active_fe_indices.end() && i->is_locally_owned()):
1215  active_fe_indices.find(i->active_fe_index()) != active_fe_indices.end();
1216  }
1217 
1218 }
1219 
1220 
1221 DEAL_II_NAMESPACE_CLOSE
1222 
1223 /*------------------------- filtered_iterator.h ------------------------*/
1224 #endif
1225 /*------------------------- filtered_iterator.h ------------------------*/
1226 
1227 
bool operator()(const Iterator &i) const
FilteredIterator & operator++()
const types::subdomain_id subdomain_id
FilteredIterator(Predicate p)
bool operator()(const Iterator &i) const
bool operator==(const FilteredIterator &fi) const
unsigned char material_id
Definition: types.h:130
bool operator()(const Iterator &i) const
FilteredIterator & operator--()
const std::set< types::material_id > material_ids
FilteredIterator & operator=(const FilteredIterator &fi)
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:542
#define Assert(cond, exc)
Definition: exceptions.h:294
bool operator()(const Iterator &i) const
bool operator!=(const FilteredIterator &fi) const
unsigned int subdomain_id
Definition: types.h:42
MaterialIdEqualTo(const types::material_id material_id, const bool only_locally_owned=false)
bool operator()(const Iterator &i) const
const std::set< unsigned int > active_fe_indices
BaseIterator::AccessorType AccessorType
FilteredIterator & set_to_next_positive(const BaseIterator &bi)
SubdomainEqualTo(const types::subdomain_id subdomain_id)
bool operator()(const Iterator &i) const
PredicateTemplate(const Predicate &predicate)
ActiveFEIndexEqualTo(const unsigned int active_fe_index, const bool only_locally_owned=false)
Iterator points to a valid object.
const PredicateBase * predicate
FilteredIterator & set_to_previous_positive(const BaseIterator &bi)
bool operator<(const FilteredIterator &fi) const
FilteredIterator< BaseIterator > make_filtered_iterator(const BaseIterator &i, const Predicate &p)
bool operator()(const Iterator &i) const