Reference documentation for deal.II version Git 24fafe1087 2021-11-29 17:11:51 +0100
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
filtered_iterator.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2002 - 2020 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_filtered_iterator_h
17 # define dealii_filtered_iterator_h
18 
19 
20 # include <deal.II/base/config.h>
21 
22 # include <deal.II/base/exceptions.h>
24 
26 
27 # include <memory>
28 # include <set>
29 # include <tuple>
30 
32 
33 
49 namespace IteratorFilters
50 {
57  class Active
58  {
59  public:
64  template <class Iterator>
65  bool
66  operator()(const Iterator &i) const;
67  };
68 
78  {
79  public:
84  template <class Iterator>
85  bool
86  operator()(const Iterator &i) const;
87  };
88 
89 
98  {
99  public:
104  template <class Iterator>
105  bool
106  operator()(const Iterator &i) const;
107  };
108 
109 
118  {
119  public:
124  LevelEqualTo(const unsigned int level);
125 
131  template <class Iterator>
132  bool
133  operator()(const Iterator &i) const;
134 
135  protected:
139  const unsigned int level;
140  };
141 
142 
143 
153  {
154  public:
160 
166  template <class Iterator>
167  bool
168  operator()(const Iterator &i) const;
169 
170  protected:
175  };
176 
177 
178 
191  {
192  public:
196  template <class Iterator>
197  bool
198  operator()(const Iterator &i) const;
199  };
200 
201 
202 
210  {
211  public:
216  template <class Iterator>
217  bool
218  operator()(const Iterator &i) const;
219  };
220 
221 
231  {
232  public:
238  const bool only_locally_owned = false);
239 
245  MaterialIdEqualTo(const std::set<types::material_id> &material_ids,
246  const bool only_locally_owned = false);
247 
253  template <class Iterator>
254  bool
255  operator()(const Iterator &i) const;
256 
257  protected:
261  const std::set<types::material_id> material_ids;
265  const bool only_locally_owned;
266  };
267 
277  {
278  public:
283  ActiveFEIndexEqualTo(const unsigned int active_fe_index,
284  const bool only_locally_owned = false);
285 
291  ActiveFEIndexEqualTo(const std::set<unsigned int> &active_fe_indices,
292  const bool only_locally_owned = false);
293 
299  template <class Iterator>
300  bool
301  operator()(const Iterator &i) const;
302 
303  protected:
307  const std::set<unsigned int> active_fe_indices;
311  const bool only_locally_owned;
312  };
313 
322  {
323  public:
327  template <class Iterator>
328  bool
329  operator()(const Iterator &i) const;
330  };
331 } // namespace IteratorFilters
332 
333 
551 template <typename BaseIterator>
553 {
554 public:
558  using AccessorType = typename BaseIterator::AccessorType;
559 
564  template <typename Predicate>
565  FilteredIterator(Predicate p);
566 
584  template <typename Predicate>
585  FilteredIterator(Predicate p, const BaseIterator &bi);
586 
592 
600  operator=(const FilteredIterator &fi);
601 
608  operator=(const BaseIterator &fi);
609 
619  set_to_next_positive(const BaseIterator &bi);
620 
630  set_to_previous_positive(const BaseIterator &bi);
631 
638  bool
639  operator==(const FilteredIterator &fi) const;
640 
647  bool
648  operator==(const BaseIterator &fi) const;
649 
656  bool
657  operator!=(const FilteredIterator &fi) const;
658 
665  bool
666  operator!=(const BaseIterator &fi) const;
667 
674  bool
675  operator<(const FilteredIterator &fi) const;
676 
683  bool
684  operator<(const BaseIterator &fi) const;
685 
691  operator++();
692 
698  operator++(int);
699 
705  operator--();
706 
712  operator--(int);
713 
718  ExcInvalidElement,
719  BaseIterator,
720  << "The element " << arg1
721  << " with which you want to compare or which you want to"
722  << " assign from is invalid since it does not satisfy the predicate.");
723 
724 private:
735  {
736  public:
741  virtual ~PredicateBase() = default;
742 
747  virtual bool
748  operator()(const BaseIterator &bi) const = 0;
749 
754  virtual std::unique_ptr<PredicateBase>
755  clone() const = 0;
756  };
757 
758 
767  template <typename Predicate>
769  {
770  public:
774  PredicateTemplate(const Predicate &predicate);
775 
779  virtual bool
780  operator()(const BaseIterator &bi) const override;
781 
786  virtual std::unique_ptr<PredicateBase>
787  clone() const override;
788 
789  private:
793  const Predicate predicate;
794  };
795 
800  std::unique_ptr<const PredicateBase> predicate;
801 };
802 
803 
804 
814 template <typename BaseIterator, typename Predicate>
816 make_filtered_iterator(const BaseIterator &i, const Predicate &p)
817 {
819  fi.set_to_next_positive(i);
820  return fi;
821 }
822 
823 
824 
825 namespace internal
826 {
827  namespace FilteredIteratorImplementation
828  {
829  // The following classes create a nested sequence of
830  // FilteredIterator<FilteredIterator<...<BaseIterator>...>> with as many
831  // levels of FilteredIterator classes as there are elements in the TypeList
832  // if the latter is given as a std::tuple<Args...>.
833  template <typename BaseIterator, typename TypeList>
835 
836  template <typename BaseIterator, typename Predicate>
837  struct NestFilteredIterators<BaseIterator, std::tuple<Predicate>>
838  {
840  };
841 
842  template <typename BaseIterator, typename Predicate, typename... Targs>
843  struct NestFilteredIterators<BaseIterator, std::tuple<Predicate, Targs...>>
844  {
845  using type = ::FilteredIterator<
847  std::tuple<Targs...>>::type>;
848  };
849  } // namespace FilteredIteratorImplementation
850 } // namespace internal
851 
852 
853 
904 template <typename BaseIterator, typename Predicate>
907 {
909  FilteredIterator<BaseIterator> fi_end(p, *(i.end()));
910 
912 }
913 
914 
915 
971 template <typename BaseIterator, typename Predicate, typename... Targs>
973  typename internal::FilteredIteratorImplementation::
974  NestFilteredIterators<BaseIterator, std::tuple<Predicate, Targs...>>::type>
976  const Predicate & p,
977  const Targs... args)
978 {
979  // Recursively create filtered iterators, one predicate at a time
980  auto fi = filter_iterators(i, p);
981  return filter_iterators(fi, args...);
982 }
983 
984 
985 
1044 template <typename BaseIterator, typename Predicate>
1047 {
1048  return filter_iterators(i, p);
1049 }
1050 
1051 
1052 
1053 /* ------------------ Inline functions and templates ------------ */
1054 
1055 
1056 template <typename BaseIterator>
1057 template <typename Predicate>
1059  : predicate(new PredicateTemplate<Predicate>(p))
1060 {}
1061 
1062 
1063 
1064 template <typename BaseIterator>
1065 template <typename Predicate>
1067  const BaseIterator &bi)
1068  : BaseIterator(bi)
1069  , predicate(new PredicateTemplate<Predicate>(p))
1070 {
1071  if ((this->state() == IteratorState::valid) && !(*predicate)(*this))
1073 }
1074 
1075 
1076 
1077 template <typename BaseIterator>
1079  const FilteredIterator &fi)
1080  : // this construction looks strange, but without going through the
1081  // address of fi, GCC would not cast fi to the base class of type
1082  // BaseIterator but tries to go through constructing a new
1083  // BaseIterator with an Accessor.
1084  BaseIterator(*static_cast<const BaseIterator *>(&fi))
1085  , predicate(fi.predicate->clone())
1086 {}
1087 
1088 
1089 
1090 template <typename BaseIterator>
1093 {
1094  // Using equivalent code to the one for 'operator=(const BaseIterator &bi)'
1095  // below, some compiler would not cast fi to the base class of type
1096  // BaseIterator but try to go through constructing a new Accessor from fi
1097  // which fails. Hence, we just use an explicit upcast and call the above-
1098  // mentioned method.
1099  const BaseIterator &bi = fi;
1100  return operator=(bi);
1101 }
1102 
1103 
1104 
1105 template <typename BaseIterator>
1108 {
1109  Assert((bi.state() != IteratorState::valid) || (*predicate)(bi),
1110  ExcInvalidElement(bi));
1111  BaseIterator::operator=(bi);
1112  return *this;
1113 }
1114 
1115 
1116 
1117 template <typename BaseIterator>
1120 {
1121  BaseIterator::operator=(bi);
1122  while ((this->state() == IteratorState::valid) && (!(*predicate)(*this)))
1124 
1125  return *this;
1126 }
1127 
1128 
1129 
1130 template <typename BaseIterator>
1133 {
1134  BaseIterator::operator=(bi);
1135  while ((this->state() == IteratorState::valid) && (!(*predicate)(*this)))
1136  BaseIterator::operator--();
1137 
1138  return *this;
1139 }
1140 
1141 
1142 
1143 template <typename BaseIterator>
1144 inline bool
1146 {
1147  return (static_cast<const BaseIterator &>(*this) ==
1148  static_cast<const BaseIterator &>(fi));
1149 }
1150 
1151 
1152 
1153 template <typename BaseIterator>
1154 inline bool
1156 {
1157  return (static_cast<const BaseIterator &>(*this) !=
1158  static_cast<const BaseIterator &>(fi));
1159 }
1160 
1161 
1162 
1163 template <typename BaseIterator>
1164 inline bool
1166 {
1167  return (static_cast<const BaseIterator &>(*this) <
1168  static_cast<const BaseIterator &>(fi));
1169 }
1170 
1171 
1172 
1173 template <typename BaseIterator>
1174 inline bool
1175 FilteredIterator<BaseIterator>::operator==(const BaseIterator &bi) const
1176 {
1177  return (static_cast<const BaseIterator &>(*this) == bi);
1178 }
1179 
1180 
1181 
1182 template <typename BaseIterator>
1183 inline bool
1184 FilteredIterator<BaseIterator>::operator!=(const BaseIterator &bi) const
1185 {
1186  return (static_cast<const BaseIterator &>(*this) != bi);
1187 }
1188 
1189 
1190 
1191 template <typename BaseIterator>
1192 inline bool
1193 FilteredIterator<BaseIterator>::operator<(const BaseIterator &bi) const
1194 {
1195  return (static_cast<const BaseIterator &>(*this) < bi);
1196 }
1197 
1198 
1199 template <typename BaseIterator>
1202 {
1203  if (this->state() == IteratorState::valid)
1204  do
1206  while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1207  return *this;
1208 }
1209 
1210 
1211 
1212 template <typename BaseIterator>
1215 {
1216  const FilteredIterator old_state = *this;
1217 
1218  if (this->state() == IteratorState::valid)
1219  do
1221  while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1222  return old_state;
1223 }
1224 
1225 
1226 
1227 template <typename BaseIterator>
1230 {
1231  if (this->state() == IteratorState::valid)
1232  do
1233  BaseIterator::operator--();
1234  while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1235  return *this;
1236 }
1237 
1238 
1239 
1240 template <typename BaseIterator>
1243 {
1244  const FilteredIterator old_state = *this;
1245 
1246  if (this->state() == IteratorState::valid)
1247  do
1248  BaseIterator::operator--();
1249  while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1250  return old_state;
1251 }
1252 
1253 
1254 
1255 template <typename BaseIterator>
1256 template <typename Predicate>
1258  Predicate>::PredicateTemplate(const Predicate &predicate)
1259  : predicate(predicate)
1260 {}
1261 
1262 
1263 
1264 template <typename BaseIterator>
1265 template <typename Predicate>
1266 bool
1268  const BaseIterator &bi) const
1269 {
1270  return predicate(bi);
1271 }
1272 
1273 
1274 
1275 template <typename BaseIterator>
1276 template <typename Predicate>
1277 std::unique_ptr<typename FilteredIterator<BaseIterator>::PredicateBase>
1279 {
1280  return std::make_unique<PredicateTemplate>(predicate);
1281 }
1282 
1283 
1284 
1285 namespace IteratorFilters
1286 {
1287  // ---------------- IteratorFilters::Active ---------
1288 
1289  template <class Iterator>
1290  inline bool
1291  Active::operator()(const Iterator &i) const
1292  {
1293  return i->is_active();
1294  }
1295 
1296 
1297  // ---------------- IteratorFilters::UserFlagSet ---------
1298 
1299  template <class Iterator>
1300  inline bool
1301  UserFlagSet::operator()(const Iterator &i) const
1302  {
1303  return (i->user_flag_set());
1304  }
1305 
1306 
1307  // ---------------- IteratorFilters::UserFlagNotSet ---------
1308 
1309  template <class Iterator>
1310  inline bool
1311  UserFlagNotSet::operator()(const Iterator &i) const
1312  {
1313  return (!i->user_flag_set());
1314  }
1315 
1316 
1317  // ---------------- IteratorFilters::LevelEqualTo ---------
1318  inline LevelEqualTo::LevelEqualTo(const unsigned int level)
1319  : level(level)
1320  {}
1321 
1322 
1323 
1324  template <class Iterator>
1325  inline bool
1326  LevelEqualTo::operator()(const Iterator &i) const
1327  {
1328  return (static_cast<unsigned int>(i->level()) == level);
1329  }
1330 
1331 
1332 
1333  // ---------------- IteratorFilters::SubdomainEqualTo ---------
1336  : subdomain_id(subdomain_id)
1337  {}
1338 
1339 
1340 
1341  template <class Iterator>
1342  inline bool
1343  SubdomainEqualTo::operator()(const Iterator &i) const
1344  {
1345  return (i->subdomain_id() == subdomain_id);
1346  }
1347 
1348 
1349 
1350  // ---------------- IteratorFilters::LocallyOwnedCell ---------
1351 
1352  template <class Iterator>
1353  inline bool
1354  LocallyOwnedCell::operator()(const Iterator &i) const
1355  {
1356  return (i->is_locally_owned());
1357  }
1358 
1359 
1360  // ---------------- IteratorFilters::LocallyOwnedLevelCell ---------
1361 
1362  template <class Iterator>
1363  inline bool
1364  LocallyOwnedLevelCell::operator()(const Iterator &i) const
1365  {
1366  return (i->is_locally_owned_on_level());
1367  }
1368 
1369 
1370 
1371  // ---------------- IteratorFilters::MaterialIdEqualTo ---------
1374  const bool only_locally_owned)
1375  : material_ids{material_id}
1377  {}
1378 
1379 
1380 
1382  const std::set<types::material_id> &material_ids,
1383  const bool only_locally_owned)
1384  : material_ids(material_ids)
1385  , only_locally_owned(only_locally_owned)
1386  {}
1387 
1388 
1389 
1390  template <class Iterator>
1391  inline bool
1392  MaterialIdEqualTo::operator()(const Iterator &i) const
1393  {
1394  return only_locally_owned == true ?
1395  (material_ids.find(i->material_id()) != material_ids.end() &&
1396  i->is_locally_owned()) :
1397  material_ids.find(i->material_id()) != material_ids.end();
1398  }
1399 
1400 
1401 
1402  // ---------------- IteratorFilters::ActiveFEIndexEqualTo ---------
1404  const unsigned int active_fe_index,
1405  const bool only_locally_owned)
1406  : active_fe_indices{active_fe_index}
1408  {}
1409 
1410 
1411 
1413  const std::set<unsigned int> &active_fe_indices,
1414  const bool only_locally_owned)
1415  : active_fe_indices(active_fe_indices)
1416  , only_locally_owned(only_locally_owned)
1417  {}
1418 
1419 
1420 
1421  template <class Iterator>
1422  inline bool
1423  ActiveFEIndexEqualTo::operator()(const Iterator &i) const
1424  {
1425  return only_locally_owned == true ?
1426  (active_fe_indices.find(i->active_fe_index()) !=
1427  active_fe_indices.end() &&
1428  i->is_locally_owned()) :
1429  active_fe_indices.find(i->active_fe_index()) !=
1430  active_fe_indices.end();
1431  }
1432 
1433 
1434 
1435  // ---------------- IteratorFilters::AtBoundary ---------
1436 
1437  template <class Iterator>
1438  inline bool
1439  AtBoundary::operator()(const Iterator &i) const
1440  {
1441  return (i->at_boundary());
1442  }
1443 } // namespace IteratorFilters
1444 
1445 
1447 
1448 /*------------------------- filtered_iterator.h ------------------------*/
1449 #endif
1450 /*------------------------- filtered_iterator.h ------------------------*/
bool operator()(const Iterator &i) const
FilteredIterator & operator++()
IteratorOverIterators begin()
const types::subdomain_id subdomain_id
FilteredIterator(Predicate p)
bool operator()(const Iterator &i) const
bool operator==(const FilteredIterator &fi) const
IteratorRange< FilteredIterator< BaseIterator > > operator|(IteratorRange< BaseIterator > i, const Predicate &p)
bool operator()(const Iterator &i) const
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
unsigned int material_id
Definition: types.h:152
STL namespace.
bool operator<(const SynchronousIterators< Iterators > &a, const SynchronousIterators< Iterators > &b)
IteratorRange< FilteredIterator< BaseIterator > > filter_iterators(IteratorRange< BaseIterator > i, const Predicate &p)
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
FilteredIterator & operator--()
const std::set< types::material_id > material_ids
bool operator()(const Iterator &i) const
unsigned int subdomain_id
Definition: types.h:43
FilteredIterator & operator=(const FilteredIterator &fi)
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:509
#define Assert(cond, exc)
Definition: exceptions.h:1461
bool operator()(const Iterator &i) const
std::unique_ptr< const PredicateBase > predicate
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:401
unsigned int level
Definition: grid_out.cc:4614
bool operator!=(const FilteredIterator &fi) const
SynchronousIterators< Iterators > operator++(SynchronousIterators< Iterators > &a)
MaterialIdEqualTo(const types::material_id material_id, const bool only_locally_owned=false)
bool operator()(const Iterator &i) const
FilteredIterator< BaseIterator > make_filtered_iterator(const BaseIterator &i, const Predicate &p)
const std::set< unsigned int > active_fe_indices
FilteredIterator & set_to_next_positive(const BaseIterator &bi)
SubdomainEqualTo(const types::subdomain_id subdomain_id)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:400
static ::ExceptionBase & ExcInvalidElement(BaseIterator arg1)
bool operator()(const Iterator &i) const
ActiveFEIndexEqualTo(const unsigned int active_fe_index, const bool only_locally_owned=false)
Iterator points to a valid object.
FilteredIterator & set_to_previous_positive(const BaseIterator &bi)
bool operator<(const FilteredIterator &fi) const
typename BaseIterator::AccessorType AccessorType
bool operator()(const Iterator &i) const
IteratorOverIterators end() const