GCC Code Coverage Report


Directory: gridformat/
File: gridformat/grid/filtered.hpp
Date: 2025-04-11 15:08:41
Exec Total Coverage
Lines: 19 19 100.0%
Functions: 223 241 92.5%
Branches: 3 5 60.0%

Line Branch Exec Source
1 // SPDX-FileCopyrightText: 2025 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2 // SPDX-License-Identifier: MIT
3 /*!
4 * \file
5 * \ingroup Grid
6 * \brief Wrapper around a grid that exposes only those cells fulfilling a given predicate.
7 */
8 #ifndef GRIDFORMAT_GRID_FILTERED_HPP_
9 #define GRIDFORMAT_GRID_FILTERED_HPP_
10
11 #include <type_traits>
12 #include <concepts>
13 #include <ranges>
14
15 #include <gridformat/common/type_traits.hpp>
16 #include <gridformat/common/ranges.hpp>
17
18 #include <gridformat/grid/concepts.hpp>
19 #include <gridformat/grid/type_traits.hpp>
20 #include <gridformat/grid/traits.hpp>
21
22 namespace GridFormat {
23
24 /*!
25 * \ingroup Grid
26 * \brief Wrapper around a grid that exposes only those cells fulfilling a given predicate.
27 * \note The wrapped grid fulfills the UnstructuredGrid concept and thus requires the given grid to do so as well.
28 * \note All points of the original grid are exposed, i.e. they are not filtered to contain only those connected to
29 * the filtered cells.
30 */
31 template<Concepts::UnstructuredGrid G, typename Predicate>
32 requires(
33 std::invocable<const std::remove_cvref_t<Predicate>&, const Cell<G>&> and
34 std::convertible_to<bool, std::invoke_result_t<const std::remove_cvref_t<Predicate>&, const Cell<G>&>>
35 )
36 class FilteredGrid {
37 using StoredPredicate = LVReferenceOrValue<Predicate>;
38
39 public:
40 template<typename P> requires(std::convertible_to<Predicate, StoredPredicate>)
41 1849 explicit FilteredGrid(const G& grid, P&& p) noexcept
42 1849 : _grid{grid}
43 1849 , _p{std::forward<P>(p)} {
44 1849 _number_of_cells = Ranges::size(std::views::filter(Traits::Cells<G>::get(_grid), _p));
45 1849 }
46
47 1 std::size_t number_of_cells() const {
48 1 return _number_of_cells;
49 }
50
51 53998 const G& unwrap() const {
52 53998 return _grid;
53 }
54
55 6912 const std::remove_cvref_t<Predicate>& predicate() const {
56 6912 return _p;
57 }
58
59 private:
60 const G& _grid;
61 StoredPredicate _p;
62 std::size_t _number_of_cells;
63 };
64
65 template<typename G, typename P> requires(Concepts::UnstructuredGrid<std::remove_cvref_t<G>>, std::is_lvalue_reference_v<G>)
66 FilteredGrid(G&&, P&&) -> FilteredGrid<std::remove_cvref_t<G>, P>;
67
68
69 #ifndef DOXYGEN
70 namespace Traits {
71
72 template<Concepts::UnstructuredGrid G, typename P>
73 struct Points<FilteredGrid<G, P>> {
74 static std::ranges::range auto get(const FilteredGrid<G, P>& grid) {
75 return Points<G>::get(grid.unwrap());
76 }
77 };
78
79 template<Concepts::UnstructuredGrid G, typename P>
80 struct Cells<FilteredGrid<G, P>> {
81 6912 static std::ranges::range auto get(const FilteredGrid<G, P>& grid) {
82
3/5
✓ Branch 3 taken 368 times.
✓ Branch 4 taken 3088 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 368 times.
✗ Branch 7 not taken.
6912 return std::views::filter(Cells<G>::get(grid.unwrap()), grid.predicate());
83 }
84 };
85
86 template<Concepts::UnstructuredGrid G, typename P>
87 struct PointCoordinates<FilteredGrid<G, P>, Point<G>> {
88 static decltype(auto) get(const FilteredGrid<G, P>& grid, const Point<G>& p) {
89 return PointCoordinates<G, Point<G>>::get(grid.unwrap(), p);
90 }
91 };
92
93 template<Concepts::UnstructuredGrid G, typename P>
94 struct CellPoints<FilteredGrid<G, P>, Cell<G>> {
95 21492 static auto get(const FilteredGrid<G, P>& grid, const Cell<G>& c) {
96 21492 return CellPoints<G, Cell<G>>::get(grid.unwrap(), c);
97 }
98 };
99
100 template<Concepts::UnstructuredGrid G, typename P>
101 struct PointId<FilteredGrid<G, P>, Point<G>> {
102 12797 static auto get(const FilteredGrid<G, P>& grid, const Point<G>& p) {
103 12797 return PointId<G, Point<G>>::get(grid.unwrap(), p);
104 }
105 };
106
107 template<Concepts::UnstructuredGrid G, typename P>
108 struct CellType<FilteredGrid<G, P>, Cell<G>> {
109 static auto get(const FilteredGrid<G, P>& grid, const Cell<G>& c) {
110 return CellType<G, Cell<G>>::get(grid.unwrap(), c);
111 }
112 };
113
114 template<Concepts::UnstructuredGrid G, typename P>
115 struct NumberOfCells<FilteredGrid<G, P>> {
116 1 static auto get(const FilteredGrid<G, P>& grid) {
117 1 return grid.number_of_cells();
118 }
119 };
120
121 } // namespace Traits
122 #endif // DOXYGEN
123
124 } // namespace GridFormat
125
126 #endif // GRIDFORMAT_GRID_FILTERED_GRID_HPP_
127