dune-typetree  2.4-dev
filters.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_TYPETREE_FILTERS_HH
5 #define DUNE_TYPETREE_FILTERS_HH
6 
7 #include <dune/common/tuples.hh>
8 #include <dune/common/typetraits.hh>
9 
10 namespace Dune {
11  namespace TypeTree {
12 
18  template<std::size_t new_k, std::size_t old_k>
20  struct FilterEntry
21  {
22 
23 #ifndef DOXYGEN
24 
25  // The precise contents of this class is an implementation detail.
26 
27  static const std::size_t filtered_index = new_k;
28  static const std::size_t original_index = old_k;
29 
30 #endif // DOXYGEN
31 
32  };
33 
35  template<typename... FilterEntries>
36  struct FilterResult
37  {
38 
39  static const std::size_t size = sizeof...(FilterEntries);
40 
41  typedef tuple<FilterEntries...> IndexMap;
42 
43  template<typename Node>
44  struct apply
45  {
46  typedef tuple<typename Node::template Child<FilterEntries::original_index>...> Children;
48  typedef tuple<typename Node::template Child<FilterEntries::original_index>::Storage...> NodeStorage;
49  };
50 
51  };
52 
54  struct SimpleFilterTag {};
55 
57  struct AdvancedFilterTag {};
58 
59 
62  {
63 
66 
67 #ifdef DOXYGEN
68 
70  template<typename Node, typename... Children>
71  struct apply
72  {
74 
77  typedef implementation-defined type;
78  };
79 
80 #endif // DOXYGEN
81 
82  };
83 
85 
90  struct SimpleFilter
91  {
92 
95 
96 
98  template<typename Node>
99  struct validate
100  {
102  static const bool value = true;
103  };
104 
106 
114  template<typename Child, std::size_t new_index, std::size_t old_index>
115  struct apply
116  {
118  static const bool value = true;
119  };
120 
121  };
122 
123  namespace {
124 
125  // ********************************************************************************
126  // IndexFilter helpers
127  // ********************************************************************************
128 
129  template<typename Node, std::size_t new_index, std::size_t... indices>
130  struct index_filter_helper
131  {
132  template<typename... FilterEntries>
133  struct apply
134  {
135  typedef FilterResult<FilterEntries...> type;
136  };
137  };
138 
139  template<typename Node, std::size_t new_index, std::size_t old_index, std::size_t... indices>
140  struct index_filter_helper<Node,new_index,old_index,indices...>
141  {
142  template<typename... FilterEntries>
143  struct apply
144  : public index_filter_helper<Node,new_index+1,indices...>::template apply<FilterEntries...,
145  FilterEntry<new_index,
146  old_index>
147  >
148  {};
149  };
150 
151  } // anonymous namespace
152 
153 
155  template<std::size_t... indices>
156  struct IndexFilter
157  : public AdvancedFilter
158  {
159 
160 #ifndef DOXYGEN
161 
162  template<typename Node, typename... Children>
163  struct apply
164  {
165  typedef typename index_filter_helper<Node,0,indices...>::template apply<>::type type;
166  };
167 
168 #endif // DOXYGEN
169 
170  };
171 
172 
173  // ********************************************************************************
174  // filter: Wrapper class for turning a simple filter into an advanced filter
175  // usable by FilteredCompositeNode
176  // ********************************************************************************
177 
178  namespace {
179 
180  template<typename Filter, std::size_t new_k, std::size_t old_k, typename... tail>
181  struct filter_helper
182  {
183  template<typename... FilterDescriptors>
184  struct apply
185  {
186  typedef FilterResult<FilterDescriptors...> type;
187  };
188  };
189 
190  template<typename Filter, std::size_t new_k, std::size_t old_k, typename child, typename... tail>
191  struct filter_helper<Filter,new_k,old_k,child,tail...>
192  {
193 
194  template<typename... FilterDescriptors>
195  struct apply
196  : public Dune::conditional<Filter::template apply<child,new_k,old_k>::value,
197  typename filter_helper<Filter,new_k+1,old_k+1,tail...>::template apply<FilterDescriptors...,FilterEntry<new_k,old_k> >,
198  typename filter_helper<Filter,new_k,old_k+1,tail...>::template apply<FilterDescriptors...>
199  >::type
200  {};
201 
202  };
203 
204  } // anonymous namespace
205 
207  template<typename Filter>
208  struct filter
209  {
210 
212  template<typename Node, typename... Children>
213  struct apply
214  {
215 
216  static_assert((Filter::template validate<Node>::value),"Invalid simple filter");
217 
218  typedef typename filter_helper<Filter,0,0,Children...>::template apply<>::type type;
219 
220  };
221 
222  };
223 
225 
226  } // namespace TypeTree
227 } //namespace Dune
228 
229 #endif // DUNE_TYPETREE_FILTERS_HH
Validates the combination of filter and node.
Definition: filters.hh:99
Apply this filter to the given node and children.
Definition: filters.hh:71
Default simple filter that accepts any node and leaves its child structure unchanged.
Definition: filters.hh:90
SimpleFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition: filters.hh:94
Definition: accumulate_static.hh:12
implementation defined type
The result of the filtering process.
Definition: filters.hh:77
filter_helper< Filter, 0, 0, Children...>::template apply::type type
Definition: filters.hh:216
static const bool value
True if the child will be included in the filtered node.
Definition: filters.hh:118
tuple< typename Node::template Child< FilterEntries::original_index >::Type...> ChildTypes
Definition: filters.hh:47
Tag describing an advanced filter that has full control over the construction of the list of FilterEn...
Definition: filters.hh:57
Definition: filters.hh:44
Apply the filter.
Definition: filters.hh:213
static const bool value
True if the combination of filter and node is valid.
Definition: filters.hh:102
Adapter class that takes a SimpleFilter, validated it and turns it into an AdvancedFilter.
Definition: filters.hh:208
Base class for advanced filters.
Definition: filters.hh:61
The result of a filter.
Definition: filters.hh:36
Filter class for FilteredCompositeNode that selects the children with the given indices.
Definition: filters.hh:156
tuple< FilterEntries...> IndexMap
Definition: filters.hh:41
Type
Definition: treepath.hh:25
Applies the filter to the given child node.
Definition: filters.hh:115
tuple< typename Node::template Child< FilterEntries::original_index >...> Children
Definition: filters.hh:46
static const std::size_t size
Definition: filters.hh:39
A filter entry describing the mapping of one child in the filtered node.
Definition: filters.hh:20
tuple< typename Node::template Child< FilterEntries::original_index >::Storage...> NodeStorage
Definition: filters.hh:48
Tag describing a simple filter that can only decide whether or not to include a single given child...
Definition: filters.hh:54
AdvancedFilterTag FilterTag
Filter tag for deciding on filter application mechanism.
Definition: filters.hh:65