123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAX_INTERVAL_GAP_HPP
- #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAX_INTERVAL_GAP_HPP
- #include <cstddef>
- #include <functional>
- #include <queue>
- #include <utility>
- #include <vector>
- #include <boost/range/begin.hpp>
- #include <boost/range/end.hpp>
- #include <boost/range/value_type.hpp>
- #include <boost/geometry/core/assert.hpp>
- #include <boost/geometry/util/math.hpp>
- #include <boost/geometry/algorithms/detail/sweep.hpp>
- namespace boost { namespace geometry
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace max_interval_gap
- {
- template <typename Interval>
- class sweep_event
- {
- public:
- typedef Interval interval_type;
- typedef typename Interval::value_type time_type;
- sweep_event(Interval const& interval, bool start_event = true)
- : m_interval(std::cref(interval))
- , m_start_event(start_event)
- {}
- inline bool is_start_event() const
- {
- return m_start_event;
- }
- inline interval_type const& interval() const
- {
- return m_interval;
- }
- inline time_type time() const
- {
- return (m_start_event)
- ? interval().template get<0>()
- : interval().template get<1>();
- }
- inline bool operator<(sweep_event const& other) const
- {
- if (! math::equals(time(), other.time()))
- {
- return time() < other.time();
- }
-
- return is_start_event() && ! other.is_start_event();
- }
- private:
- std::reference_wrapper<Interval const> m_interval;
- bool m_start_event;
- };
- template <typename Event>
- struct event_greater
- {
- inline bool operator()(Event const& event1, Event const& event2) const
- {
- return event2 < event1;
- }
- };
- struct initialization_visitor
- {
- template <typename Range, typename PriorityQueue, typename EventVisitor>
- static inline void apply(Range const& range,
- PriorityQueue& queue,
- EventVisitor&)
- {
- BOOST_GEOMETRY_ASSERT(queue.empty());
-
-
- PriorityQueue pq(boost::begin(range), boost::end(range));
- std::swap(pq, queue);
- }
- };
- template <typename Event>
- class event_visitor
- {
- typedef typename Event::time_type event_time_type;
- typedef typename Event::interval_type::difference_type difference_type;
- public:
- event_visitor()
- : m_overlap_count(0)
- , m_max_gap_left(0)
- , m_max_gap_right(0)
- {}
- template <typename PriorityQueue>
- inline void apply(Event const& event, PriorityQueue& queue)
- {
- if (event.is_start_event())
- {
- ++m_overlap_count;
- queue.push(Event(event.interval(), false));
- }
- else
- {
- --m_overlap_count;
- if (m_overlap_count == 0 && ! queue.empty())
- {
-
- BOOST_GEOMETRY_ASSERT(queue.top().is_start_event());
- event_time_type next_event_time
- = queue.top().interval().template get<0>();
- difference_type gap = next_event_time - event.time();
- if (gap > max_gap())
- {
- m_max_gap_left = event.time();
- m_max_gap_right = next_event_time;
- }
- }
- }
- }
- event_time_type const& max_gap_left() const
- {
- return m_max_gap_left;
- }
- event_time_type const& max_gap_right() const
- {
- return m_max_gap_right;
- }
- difference_type max_gap() const
- {
- return m_max_gap_right - m_max_gap_left;
- }
- private:
- std::size_t m_overlap_count;
- event_time_type m_max_gap_left, m_max_gap_right;
- };
- }}
- #endif
- template <typename RangeOfIntervals, typename T>
- inline typename boost::range_value<RangeOfIntervals>::type::difference_type
- maximum_gap(RangeOfIntervals const& range_of_intervals,
- T& max_gap_left, T& max_gap_right)
- {
- typedef typename boost::range_value<RangeOfIntervals>::type interval_type;
- typedef detail::max_interval_gap::sweep_event<interval_type> event_type;
-
- std::priority_queue
- <
- event_type,
- std::vector<event_type>,
- detail::max_interval_gap::event_greater<event_type>
- > queue;
-
- detail::max_interval_gap::initialization_visitor init_visitor;
- detail::max_interval_gap::event_visitor<event_type> sweep_visitor;
-
- geometry::sweep(range_of_intervals,
- queue,
- init_visitor,
- sweep_visitor);
- max_gap_left = sweep_visitor.max_gap_left();
- max_gap_right = sweep_visitor.max_gap_right();
- return sweep_visitor.max_gap();
- }
- template <typename RangeOfIntervals>
- inline typename boost::range_value<RangeOfIntervals>::type::difference_type
- maximum_gap(RangeOfIntervals const& range_of_intervals)
- {
- typedef typename boost::range_value<RangeOfIntervals>::type interval_type;
- typename interval_type::value_type left, right;
- return maximum_gap(range_of_intervals, left, right);
- }
- }}
- #endif
|