Chris@16: // Boost.Geometry (aka GGL, Generic Geometry Library) Chris@16: Chris@16: // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands. Chris@16: Chris@16: // Use, modification and distribution is subject to the Boost Software License, Chris@16: // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_GEOMETRY_POLICIES_COMPARE_HPP Chris@16: #define BOOST_GEOMETRY_POLICIES_COMPARE_HPP Chris@16: Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost { namespace geometry Chris@16: { Chris@16: Chris@16: Chris@16: #ifndef DOXYGEN_NO_DETAIL Chris@16: namespace detail { namespace compare Chris@16: { Chris@16: Chris@16: Chris@16: template Chris@16: < Chris@16: int Direction, Chris@16: typename Point, Chris@16: typename Strategy, Chris@16: std::size_t Dimension, Chris@16: std::size_t DimensionCount Chris@16: > Chris@16: struct compare_loop Chris@16: { Chris@16: typedef typename strategy::compare::detail::select_strategy Chris@16: < Chris@16: Strategy, Direction, Point, Dimension Chris@16: >::type compare_type; Chris@16: Chris@16: typedef typename geometry::coordinate_type::type coordinate_type; Chris@16: Chris@16: static inline bool apply(Point const& left, Point const& right) Chris@16: { Chris@16: coordinate_type const& cleft = geometry::get(left); Chris@16: coordinate_type const& cright = geometry::get(right); Chris@16: Chris@16: if (geometry::math::equals(cleft, cright)) Chris@16: { Chris@16: return compare_loop Chris@16: < Chris@16: Direction, Point, Strategy, Chris@16: Dimension + 1, DimensionCount Chris@16: >::apply(left, right); Chris@16: } Chris@16: else Chris@16: { Chris@16: compare_type compare; Chris@16: return compare(cleft, cright); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: < Chris@16: int Direction, Chris@16: typename Point, Chris@16: typename Strategy, Chris@16: std::size_t DimensionCount Chris@16: > Chris@16: struct compare_loop Chris@16: { Chris@16: static inline bool apply(Point const&, Point const&) Chris@16: { Chris@16: // On coming here, points are equal. Return true if Chris@16: // direction = 0 (equal), false if -1/1 (greater/less) Chris@16: return Direction == 0; Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: template Chris@16: struct compare_in_all_dimensions Chris@16: { Chris@16: inline bool operator()(Point const& left, Point const& right) const Chris@16: { Chris@16: return detail::compare::compare_loop Chris@16: < Chris@16: Direction, Point, Strategy, Chris@16: 0, geometry::dimension::type::value Chris@16: >::apply(left, right); Chris@16: } Chris@16: }; Chris@16: Chris@16: Chris@16: template Chris@16: class compare_in_one_dimension Chris@16: { Chris@16: Strategy compare; Chris@16: Chris@16: public : Chris@16: inline bool operator()(Point const& left, Point const& right) const Chris@16: { Chris@16: typedef typename geometry::coordinate_type::type coordinate_type; Chris@16: Chris@16: coordinate_type const& cleft = get(left); Chris@16: coordinate_type const& cright = get(right); Chris@16: return compare(cleft, cright); Chris@16: } Chris@16: }; Chris@16: Chris@16: }} // namespace detail::compare Chris@16: Chris@16: #endif Chris@16: Chris@16: #ifndef DOXYGEN_NO_DISPATCH Chris@16: namespace dispatch Chris@16: { Chris@16: Chris@16: template Chris@16: < Chris@16: int Direction, Chris@16: typename Point, Chris@16: typename Strategy, Chris@16: int Dimension Chris@16: > Chris@16: struct compare_geometries Chris@16: : detail::compare::compare_in_one_dimension Chris@16: < Chris@16: Point, Chris@16: typename strategy::compare::detail::select_strategy Chris@16: < Chris@16: Strategy, Direction, Point, Dimension Chris@16: >::type, Chris@16: Dimension Chris@16: > Chris@16: {}; Chris@16: Chris@16: Chris@16: // Specialization with -1: compare in all dimensions Chris@16: template Chris@16: struct compare_geometries Chris@16: : detail::compare::compare_in_all_dimensions Chris@16: {}; Chris@16: Chris@16: Chris@16: Chris@16: } // namespace dispatch Chris@16: #endif // DOXYGEN_NO_DISPATCH Chris@16: Chris@16: Chris@16: /*! Chris@16: \brief Less functor, to sort points in ascending order. Chris@16: \ingroup compare Chris@16: \details This functor compares points and orders them on x, Chris@16: then on y, then on z coordinate. Chris@16: \tparam Geometry the geometry Chris@16: \tparam Dimension the dimension to sort on, defaults to -1, Chris@16: indicating ALL dimensions. That's to say, first on x, Chris@16: on equal x-es then on y, etc. Chris@16: If a dimension is specified, only that dimension is considered Chris@16: \tparam Strategy underlying coordinate comparing functor, Chris@16: defaults to the default comparison strategies Chris@16: related to the point coordinate system. If specified, the specified Chris@16: strategy is used. This can e.g. be std::less. Chris@16: */ Chris@16: template Chris@16: < Chris@16: typename Point, Chris@16: int Dimension = -1, Chris@16: typename Strategy = strategy::compare::default_strategy Chris@16: > Chris@16: struct less Chris@16: : dispatch::compare_geometries Chris@16: < Chris@16: 1, // indicates ascending Chris@16: Point, Chris@16: Strategy, Chris@16: Dimension Chris@16: > Chris@16: { Chris@16: typedef Point first_argument_type; Chris@16: typedef Point second_argument_type; Chris@16: typedef bool result_type; Chris@16: }; Chris@16: Chris@16: Chris@16: /*! Chris@16: \brief Greater functor Chris@16: \ingroup compare Chris@16: \details Can be used to sort points in reverse order Chris@16: \see Less functor Chris@16: */ Chris@16: template Chris@16: < Chris@16: typename Point, Chris@16: int Dimension = -1, Chris@16: typename Strategy = strategy::compare::default_strategy Chris@16: > Chris@16: struct greater Chris@16: : dispatch::compare_geometries Chris@16: < Chris@16: -1, // indicates descending Chris@16: Point, Chris@16: Strategy, Chris@16: Dimension Chris@16: > Chris@16: {}; Chris@16: Chris@16: Chris@16: /*! Chris@16: \brief Equal To functor, to compare if points are equal Chris@16: \ingroup compare Chris@16: \tparam Geometry the geometry Chris@16: \tparam Dimension the dimension to compare on, defaults to -1, Chris@16: indicating ALL dimensions. Chris@16: If a dimension is specified, only that dimension is considered Chris@16: \tparam Strategy underlying coordinate comparing functor Chris@16: */ Chris@16: template Chris@16: < Chris@16: typename Point, Chris@16: int Dimension = -1, Chris@16: typename Strategy = strategy::compare::default_strategy Chris@16: > Chris@16: struct equal_to Chris@16: : dispatch::compare_geometries Chris@16: < Chris@16: 0, Chris@16: Point, Chris@16: Strategy, Chris@16: Dimension Chris@16: > Chris@16: {}; Chris@16: Chris@16: Chris@16: }} // namespace boost::geometry Chris@16: Chris@16: Chris@16: #endif // BOOST_GEOMETRY_POLICIES_COMPARE_HPP