Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
function.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 
13 #ifndef SPATIAL_FUNCTION_HPP
14 #define SPATIAL_FUNCTION_HPP
15 
16 #include <iterator> // std::advance
17 #include "spatial.hpp"
18 
19 namespace spatial
20 {
28  template <typename Accessor, typename Tp, typename Unit>
29  struct accessor_minus
30  : private Accessor // empty member optimization
31  {
32  explicit accessor_minus(Accessor accessor_ = Accessor())
33  : Accessor(accessor_)
34  { }
35 
36  template <typename AnyUnit>
38  : Accessor(other.accessor())
39  { }
40 
41  Unit
42  operator() (dimension_type n, const Tp& x, const Tp& y) const
43  { return Accessor::operator()(n, x) - Accessor::operator()(n, y); }
44 
45  Accessor accessor() const { return *static_cast<const Accessor*>(this); }
46  };
47 
55  template <typename Tp, typename Unit>
56  struct bracket_minus
57  {
59 
60  template <typename AnyUnit>
62 
63  Unit
64  operator() (dimension_type n, const Tp& x, const Tp& y) const
65  { return x[n] - y[n]; }
66  };
67 
75  template <typename Tp, typename Unit>
76  struct paren_minus
77  {
79 
80  template <typename AnyUnit>
82 
83  Unit
84  operator() (dimension_type n, const Tp& x, const Tp& y) const
85  { return x(n) - y(n); }
86  };
87 
95  template <typename Tp, typename Unit>
96  struct iterator_minus
97  {
99 
100  template <typename AnyUnit>
102 
103  Unit
104  operator() (dimension_type n, const Tp& x, const Tp& y) const
105  {
106  typename Tp::const_iterator ix = x.begin();
107  typename Tp::const_iterator iy = y.begin();
108  {
109  using namespace std;
110  typedef typename std::iterator_traits<typename Tp::const_iterator>
111  ::difference_type diff_t;
112  advance(ix, static_cast<diff_t>(n));
113  advance(iy, static_cast<diff_t>(n));
114  }
115  return *ix - *iy;
116  }
117  };
118 
132  template<typename Accessor, typename Tp>
133  struct accessor_less
134  : private Accessor // empty member optimization
135  {
136  explicit accessor_less(Accessor access = Accessor())
137  : Accessor(access)
138  { }
139 
140  bool
141  operator() (dimension_type n, const Tp& x, const Tp& y) const
142  {
143  return (Accessor::operator()(n, x) < Accessor::operator()(n, y));
144  }
145 
146  bool operator()
147  (dimension_type a, const Tp& x, dimension_type b, const Tp& y) const
148  {
149  return (Accessor::operator()(a, x) < Accessor::operator()(b, y));
150  }
151 
152  const Accessor& accessor() const
153  { return *static_cast<const Accessor*>(this); }
154  };
155 
162  template <typename Tp>
163  struct bracket_less
164  {
165  bool
166  operator() (dimension_type n, const Tp& x, const Tp& y) const
167  {
168  return (x[n] < y[n]);
169  }
170 
171  bool operator()
172  (dimension_type a, const Tp& x, dimension_type b, const Tp& y) const
173  {
174  return (x[a] < y[b]);
175  }
176  };
177 
184  template <typename Tp>
185  struct paren_less
186  {
187  bool
188  operator() (dimension_type n, const Tp& x, const Tp& y) const
189  {
190  return (x(n) < y(n));
191  }
192 
193  bool operator()
194  (dimension_type a, const Tp& x, dimension_type b, const Tp& y) const
195  {
196  return (x(a) < y(b));
197  }
198  };
199 
206  template <typename Tp>
207  struct iterator_less
208  {
209  bool
210  operator() (dimension_type n, const Tp& x, const Tp& y) const
211  {
212  typename Tp::const_iterator ix = x.begin();
213  typename Tp::const_iterator iy = y.begin();
214  {
215  using namespace std;
216  typedef typename std::iterator_traits<typename Tp::const_iterator>
217  ::difference_type diff_t;
218  advance(ix, static_cast<diff_t>(n));
219  advance(iy, static_cast<diff_t>(n));
220  }
221  return (*ix < *iy);
222  }
223 
224  bool operator()
225  (dimension_type a, const Tp& x, dimension_type b, const Tp& y) const
226  {
227  typename Tp::const_iterator ix = x.begin();
228  typename Tp::const_iterator iy = y.begin();
229  {
230  using namespace std;
231  typedef typename std::iterator_traits<typename Tp::const_iterator>
232  ::difference_type diff_t;
233  advance(ix, static_cast<diff_t>(a));
234  advance(iy, static_cast<diff_t>(b));
235  }
236  return (*ix < *iy);
237  }
238  };
239 
240 } // namespace spatial
241 
242 #endif // SPATIAL_FUNCTION_HPP
iterator_minus(const iterator_minus< Tp, AnyUnit > &)
Definition: function.hpp:101
Unit operator()(dimension_type n, const Tp &x, const Tp &y) const
Definition: function.hpp:84
Unit operator()(dimension_type n, const Tp &x, const Tp &y) const
Definition: function.hpp:64
This functor uses the minus operator to calculate the difference between 2 elements of Tp along the d...
STL namespace.
This functor uses the minus operator to calculate the difference between 2 elements of Tp along the d...
bracket_minus(const bracket_minus< Tp, AnyUnit > &)
Definition: function.hpp:61
This functor uses the minus operator to calculate the difference between 2 elements of Tp along the d...
std::size_t dimension_type
Defines the type for the dimension as being a size.
Definition: spatial.hpp:71
const Accessor & accessor() const
Definition: function.hpp:152
accessor_minus(const accessor_minus< Accessor, Tp, AnyUnit > &other)
Definition: function.hpp:37
Accessor accessor() const
Definition: function.hpp:45
accessor_less(Accessor access=Accessor())
Definition: function.hpp:136
The main namespace used in the library.
Definition: algorithm.hpp:23
This functor uses the minus operator to calculate the difference between 2 elements of Tp along the d...
paren_minus(const paren_minus< Tp, AnyUnit > &)
Definition: function.hpp:81
Unit operator()(dimension_type n, const Tp &x, const Tp &y) const
Definition: function.hpp:42
accessor_minus(Accessor accessor_=Accessor())
Definition: function.hpp:32
Unit operator()(dimension_type n, const Tp &x, const Tp &y) const
Definition: function.hpp:104