Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
spatial_overlap_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_OVERLAP_REGION_HPP
17 #define SPATIAL_OVERLAP_REGION_HPP
18 
19 #include "spatial_region.hpp"
20 
21 namespace spatial
22 {
59  template <typename Key, typename Compare,
60  typename Layout = llhh_layout_tag>
62  : private Compare
63  {
64  public:
68  overlap_bounds() : Compare(), _target() { }
69 
73  overlap_bounds(const Compare& compare, const Key& target)
74  : Compare(compare), _target(target)
75  { }
76 
81  operator()(dimension_type dim, dimension_type rank, const Key& key) const
82  {
83  return overlap_bounds_impl(dim, rank, key, Layout());
84  }
85 
86  private:
90  Key _target;
91 
93  (dimension_type dim, dimension_type rank, const Key& key, llhh_layout_tag)
94  const
95  {
96  return (dim < (rank >> 1))
97  ? (Compare::operator()(dim, key, dim + (rank >> 1), _target)
98  ? matching : above)
99  : (Compare::operator()(dim - (rank >> 1), _target, dim, key)
100  ? matching : below);
101  }
102 
105  const
106  {
107  return ((dim % 2) == 0)
108  ? (Compare::operator()(dim, key, dim + 1, _target) ? matching : above)
109  : (Compare::operator()(dim - 1, _target, dim, key) ? matching : below);
110  }
111 
113  (dimension_type dim, dimension_type rank, const Key& key, hhll_layout_tag)
114  const
115  {
116  return (dim < (rank >> 1))
117  ? (Compare::operator()(dim + (rank >> 1), _target, dim, key)
118  ? matching : below)
119  : (Compare::operator()(dim, key, dim - (rank >> 1), _target)
120  ? matching : above);
121  }
122 
125  const
126  {
127  return ((dim % 2) == 0)
128  ? (Compare::operator()(dim + 1, _target, dim, key) ? matching : below)
129  : (Compare::operator()(dim, key, dim - 1, _target) ? matching : above);
130  }
131  };
132 
140  template <typename Tp, typename Layout>
142  overlap_bounds<typename container_traits<Tp>::key_type,
143  typename container_traits<Tp>::key_compare,
144  Layout>
146  (const Tp& container,
147  const typename container_traits<Tp>::key_type& target,
148  Layout tag)
149  {
150  ::spatial::except::check_box(container, target, tag);
151  return overlap_bounds
153  typename container_traits<Tp>::key_compare, Layout>
154  (container.key_comp(), target);
155  }
156 
157  template <typename Tp>
158  overlap_bounds<typename container_traits<Tp>::key_type,
159  typename container_traits<Tp>::key_compare>
161  (const Tp& container,
162  const typename container_traits<Tp>::key_type& target)
163  { return make_overlap_bounds(container, target, llhh_layout_tag()); }
165 
166  template<typename Ct, typename Layout = llhh_layout_tag>
169  <Ct, overlap_bounds<typename container_traits<Ct>::key_type,
170  typename container_traits<Ct>::key_compare,
171  Layout> >
172  {
175 
180  Layout> >& other)
184  Layout> >(other) { }
185  };
186 
187  template<typename Ct, typename Layout>
188  struct overlap_region_iterator<const Ct, Layout>
190  <const Ct, overlap_bounds<typename container_traits<Ct>::key_type,
191  typename container_traits<Ct>::key_compare,
192  Layout> >
193  {
196 
201  Layout> >& other)
205  Layout> >
206  (other) { }
207 
211  typename container_traits<Ct>::key_compare,
212  Layout> >& other)
214  <const Ct, overlap_bounds<typename container_traits<Ct>::key_type,
215  typename container_traits<Ct>::key_compare,
216  Layout> >
217  (other) { }
218  };
219 
220  template<typename Ct, typename Layout = llhh_layout_tag>
223  <Ct, overlap_bounds<typename container_traits<Ct>::key_type,
224  typename container_traits<Ct>::key_compare,
225  Layout> >
226  {
229 
233  typename container_traits<Ct>::key_compare,
234  Layout> >& a,
235  const region_iterator
237  typename container_traits<Ct>::key_compare,
238  Layout> >& b)
240  <Ct, overlap_bounds<typename container_traits<Ct>::key_type,
241  typename container_traits<Ct>::key_compare,
242  Layout> >
243  (a, b) { }
244  };
245 
246  template<typename Ct, typename Layout>
247  struct overlap_region_iterator_pair<const Ct, Layout>
249  <const Ct, overlap_bounds<typename container_traits<Ct>::key_type,
250  typename container_traits<Ct>::key_compare,
251  Layout> >
252  {
255 
259  typename container_traits<Ct>::key_compare,
260  Layout> >& a,
261  const region_iterator
263  typename container_traits<Ct>::key_compare,
264  Layout> >& b)
266  <const Ct, overlap_bounds<typename container_traits<Ct>::key_type,
267  typename container_traits<Ct>::key_compare,
268  Layout> >
269  (a, b) { }
270 
274  <const Ct, overlap_bounds<typename container_traits<Ct>::key_type,
275  typename container_traits<Ct>::key_compare,
276  Layout> >
277  (other) { }
278  };
279 
280  template <typename Ct>
281  inline overlap_region_iterator<Ct>
282  overlap_region_end(Ct& container,
283  const typename container_traits<Ct>::key_type& target)
284  {
285  return region_end
286  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
287  }
288 
289  template <typename Ct, typename Layout>
290  inline overlap_region_iterator<Ct, Layout>
291  overlap_region_end(Ct& container,
292  const typename container_traits<Ct>::key_type& target,
293  const Layout& layout)
294  {
295  return region_end
296  (container, make_overlap_bounds(container, target, layout));
297  }
298 
299  template <typename Ct>
300  inline overlap_region_iterator<const Ct>
301  overlap_region_end(const Ct& container,
302  const typename container_traits<Ct>::key_type& target)
303  {
304  return _regionend
305  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
306  }
307 
308  template <typename Ct, typename Layout>
309  inline overlap_region_iterator<const Ct, Layout>
310  overlap_region_end(const Ct& container,
311  const typename container_traits<Ct>::key_type& target,
312  const Layout& layout)
313  {
314  return region_end
315  (container, make_overlap_bounds(container, target, layout));
316  }
317 
318  template <typename Ct>
319  inline overlap_region_iterator<const Ct>
320  overlap_region_cend(const Ct& container,
321  const typename container_traits<Ct>::key_type& target)
322  {
323  return region_cend
324  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
325  }
326 
327  template <typename Ct, typename Layout>
328  inline overlap_region_iterator<const Ct, Layout>
329  overlap_region_cend(const Ct& container,
330  const typename container_traits<Ct>::key_type& target,
331  const Layout& layout)
332  {
333  return region_cend
334  (container, make_overlap_bounds(container, target, layout));
335  }
336 
337  template <typename Ct>
338  inline overlap_region_iterator<Ct>
339  overlap_region_begin(Ct& container,
340  const typename container_traits<Ct>::key_type& target)
341  {
342  return region_begin
343  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
344  }
345 
346  template <typename Ct, typename Layout>
347  inline overlap_region_iterator<Ct, Layout>
348  overlap_region_begin(Ct& container,
349  const typename container_traits<Ct>::key_type& target,
350  const Layout& layout)
351  {
352  return region_begin
353  (container, make_overlap_bounds(container, target, layout));
354  }
355 
356  template <typename Ct>
357  inline overlap_region_iterator<const Ct>
358  overlap_region_begin(const Ct& container,
359  const typename container_traits<Ct>::key_type& target)
360  {
361  return _regionbegin
362  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
363  }
364 
365  template <typename Ct, typename Layout>
366  inline overlap_region_iterator<const Ct, Layout>
367  overlap_region_begin(const Ct& container,
368  const typename container_traits<Ct>::key_type& target,
369  const Layout& layout)
370  {
371  return region_begin
372  (container, make_overlap_bounds(container, target, layout));
373  }
374 
375  template <typename Ct>
376  inline overlap_region_iterator<const Ct>
377  overlap_region_cbegin(const Ct& container,
378  const typename container_traits<Ct>::key_type& target)
379  {
380  return region_cbegin
381  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
382  }
383 
384  template <typename Ct, typename Layout>
385  inline overlap_region_iterator<const Ct, Layout>
386  overlap_region_cbegin(const Ct& container,
387  const typename container_traits<Ct>::key_type& target,
388  const Layout& layout)
389  {
390  return region_cbegin
391  (container, make_overlap_bounds(container, target, layout));
392  }
393 
394  template <typename Ct>
395  inline overlap_region_iterator_pair<Ct>
396  overlap_region_range(Ct& container,
397  const typename container_traits<Ct>::key_type& target)
398  {
399  return region_range
400  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
401  }
402 
403  template <typename Ct, typename Layout>
404  inline overlap_region_iterator_pair<Ct, Layout>
405  overlap_region_range(Ct& container,
406  const typename container_traits<Ct>::key_type& target,
407  const Layout& layout)
408  {
409  return region_range
410  (container, make_overlap_bounds(container, target, layout));
411  }
412 
413  template <typename Ct>
414  inline overlap_region_iterator_pair<const Ct>
415  overlap_region_range(const Ct& container,
416  const typename container_traits<Ct>::key_type& target)
417  {
418  return region_range
419  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
420  }
421 
422  template <typename Ct, typename Layout>
423  inline overlap_region_iterator_pair<const Ct, Layout>
424  overlap_region_range(const Ct& container,
425  const typename container_traits<Ct>::key_type& target,
426  const Layout& layout)
427  {
428  return region_range
429  (container, make_overlap_bounds(container, target, layout));
430  }
431 
432  template <typename Ct>
433  inline overlap_region_iterator_pair<const Ct>
434  overlap_region_crange(const Ct& container,
435  const typename container_traits<Ct>::key_type& target)
436  {
437  return region_crange
438  (container, make_overlap_bounds(container, target, llhh_layout_tag()));
439  }
440 
441  template <typename Ct, typename Layout>
442  inline overlap_region_iterator_pair<const Ct, Layout>
443  overlap_region_crange(const Ct& container,
444  const typename container_traits<Ct>::key_type& target,
445  const Layout& layout)
446  {
447  return region_crange
448  (container, make_overlap_bounds(container, target, layout));
449  }
450 
451 } // namespace spatial
452 
453 #endif // SPATIAL_OVERLAP_REGION_HPP
overlap_region_iterator< Ct > overlap_region_begin(Ct &container, const typename container_traits< Ct >::key_type &target)
This type provides both an iterator and a constant iterator to iterate through all elements of a tree...
overlap_region_iterator< Ct > overlap_region_end(Ct &container, const typename container_traits< Ct >::key_type &target)
overlap_region_iterator_pair< const Ct > overlap_region_crange(const Ct &container, const typename container_traits< Ct >::key_type &target)
Represents a coordinate layout for the box.
Definition: spatial.hpp:126
Represents a coordinate layout for the box.
Definition: spatial.hpp:97
region_iterator< const Ct, Predicate > region_cend(const Ct &container, const Predicate &pred)
This structure defines a pair of mutable region iterator.
relative_order overlap_bounds_impl(dimension_type dim, dimension_type rank, const Key &key, llhh_layout_tag) const
Tp::key_type key_type
The type representing the key managed by the container.
Definition: traits.hpp:48
overlap_bounds()
The default constructor leaves everything un-initialized.
This class is a model of Region Predicate that matches any keys that is overlapping with a given test...
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)
overlap_bounds(const Compare &compare, const Key &target)
Set the target box and the comparator to the appropriate value.
relative_order operator()(dimension_type dim, dimension_type rank, const Key &key) const
The operator that tells wheather the point is in region or not.
Represents a coordinate layout for the box.
Definition: spatial.hpp:112
overlap_region_iterator_pair< Ct > overlap_region_range(Ct &container, const typename container_traits< Ct >::key_type &target)
The main namespace used in the library.
Definition: algorithm.hpp:23
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...
Key _target
The box value that will be used for overlaping comparison.
overlap_region_iterator< const Ct > overlap_region_cend(const Ct &container, const typename container_traits< Ct >::key_type &target)
Represents a coordinate layout for the box.
Definition: spatial.hpp:141
overlap_region_iterator< const Ct > overlap_region_cbegin(const Ct &container, const typename container_traits< Ct >::key_type &target)
overlap_bounds< typename container_traits< Tp >::key_type, typename container_traits< Tp >::key_compare, Layout > make_overlap_bounds(const Tp &container, const typename container_traits< Tp >::key_type &target, Layout tag)
Overlap bounds factory that takes in a container, a key and returns an overlap_bounds type...