Reference documentation for deal.II version 9.4.0
\(\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 - 2021 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
24
26
27# include <memory>
28# include <set>
29# include <tuple>
30
32
33
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;
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;
312 };
313
322 {
323 public:
327 template <class Iterator>
328 bool
329 operator()(const Iterator &i) const;
330 };
331
332
341 {
342 public:
348
353 BoundaryIdEqualTo(const std::set<types::boundary_id> &boundary_ids);
354
359 template <class Iterator>
360 bool
361 operator()(const Iterator &i) const;
362
363 protected:
367 const std::set<types::boundary_id> boundary_ids;
368 };
369
370
379 {
380 public:
386
391 ManifoldIdEqualTo(const std::set<types::manifold_id> &manifold_ids);
392
397 template <class Iterator>
398 bool
399 operator()(const Iterator &i) const;
400
401 protected:
405 const std::set<types::manifold_id> manifold_ids;
406 };
407} // namespace IteratorFilters
408
409
627template <typename BaseIterator>
629{
630public:
634 using AccessorType = typename BaseIterator::AccessorType;
635
640 template <typename Predicate>
641 FilteredIterator(Predicate p);
642
660 template <typename Predicate>
661 FilteredIterator(Predicate p, const BaseIterator &bi);
662
668
676 operator=(const FilteredIterator &fi);
677
684 operator=(const BaseIterator &fi);
685
696
707
714 bool
715 operator==(const FilteredIterator &fi) const;
716
723 bool
724 operator==(const BaseIterator &fi) const;
725
732 bool
733 operator!=(const FilteredIterator &fi) const;
734
741 bool
742 operator!=(const BaseIterator &fi) const;
743
750 bool
751 operator<(const FilteredIterator &fi) const;
752
759 bool
760 operator<(const BaseIterator &fi) const;
761
767 operator++();
768
774 operator++(int);
775
781 operator--();
782
788 operator--(int);
789
796 << "The element " << arg1
797 << " with which you want to compare or which you want to"
798 << " assign from is invalid since it does not satisfy the predicate.");
799
800private:
811 {
812 public:
817 virtual ~PredicateBase() = default;
818
823 virtual bool
824 operator()(const BaseIterator &bi) const = 0;
825
830 virtual std::unique_ptr<PredicateBase>
831 clone() const = 0;
832 };
833
834
843 template <typename Predicate>
845 {
846 public:
850 PredicateTemplate(const Predicate &predicate);
851
855 virtual bool
856 operator()(const BaseIterator &bi) const override;
857
862 virtual std::unique_ptr<PredicateBase>
863 clone() const override;
864
865 private:
869 const Predicate predicate;
870 };
871
876 std::unique_ptr<const PredicateBase> predicate;
877};
878
879
880
890template <typename BaseIterator, typename Predicate>
892make_filtered_iterator(const BaseIterator &i, const Predicate &p)
893{
896 return fi;
897}
898
899
900
901namespace internal
902{
903 namespace FilteredIteratorImplementation
904 {
905 // The following classes create a nested sequence of
906 // FilteredIterator<FilteredIterator<...<BaseIterator>...>> with as many
907 // levels of FilteredIterator classes as there are elements in the TypeList
908 // if the latter is given as a std::tuple<Args...>.
909 template <typename BaseIterator, typename TypeList>
911
912 template <typename BaseIterator, typename Predicate>
913 struct NestFilteredIterators<BaseIterator, std::tuple<Predicate>>
914 {
916 };
917
918 template <typename BaseIterator, typename Predicate, typename... Targs>
919 struct NestFilteredIterators<BaseIterator, std::tuple<Predicate, Targs...>>
920 {
923 std::tuple<Targs...>>::type>;
924 };
925 } // namespace FilteredIteratorImplementation
926} // namespace internal
927
928
929
980template <typename BaseIterator, typename Predicate>
983{
985 FilteredIterator<BaseIterator> fi_end(p, *(i.end()));
986
988}
989
990
991
1047template <typename BaseIterator, typename Predicate, typename... Targs>
1049 typename internal::FilteredIteratorImplementation::
1050 NestFilteredIterators<BaseIterator, std::tuple<Predicate, Targs...>>::type>
1052 const Predicate & p,
1053 const Targs... args)
1054{
1055 // Recursively create filtered iterators, one predicate at a time
1056 auto fi = filter_iterators(i, p);
1057 return filter_iterators(fi, args...);
1058}
1059
1060
1061
1120template <typename BaseIterator, typename Predicate>
1123{
1124 return filter_iterators(i, p);
1125}
1126
1127
1128
1129/* ------------------ Inline functions and templates ------------ */
1130
1131
1132template <typename BaseIterator>
1133template <typename Predicate>
1135 : predicate(new PredicateTemplate<Predicate>(p))
1136{}
1137
1138
1139
1140template <typename BaseIterator>
1141template <typename Predicate>
1143 const BaseIterator &bi)
1144 : BaseIterator(bi)
1145 , predicate(new PredicateTemplate<Predicate>(p))
1146{
1147 if ((this->state() == IteratorState::valid) && !(*predicate)(*this))
1149}
1150
1151
1152
1153template <typename BaseIterator>
1155 const FilteredIterator &fi)
1156 : // this construction looks strange, but without going through the
1157 // address of fi, GCC would not cast fi to the base class of type
1158 // BaseIterator but tries to go through constructing a new
1159 // BaseIterator with an Accessor.
1160 BaseIterator(*static_cast<const BaseIterator *>(&fi))
1161 , predicate(fi.predicate->clone())
1162{}
1163
1164
1165
1166template <typename BaseIterator>
1169{
1170 // Using equivalent code to the one for 'operator=(const BaseIterator &bi)'
1171 // below, some compiler would not cast fi to the base class of type
1172 // BaseIterator but try to go through constructing a new Accessor from fi
1173 // which fails. Hence, we just use an explicit upcast and call the above-
1174 // mentioned method.
1175 const BaseIterator &bi = fi;
1176 return operator=(bi);
1177}
1178
1179
1180
1181template <typename BaseIterator>
1184{
1185 Assert((bi.state() != IteratorState::valid) || (*predicate)(bi),
1186 ExcInvalidElement(bi));
1187 BaseIterator::operator=(bi);
1188 return *this;
1189}
1190
1191
1192
1193template <typename BaseIterator>
1196{
1197 BaseIterator::operator=(bi);
1198 while ((this->state() == IteratorState::valid) && (!(*predicate)(*this)))
1200
1201 return *this;
1202}
1203
1204
1205
1206template <typename BaseIterator>
1209{
1210 BaseIterator::operator=(bi);
1211 while ((this->state() == IteratorState::valid) && (!(*predicate)(*this)))
1212 BaseIterator::operator--();
1213
1214 return *this;
1215}
1216
1217
1218
1219template <typename BaseIterator>
1220inline bool
1222{
1223 return (static_cast<const BaseIterator &>(*this) ==
1224 static_cast<const BaseIterator &>(fi));
1225}
1226
1227
1228
1229template <typename BaseIterator>
1230inline bool
1232{
1233 return (static_cast<const BaseIterator &>(*this) !=
1234 static_cast<const BaseIterator &>(fi));
1235}
1236
1237
1238
1239template <typename BaseIterator>
1240inline bool
1242{
1243 return (static_cast<const BaseIterator &>(*this) <
1244 static_cast<const BaseIterator &>(fi));
1245}
1246
1247
1248
1249template <typename BaseIterator>
1250inline bool
1252{
1253 return (static_cast<const BaseIterator &>(*this) == bi);
1254}
1255
1256
1257
1258template <typename BaseIterator>
1259inline bool
1261{
1262 return (static_cast<const BaseIterator &>(*this) != bi);
1263}
1264
1265
1266
1267template <typename BaseIterator>
1268inline bool
1270{
1271 return (static_cast<const BaseIterator &>(*this) < bi);
1272}
1273
1274
1275template <typename BaseIterator>
1278{
1279 if (this->state() == IteratorState::valid)
1280 do
1282 while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1283 return *this;
1284}
1285
1286
1287
1288template <typename BaseIterator>
1291{
1292 const FilteredIterator old_state = *this;
1293
1294 if (this->state() == IteratorState::valid)
1295 do
1297 while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1298 return old_state;
1299}
1300
1301
1302
1303template <typename BaseIterator>
1306{
1307 if (this->state() == IteratorState::valid)
1308 do
1309 BaseIterator::operator--();
1310 while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1311 return *this;
1312}
1313
1314
1315
1316template <typename BaseIterator>
1319{
1320 const FilteredIterator old_state = *this;
1321
1322 if (this->state() == IteratorState::valid)
1323 do
1324 BaseIterator::operator--();
1325 while ((this->state() == IteratorState::valid) && !(*predicate)(*this));
1326 return old_state;
1327}
1328
1329
1330
1331template <typename BaseIterator>
1332template <typename Predicate>
1334 Predicate>::PredicateTemplate(const Predicate &predicate)
1335 : predicate(predicate)
1336{}
1337
1338
1339
1340template <typename BaseIterator>
1341template <typename Predicate>
1342bool
1344 const BaseIterator &bi) const
1345{
1346 return predicate(bi);
1347}
1348
1349
1350
1351template <typename BaseIterator>
1352template <typename Predicate>
1353std::unique_ptr<typename FilteredIterator<BaseIterator>::PredicateBase>
1355{
1356 return std::make_unique<PredicateTemplate>(predicate);
1357}
1358
1359
1360
1361namespace IteratorFilters
1362{
1363 // ---------------- IteratorFilters::Active ---------
1364
1365 template <class Iterator>
1366 inline bool
1367 Active::operator()(const Iterator &i) const
1368 {
1369 return i->is_active();
1370 }
1371
1372
1373 // ---------------- IteratorFilters::UserFlagSet ---------
1374
1375 template <class Iterator>
1376 inline bool
1377 UserFlagSet::operator()(const Iterator &i) const
1378 {
1379 return (i->user_flag_set());
1380 }
1381
1382
1383 // ---------------- IteratorFilters::UserFlagNotSet ---------
1384
1385 template <class Iterator>
1386 inline bool
1387 UserFlagNotSet::operator()(const Iterator &i) const
1388 {
1389 return (!i->user_flag_set());
1390 }
1391
1392
1393 // ---------------- IteratorFilters::LevelEqualTo ---------
1394 inline LevelEqualTo::LevelEqualTo(const unsigned int level)
1395 : level(level)
1396 {}
1397
1398
1399
1400 template <class Iterator>
1401 inline bool
1402 LevelEqualTo::operator()(const Iterator &i) const
1403 {
1404 return (static_cast<unsigned int>(i->level()) == level);
1405 }
1406
1407
1408
1409 // ---------------- IteratorFilters::SubdomainEqualTo ---------
1413 {}
1414
1415
1416
1417 template <class Iterator>
1418 inline bool
1419 SubdomainEqualTo::operator()(const Iterator &i) const
1420 {
1421 return (i->subdomain_id() == subdomain_id);
1422 }
1423
1424
1425
1426 // ---------------- IteratorFilters::LocallyOwnedCell ---------
1427
1428 template <class Iterator>
1429 inline bool
1430 LocallyOwnedCell::operator()(const Iterator &i) const
1431 {
1432 return (i->is_locally_owned());
1433 }
1434
1435
1436 // ---------------- IteratorFilters::LocallyOwnedLevelCell ---------
1437
1438 template <class Iterator>
1439 inline bool
1440 LocallyOwnedLevelCell::operator()(const Iterator &i) const
1441 {
1442 return (i->is_locally_owned_on_level());
1443 }
1444
1445
1446
1447 // ---------------- IteratorFilters::MaterialIdEqualTo ---------
1450 const bool only_locally_owned)
1451 : material_ids{material_id}
1452 , only_locally_owned(only_locally_owned)
1453 {}
1454
1455
1456
1458 const std::set<types::material_id> &material_ids,
1459 const bool only_locally_owned)
1460 : material_ids(material_ids)
1461 , only_locally_owned(only_locally_owned)
1462 {}
1463
1464
1465
1466 template <class Iterator>
1467 inline bool
1468 MaterialIdEqualTo::operator()(const Iterator &i) const
1469 {
1470 return only_locally_owned == true ?
1471 (material_ids.find(i->material_id()) != material_ids.end() &&
1472 i->is_locally_owned()) :
1473 material_ids.find(i->material_id()) != material_ids.end();
1474 }
1475
1476
1477
1478 // ---------------- IteratorFilters::ActiveFEIndexEqualTo ---------
1480 const unsigned int active_fe_index,
1481 const bool only_locally_owned)
1482 : active_fe_indices{active_fe_index}
1483 , only_locally_owned(only_locally_owned)
1484 {}
1485
1486
1487
1489 const std::set<unsigned int> &active_fe_indices,
1490 const bool only_locally_owned)
1491 : active_fe_indices(active_fe_indices)
1492 , only_locally_owned(only_locally_owned)
1493 {}
1494
1495
1496
1497 template <class Iterator>
1498 inline bool
1499 ActiveFEIndexEqualTo::operator()(const Iterator &i) const
1500 {
1501 return only_locally_owned == true ?
1502 (active_fe_indices.find(i->active_fe_index()) !=
1503 active_fe_indices.end() &&
1504 i->is_locally_owned()) :
1505 active_fe_indices.find(i->active_fe_index()) !=
1506 active_fe_indices.end();
1507 }
1508
1509
1510
1511 // ---------------- IteratorFilters::AtBoundary ---------
1512
1513 template <class Iterator>
1514 inline bool
1515 AtBoundary::operator()(const Iterator &i) const
1516 {
1517 return (i->at_boundary());
1518 }
1519
1520
1521
1522 // ---------------- IteratorFilters::BoundaryIdEqualTo ---------
1525 : boundary_ids{boundary_id}
1526 {}
1527
1528
1529
1531 const std::set<types::boundary_id> &boundary_ids)
1532 : boundary_ids(boundary_ids)
1533 {}
1534
1535
1536
1537 template <class Iterator>
1538 inline bool
1539 BoundaryIdEqualTo::operator()(const Iterator &i) const
1540 {
1541 return boundary_ids.find(i->boundary_id()) != boundary_ids.end();
1542 }
1543
1544
1545
1546 // ---------------- IteratorFilters::ManifoldIdEqualTo ---------
1549 : manifold_ids{manifold_id}
1550 {}
1551
1552
1553
1555 const std::set<types::manifold_id> &manifold_ids)
1556 : manifold_ids(manifold_ids)
1557 {}
1558
1559
1560
1561 template <class Iterator>
1562 inline bool
1563 ManifoldIdEqualTo::operator()(const Iterator &i) const
1564 {
1565 return manifold_ids.find(i->manifold_id()) != manifold_ids.end();
1566 }
1567} // namespace IteratorFilters
1568
1569
1571
1572/*------------------------- filtered_iterator.h ------------------------*/
1573#endif
1574/*------------------------- filtered_iterator.h ------------------------*/
virtual ~PredicateBase()=default
virtual bool operator()(const BaseIterator &bi) const =0
virtual std::unique_ptr< PredicateBase > clone() const =0
virtual bool operator()(const BaseIterator &bi) const override
PredicateTemplate(const Predicate &predicate)
virtual std::unique_ptr< PredicateBase > clone() const override
FilteredIterator & set_to_next_positive(const BaseIterator &bi)
FilteredIterator & set_to_previous_positive(const BaseIterator &bi)
FilteredIterator & operator--()
typename BaseIterator::AccessorType AccessorType
bool operator==(const FilteredIterator &fi) const
bool operator!=(const FilteredIterator &fi) const
FilteredIterator< BaseIterator > make_filtered_iterator(const BaseIterator &i, const Predicate &p)
FilteredIterator(Predicate p)
bool operator<(const FilteredIterator &fi) const
FilteredIterator & operator=(const FilteredIterator &fi)
std::unique_ptr< const PredicateBase > predicate
FilteredIterator & operator++()
bool operator()(const Iterator &i) const
const std::set< unsigned int > active_fe_indices
ActiveFEIndexEqualTo(const unsigned int active_fe_index, const bool only_locally_owned=false)
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const
const std::set< types::boundary_id > boundary_ids
BoundaryIdEqualTo(const types::boundary_id boundary_id)
bool operator()(const Iterator &i) const
LevelEqualTo(const unsigned int level)
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const
ManifoldIdEqualTo(const types::manifold_id manifold_id)
const std::set< types::manifold_id > manifold_ids
bool operator()(const Iterator &i) const
MaterialIdEqualTo(const types::material_id material_id, const bool only_locally_owned=false)
const std::set< types::material_id > material_ids
SubdomainEqualTo(const types::subdomain_id subdomain_id)
bool operator()(const Iterator &i) const
const types::subdomain_id subdomain_id
bool operator()(const Iterator &i) const
bool operator()(const Iterator &i) const
IteratorOverIterators end() const
IteratorOverIterators begin()
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
unsigned int level
Definition: grid_out.cc:4606
IteratorRange< typename internal::FilteredIteratorImplementation::NestFilteredIterators< BaseIterator, std::tuple< Predicate, Targs... > >::type > filter_iterators(IteratorRange< BaseIterator > i, const Predicate &p, const Targs... args)
IteratorRange< FilteredIterator< BaseIterator > > filter_iterators(IteratorRange< BaseIterator > i, const Predicate &p)
IteratorRange< FilteredIterator< BaseIterator > > operator|(IteratorRange< BaseIterator > i, const Predicate &p)
static ::ExceptionBase & ExcInvalidElement(BaseIterator arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:509
@ valid
Iterator points to a valid object.
STL namespace.
unsigned int manifold_id
Definition: types.h:141
unsigned int subdomain_id
Definition: types.h:43
unsigned int material_id
Definition: types.h:152
unsigned int boundary_id
Definition: types.h:129
SynchronousIterators< Iterators > operator++(SynchronousIterators< Iterators > &a)