Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
spatial_closed_region.hpp
1 // -*- C++ -*-
2 //
3 // Copyright Sylvain Bougerel 2009 - 2013.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file COPYING or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
16 #ifndef SPATIAL_CLOSED_REGION_HPP
17 #define SPATIAL_CLOSED_REGION_HPP
18 
19 #include "spatial_region.hpp"
20 
21 namespace spatial
22 {
41  template <typename Key, typename Compare>
43  : private Compare // empty member optimization
44  {
45  public:
49  closed_bounds() : Compare(), _lower(), _upper() { }
50 
55  closed_bounds(const Compare& compare, const Key& lower,
56  const Key& upper)
57  : Compare(compare), _lower(lower), _upper(upper)
58  { }
59 
64  operator()(dimension_type dim, dimension_type, const Key& key) const
65  {
66  return (Compare::operator()(dim, key, _lower)
67  ? below
68  : (Compare::operator()(dim, _upper, key)
69  ? above
70  : matching));
71  }
72 
73  private:
77  Key _lower;
78 
82  Key _upper;
83  };
84 
103  template <typename Tp>
107  (const Tp& container,
108  const typename container_traits<Tp>::key_type& lower,
109  const typename container_traits<Tp>::key_type& upper)
110  {
111  except::check_closed_bounds(container, lower, upper);
112  return closed_bounds
115  (container.key_comp(), lower, upper);
116  }
117 
118  template<typename Ct>
121  <Ct, closed_bounds<typename container_traits<Ct>::key_type,
122  typename container_traits<Ct>::key_compare> >
123  {
125 
130  other)
134  (other) { }
135  };
136 
137  template<typename Ct>
138  struct closed_region_iterator<const Ct>
140  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
141  typename container_traits<Ct>::key_compare> >
142  {
144 
147  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
149  other)
153  (other) { }
154 
159  other)
161  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
163  (other) { }
164  };
165 
166  template<typename Ct>
169  <Ct, closed_bounds<typename container_traits<Ct>::key_type,
170  typename container_traits<Ct>::key_compare> >
171  {
173 
177  typename container_traits<Ct>::key_compare> >& a,
178  const region_iterator
180  typename container_traits<Ct>::key_compare> >& b)
182  <Ct, closed_bounds<typename container_traits<Ct>::key_type,
184  (a, b) { }
185  };
186 
187  template<typename Ct>
190  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
191  typename container_traits<Ct>::key_compare> >
192  {
194 
197  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
198  typename container_traits<Ct>::key_compare> >& a,
199  const region_iterator
200  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
201  typename container_traits<Ct>::key_compare> >& b)
203  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
205  (a, b) { }
206 
209  <const Ct, closed_bounds<typename container_traits<Ct>::key_type,
210  typename container_traits<Ct>::key_compare> >
211  (other) { }
212  };
213 
214  template <typename Ct>
215  inline closed_region_iterator<Ct>
216  closed_region_end(Ct& container,
217  const typename container_traits<Ct>::key_type& lower,
218  const typename container_traits<Ct>::key_type& upper)
219  { return region_end(container, make_closed_bounds(container, lower, upper)); }
220 
221  template <typename Ct>
222  inline closed_region_iterator<const Ct>
223  closed_region_end(const Ct& container,
224  const typename container_traits<Ct>::key_type& lower,
225  const typename container_traits<Ct>::key_type& upper)
226  { return region_end(container, make_closed_bounds(container, lower, upper)); }
227 
228  template <typename Ct>
229  inline closed_region_iterator<const Ct>
230  closed_region_cend(const Ct& container,
231  const typename container_traits<Ct>::key_type& lower,
232  const typename container_traits<Ct>::key_type& upper)
233  {
234  return region_cend(container, make_closed_bounds(container, lower, upper));
235  }
236 
237  template <typename Ct>
238  inline closed_region_iterator<Ct>
239  closed_region_begin(Ct& container,
240  const typename container_traits<Ct>::key_type& lower,
241  const typename container_traits<Ct>::key_type& upper)
242  {
243  return region_begin(container, make_closed_bounds(container, lower, upper));
244  }
245 
246  template <typename Ct>
247  inline closed_region_iterator<const Ct>
248  closed_region_begin(const Ct& container,
249  const typename container_traits<Ct>::key_type& lower,
250  const typename container_traits<Ct>::key_type& upper)
251  {
252  return region_begin(container, make_closed_bounds(container, lower, upper));
253  }
254 
255  template <typename Ct>
256  inline closed_region_iterator<const Ct>
257  closed_region_cbegin(const Ct& container,
258  const typename container_traits<Ct>::key_type& lower,
259  const typename container_traits<Ct>::key_type& upper)
260  {
261  return region_cbegin(container,
262  make_closed_bounds(container, lower, upper));
263  }
264 
265  template <typename Ct>
266  inline closed_region_iterator_pair<Ct>
267  closed_region_range(Ct& container,
268  const typename container_traits<Ct>::key_type& lower,
269  const typename container_traits<Ct>::key_type& upper)
270  {
271  return region_range(container, make_closed_bounds(container, lower, upper));
272  }
273 
274  template <typename Ct>
275  inline closed_region_iterator_pair<const Ct>
276  closed_region_range(const Ct& container,
277  const typename container_traits<Ct>::key_type& lower,
278  const typename container_traits<Ct>::key_type& upper)
279  {
280  return region_range(container, make_closed_bounds(container, lower, upper));
281  }
282 
283  template <typename Ct>
284  inline closed_region_iterator_pair<const Ct>
285  closed_region_crange(const Ct& container,
286  const typename container_traits<Ct>::key_type& lower,
287  const typename container_traits<Ct>::key_type& upper)
288  {
289  return region_crange(container,
290  make_closed_bounds(container, lower, upper));
291  }
292 
293 } // namespace spatial
294 
295 #endif // SPATIAL_CLOSED_REGION_HPP
closed_region_iterator_pair< Ct > closed_region_range(Ct &container, const typename container_traits< Ct >::key_type &lower, const typename container_traits< Ct >::key_type &upper)
This type provides both an iterator and a constant iterator to iterate through all elements of a tree...
closed_region_iterator< Ct > closed_region_begin(Ct &container, const typename container_traits< Ct >::key_type &lower, const typename container_traits< Ct >::key_type &upper)
Tp::key_compare key_compare
Comparison functor used to compare two instances of key_type.
Definition: traits.hpp:98
region_iterator< const Ct, Predicate > region_cend(const Ct &container, const Predicate &pred)
This structure defines a pair of mutable region iterator.
Tp::key_type key_type
The type representing the key managed by the container.
Definition: traits.hpp:48
closed_region_iterator_pair(const closed_region_iterator_pair< Ct > &other)
Key _lower
The lower bound for the orthogonal region iterator.
closed_bounds(const Compare &compare, const Key &lower, const Key &upper)
Set the lower and upper boundary for the orthogonal region search.
region_iterator< Ct, Predicate > region_begin(Ct &container, const Predicate &pred)
region_iterator_pair< Ct, Predicate > region_range(Ct &container, const Predicate &pred)
Returns a spatial::region_iterator_pair where first points to the first element matching the predicat...
The traits type for all containers in the spatial namespace.
Definition: traits.hpp:42
region_iterator< const Ct, Predicate > region_cbegin(const Ct &container, const Predicate &pred)
relative_order
Defines values for relative ordering.
Definition: spatial.hpp:81
std::size_t dimension_type
Defines the type for the dimension as being a size.
Definition: spatial.hpp:71
region_iterator< Ct, Predicate > region_end(Ct &container, const Predicate &pred)
closed_region_iterator< const Ct > closed_region_cbegin(const Ct &container, const typename container_traits< Ct >::key_type &lower, const typename container_traits< Ct >::key_type &upper)
closed_region_iterator< const Ct > closed_region_cend(const Ct &container, const typename container_traits< Ct >::key_type &lower, const typename container_traits< Ct >::key_type &upper)
closed_region_iterator< Ct > closed_region_end(Ct &container, const typename container_traits< Ct >::key_type &lower, const typename container_traits< Ct >::key_type &upper)
The main namespace used in the library.
Definition: algorithm.hpp:23
relative_order operator()(dimension_type dim, dimension_type, const Key &key) const
The operator that tells wheather the point is in region or not.
region_iterator_pair< const Ct, Predicate > region_crange(const Ct &container, const Predicate &pred)
This overload works only on constant containers and will return a set of constant iterators...
closed_bounds< typename container_traits< Tp >::key_type, typename container_traits< Tp >::key_compare > make_closed_bounds(const Tp &container, const typename container_traits< Tp >::key_type &lower, const typename container_traits< Tp >::key_type &upper)
A closed_bounds factory that takes in a container, a region defined by lower and upper, and returns a constructed closed_bounds object.
Key _upper
The upper bound for the orthogonal region iterator.
closed_region_iterator_pair< const Ct > closed_region_crange(const Ct &container, const typename container_traits< Ct >::key_type &lower, const typename container_traits< Ct >::key_type &upper)
void check_closed_bounds(const Tp &container, const typename container_traits< Tp >::key_type &lower, const typename container_traits< Tp >::key_type &upper)
Checks if all coordinates of lower are less or equal to these of higher along the same dimensions...
closed_bounds()
The default constructor leaves everything un-initialized.
A model of Region Predicate that checks if a value of type Key is contained within the closed boundar...