1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_POLICIES_COMPARE_HPP
10 #define BOOST_GEOMETRY_POLICIES_COMPARE_HPP
15 #include <boost/geometry/strategies/compare.hpp>
16 #include <boost/geometry/util/math.hpp>
19 namespace boost { namespace geometry
23 #ifndef DOXYGEN_NO_DETAIL
24 namespace detail { namespace compare
33 std::size_t Dimension,
34 std::size_t DimensionCount
38 typedef typename strategy::compare::detail::select_strategy
40 Strategy, Direction, Point, Dimension
43 typedef typename geometry::coordinate_type<Point>::type coordinate_type;
45 static inline bool apply(Point const& left, Point const& right)
47 coordinate_type const& cleft = geometry::get<Dimension>(left);
48 coordinate_type const& cright = geometry::get<Dimension>(right);
50 if (geometry::math::equals(cleft, cright))
54 Direction, Point, Strategy,
55 Dimension + 1, DimensionCount
56 >::apply(left, right);
61 return compare(cleft, cright);
71 std::size_t DimensionCount
73 struct compare_loop<Direction, Point, Strategy, DimensionCount, DimensionCount>
75 static inline bool apply(Point const&, Point const&)
77 // On coming here, points are equal. Return true if
78 // direction = 0 (equal), false if -1/1 (greater/less)
79 return Direction == 0;
84 template <int Direction, typename Point, typename Strategy>
85 struct compare_in_all_dimensions
87 inline bool operator()(Point const& left, Point const& right) const
89 return detail::compare::compare_loop
91 Direction, Point, Strategy,
92 0, geometry::dimension<Point>::type::value
93 >::apply(left, right);
98 template <typename Point, typename Strategy, std::size_t Dimension>
99 class compare_in_one_dimension
104 inline bool operator()(Point const& left, Point const& right) const
106 typedef typename geometry::coordinate_type<Point>::type coordinate_type;
108 coordinate_type const& cleft = get<Dimension>(left);
109 coordinate_type const& cright = get<Dimension>(right);
110 return compare(cleft, cright);
114 }} // namespace detail::compare
118 #ifndef DOXYGEN_NO_DISPATCH
129 struct compare_geometries
130 : detail::compare::compare_in_one_dimension
133 typename strategy::compare::detail::select_strategy
135 Strategy, Direction, Point, Dimension
142 // Specialization with -1: compare in all dimensions
143 template <int Direction, typename Point, typename Strategy>
144 struct compare_geometries<Direction, Point, Strategy, -1>
145 : detail::compare::compare_in_all_dimensions<Direction, Point, Strategy>
150 } // namespace dispatch
151 #endif // DOXYGEN_NO_DISPATCH
155 \brief Less functor, to sort points in ascending order.
157 \details This functor compares points and orders them on x,
158 then on y, then on z coordinate.
159 \tparam Geometry the geometry
160 \tparam Dimension the dimension to sort on, defaults to -1,
161 indicating ALL dimensions. That's to say, first on x,
162 on equal x-es then on y, etc.
163 If a dimension is specified, only that dimension is considered
164 \tparam Strategy underlying coordinate comparing functor,
165 defaults to the default comparison strategies
166 related to the point coordinate system. If specified, the specified
167 strategy is used. This can e.g. be std::less<double>.
173 typename Strategy = strategy::compare::default_strategy
176 : dispatch::compare_geometries
178 1, // indicates ascending
184 typedef Point first_argument_type;
185 typedef Point second_argument_type;
186 typedef bool result_type;
191 \brief Greater functor
193 \details Can be used to sort points in reverse order
200 typename Strategy = strategy::compare::default_strategy
203 : dispatch::compare_geometries
205 -1, // indicates descending
214 \brief Equal To functor, to compare if points are equal
216 \tparam Geometry the geometry
217 \tparam Dimension the dimension to compare on, defaults to -1,
218 indicating ALL dimensions.
219 If a dimension is specified, only that dimension is considered
220 \tparam Strategy underlying coordinate comparing functor
226 typename Strategy = strategy::compare::default_strategy
229 : dispatch::compare_geometries
239 }} // namespace boost::geometry
242 #endif // BOOST_GEOMETRY_POLICIES_COMPARE_HPP