Reference documentation for deal.II version 8.1.0
filtered_iterator.h
1 // ---------------------------------------------------------------------
2 // @f$Id: filtered_iterator.h 30053 2013-07-18 21:38:49Z maier @f$
3 //
4 // Copyright (C) 2002 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__filtered_iterator_h
18 #define __deal2__filtered_iterator_h
19 
20 
21 #include <deal.II/base/config.h>
23 #include <deal.II/grid/tria_iterator_base.h>
24 
26 
27 
44 namespace IteratorFilters
45 {
52  class Active
53  {
54  public:
59  template <class Iterator>
60  bool operator () (const Iterator &i) const;
61  };
62 
71  {
72  public:
77  template <class Iterator>
78  bool operator () (const Iterator &i) const;
79  };
80 
81 
90  {
91  public:
96  template <class Iterator>
97  bool operator () (const Iterator &i) const;
98  };
99 
100 
109  {
110  public:
115  LevelEqualTo (const unsigned int level);
116 
122  template <class Iterator>
123  bool operator () (const Iterator &i) const;
124 
125  protected:
129  const unsigned int level;
130  };
131 
132 
133 
143  {
144  public:
150 
156  template <class Iterator>
157  bool operator () (const Iterator &i) const;
158 
159  protected:
164  };
165 
166 
167 
179  {
180  public:
184  template <class Iterator>
185  bool operator () (const Iterator &i) const;
186  };
187 
188 
189 
197  {
198  public:
203  template <class Iterator>
204  bool operator () (const Iterator &i) const;
205  };
206 }
207 
208 
401 template <typename BaseIterator>
402 class FilteredIterator : public BaseIterator
403 {
404 public:
409  typedef typename BaseIterator::AccessorType AccessorType;
410 
418  template <typename Predicate>
419  FilteredIterator (Predicate p);
420 
448  template <typename Predicate>
449  FilteredIterator (Predicate p,
450  const BaseIterator &bi);
451 
458 
463 
477 
489 
506 
523 
532  bool operator == (const FilteredIterator &fi) const;
533 
544  bool operator == (const BaseIterator &fi) const;
545 
554  bool operator != (const FilteredIterator &fi) const;
555 
566  bool operator != (const BaseIterator &fi) const;
567 
576  bool operator < (const FilteredIterator &fi) const;
577 
588  bool operator < (const BaseIterator &fi) const;
589 
598 
607 
616 
625 
629  DeclException1 (ExcInvalidElement,
630  BaseIterator,
631  << "The element " << arg1
632  << " with which you want to compare or which you want to"
633  << " assign from is invalid since it does not satisfy the predicate.");
634 
635 private:
636 
655  {
656  public:
664  virtual ~PredicateBase () {}
665 
673  virtual bool operator () (const BaseIterator &bi) const = 0;
674 
680  virtual PredicateBase *clone () const = 0;
681  };
682 
683 
697  template <typename Predicate>
699  {
700  public:
706  PredicateTemplate (const Predicate &predicate);
707 
713  virtual bool operator () (const BaseIterator &bi) const;
714 
720  virtual PredicateBase *clone () const;
721 
722  private:
726  const Predicate predicate;
727  };
728 
736 
737 };
738 
739 
740 
751 template <typename BaseIterator, typename Predicate>
754  const Predicate &p)
755 {
757  fi.set_to_next_positive (i);
758  return fi;
759 }
760 
761 
762 
763 /* ------------------ Inline functions and templates ------------ */
764 
765 
766 template <typename BaseIterator>
767 template <typename Predicate>
768 inline
770 FilteredIterator (Predicate p)
771  :
772  predicate (new PredicateTemplate<Predicate>(p))
773 {}
774 
775 
776 
777 template <typename BaseIterator>
778 template <typename Predicate>
779 inline
781 FilteredIterator (Predicate p,
782  const BaseIterator &bi)
783  :
784  BaseIterator (bi),
785  predicate (new PredicateTemplate<Predicate>(p))
786 {
787  if ((this->state() == IteratorState::valid) &&
788  ! (*predicate) (*this))
790 }
791 
792 
793 
794 template <typename BaseIterator>
795 inline
798  :
799 // this construction looks strange, but without going through the
800 // address of fi, GCC would not cast fi to the base class of type
801 // BaseIterator but tries to go through constructing a new
802 // BaseIterator with an Accessor.
803  BaseIterator (*(BaseIterator *)(&fi)),
804  predicate (fi.predicate->clone ())
805 {}
806 
807 
808 
809 template <typename BaseIterator>
810 inline
813 {
814  delete predicate;
815  predicate = 0;
816 }
817 
818 
819 
820 template <typename BaseIterator>
821 inline
825 {
826  Assert ((fi.state() != IteratorState::valid) || (*predicate)(fi),
827  ExcInvalidElement(fi));
828  BaseIterator::operator = (fi);
829  return *this;
830 }
831 
832 
833 
834 template <typename BaseIterator>
835 inline
839 {
840  Assert ((bi.state() != IteratorState::valid) || (*predicate)(bi),
841  ExcInvalidElement(bi));
842  BaseIterator::operator = (bi);
843  return *this;
844 }
845 
846 
847 
848 template <typename BaseIterator>
849 inline
853 {
854  BaseIterator::operator = (bi);
855  while ((this->state() == IteratorState::valid) &&
856  ( ! (*predicate)(*this)))
857  BaseIterator::operator++ ();
858 
859  return *this;
860 }
861 
862 
863 
864 template <typename BaseIterator>
865 inline
869 {
870  BaseIterator::operator = (bi);
871  while ((this->state() == IteratorState::valid) &&
872  ( ! (*predicate)(*this)))
873  BaseIterator::operator-- ();
874 
875  return *this;
876 }
877 
878 
879 
880 template <typename BaseIterator>
881 inline
882 bool
885 {
886  return (static_cast<const BaseIterator &>(*this)
887  ==
888  static_cast<const BaseIterator &>(fi));
889 }
890 
891 
892 
893 template <typename BaseIterator>
894 inline
895 bool
898 {
899  return (static_cast<const BaseIterator &>(*this)
900  !=
901  static_cast<const BaseIterator &>(fi));
902 }
903 
904 
905 
906 template <typename BaseIterator>
907 inline
908 bool
911 {
912  return (static_cast<const BaseIterator &>(*this)
913  <
914  static_cast<const BaseIterator &>(fi));
915 }
916 
917 
918 
919 
920 template <typename BaseIterator>
921 inline
922 bool
924 operator == (const BaseIterator &bi) const
925 {
926  return (static_cast<const BaseIterator &>(*this) == bi);
927 }
928 
929 
930 
931 template <typename BaseIterator>
932 inline
933 bool
935 operator != (const BaseIterator &bi) const
936 {
937  return (static_cast<const BaseIterator &>(*this) != bi);
938 }
939 
940 
941 
942 template <typename BaseIterator>
943 inline
944 bool
946 operator < (const BaseIterator &bi) const
947 {
948  return (static_cast<const BaseIterator &>(*this) < bi);
949 }
950 
951 
952 template <typename BaseIterator>
953 inline
957 {
958  if (this->state() == IteratorState::valid)
959  do
960  BaseIterator::operator++ ();
961  while ((this->state() == IteratorState::valid) &&
962  !(*predicate) (*this));
963  return *this;
964 }
965 
966 
967 
968 template <typename BaseIterator>
969 inline
973 {
974  const FilteredIterator old_state = *this;
975 
976  if (this->state() == IteratorState::valid)
977  do
978  BaseIterator::operator++ ();
979  while ((this->state() == IteratorState::valid) &&
980  !(*predicate) (*this));
981  return old_state;
982 }
983 
984 
985 
986 
987 template <typename BaseIterator>
988 inline
992 {
993  if (this->state() == IteratorState::valid)
994  do
995  BaseIterator::operator-- ();
996  while ((this->state() == IteratorState::valid) &&
997  !(*predicate) (*this));
998  return *this;
999 }
1000 
1001 
1002 
1003 template <typename BaseIterator>
1004 inline
1008 {
1009  const FilteredIterator old_state = *this;
1010 
1011  if (this->state() == IteratorState::valid)
1012  do
1013  BaseIterator::operator-- ();
1014  while ((this->state() == IteratorState::valid) &&
1015  !(*predicate) (*this));
1016  return old_state;
1017 }
1018 
1019 
1020 
1021 template <typename BaseIterator>
1022 template <typename Predicate>
1023 inline
1025 PredicateTemplate (const Predicate &predicate)
1026  :
1027  predicate (predicate)
1028 {}
1029 
1030 
1031 
1032 template <typename BaseIterator>
1033 template <typename Predicate>
1034 bool
1036 operator () (const BaseIterator &bi) const
1037 {
1038  return predicate(bi);
1039 }
1040 
1041 
1042 
1043 template <typename BaseIterator>
1044 template <typename Predicate>
1047 clone () const
1048 {
1049  return new PredicateTemplate (predicate);
1050 }
1051 
1052 
1053 
1054 namespace IteratorFilters
1055 {
1056 
1057 // ---------------- IteratorFilters::Active ---------
1058 
1059  template <class Iterator>
1060  inline
1061  bool
1062  Active::operator () (const Iterator &i) const
1063  {
1064  return (i->active());
1065  }
1066 
1067 
1068 // ---------------- IteratorFilters::UserFlagSet ---------
1069 
1070  template <class Iterator>
1071  inline
1072  bool
1073  UserFlagSet::operator () (const Iterator &i) const
1074  {
1075  return (i->user_flag_set());
1076  }
1077 
1078 
1079 // ---------------- IteratorFilters::UserFlagNotSet ---------
1080 
1081  template <class Iterator>
1082  inline
1083  bool
1084  UserFlagNotSet::operator () (const Iterator &i) const
1085  {
1086  return (! i->user_flag_set());
1087  }
1088 
1089 
1090 // ---------------- IteratorFilters::LevelEqualTo ---------
1091  inline
1092  LevelEqualTo::LevelEqualTo (const unsigned int level)
1093  :
1094  level (level)
1095  {}
1096 
1097 
1098 
1099  template <class Iterator>
1100  inline
1101  bool
1102  LevelEqualTo::operator () (const Iterator &i) const
1103  {
1104  return (static_cast<unsigned int>(i->level()) == level);
1105  }
1106 
1107 
1108 
1109 // ---------------- IteratorFilters::SubdomainEqualTo ---------
1110  inline
1112  :
1113  subdomain_id (subdomain_id)
1114  {}
1115 
1116 
1117 
1118  template <class Iterator>
1119  inline
1120  bool
1121  SubdomainEqualTo::operator () (const Iterator &i) const
1122  {
1123  return (i->subdomain_id() == subdomain_id);
1124  }
1125 
1126 
1127 
1128 // ---------------- IteratorFilters::LocallyOwnedCell ---------
1129 
1130  template <class Iterator>
1131  inline
1132  bool
1133  LocallyOwnedCell::operator () (const Iterator &i) const
1134  {
1135  return (i->is_locally_owned());
1136  }
1137 
1138 
1139 // ---------------- IteratorFilters::LocallyOwnedLevelCell ---------
1140 
1141  template <class Iterator>
1142  inline
1143  bool
1144  LocallyOwnedLevelCell::operator () (const Iterator &i) const
1145  {
1146  return (i->is_locally_owned_on_level());
1147  }
1148 }
1149 
1150 
1151 DEAL_II_NAMESPACE_CLOSE
1152 
1153 /*------------------------- filtered_iterator.h ------------------------*/
1154 #endif
1155 /*------------------------- filtered_iterator.h ------------------------*/
1156 
1157 
bool operator()(const Iterator &i) const
DeclException1(ExcInvalidElement, BaseIterator,<< "The element "<< arg1<< " with which you want to compare or which you want to"<< " assign from is invalid since it does not satisfy the predicate.")
bool operator()(const Iterator &i) const
FilteredIterator & operator++()
Iterator points to a valid object.
const types::subdomain_id subdomain_id
FilteredIterator(Predicate p)
virtual bool operator()(const BaseIterator &bi) const
FilteredIterator & operator--()
FilteredIterator & operator=(const FilteredIterator &fi)
bool operator()(const Iterator &i) const
#define Assert(cond, exc)
Definition: exceptions.h:299
bool operator==(const FilteredIterator &fi) const
virtual bool operator()(const BaseIterator &bi) const =0
unsigned int subdomain_id
Definition: types.h:43
bool operator()(const Iterator &i) const
LevelEqualTo(const unsigned int level)
BaseIterator::AccessorType AccessorType
FilteredIterator & set_to_next_positive(const BaseIterator &bi)
SubdomainEqualTo(const types::subdomain_id subdomain_id)
bool operator<(const FilteredIterator &fi) const
virtual PredicateBase * clone() const =0
bool operator!=(const FilteredIterator &fi) const
PredicateTemplate(const Predicate &predicate)
const PredicateBase * predicate
FilteredIterator & set_to_previous_positive(const BaseIterator &bi)
FilteredIterator< BaseIterator > make_filtered_iterator(const BaseIterator &i, const Predicate &p)
virtual PredicateBase * clone() const
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const