1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2015-2020, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAX_INTERVAL_GAP_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAX_INTERVAL_GAP_HPP
20 #include <boost/range/begin.hpp>
21 #include <boost/range/end.hpp>
22 #include <boost/range/value_type.hpp>
24 #include <boost/geometry/core/assert.hpp>
25 #include <boost/geometry/util/math.hpp>
26 #include <boost/geometry/algorithms/detail/sweep.hpp>
29 namespace boost { namespace geometry
32 #ifndef DOXYGEN_NO_DETAIL
33 namespace detail { namespace max_interval_gap
36 // the class Interval must provide the following:
37 // * must define the type value_type
38 // * must define the type difference_type
39 // * must have the methods:
40 // value_type get<Index>() const
41 // difference_type length() const
42 // where an Index value of 0 (resp., 1) refers to the left (resp.,
43 // right) endpoint of the interval
45 template <typename Interval>
49 typedef Interval interval_type;
50 typedef typename Interval::value_type time_type;
52 sweep_event(Interval const& interval, bool start_event = true)
53 : m_interval(std::cref(interval))
54 , m_start_event(start_event)
57 inline bool is_start_event() const
62 inline interval_type const& interval() const
67 inline time_type time() const
69 return (m_start_event)
70 ? interval().template get<0>()
71 : interval().template get<1>();
74 inline bool operator<(sweep_event const& other) const
76 if (! math::equals(time(), other.time()))
78 return time() < other.time();
80 // a start-event is before an end-event with the same event time
81 return is_start_event() && ! other.is_start_event();
85 std::reference_wrapper<Interval const> m_interval;
89 template <typename Event>
92 inline bool operator()(Event const& event1, Event const& event2) const
94 return event2 < event1;
99 struct initialization_visitor
101 template <typename Range, typename PriorityQueue, typename EventVisitor>
102 static inline void apply(Range const& range,
103 PriorityQueue& queue,
106 BOOST_GEOMETRY_ASSERT(queue.empty());
108 // it is faster to build the queue directly from the entire
109 // range, rather than insert elements one after the other
110 PriorityQueue pq(boost::begin(range), boost::end(range));
111 std::swap(pq, queue);
116 template <typename Event>
119 typedef typename Event::time_type event_time_type;
120 typedef typename Event::interval_type::difference_type difference_type;
129 template <typename PriorityQueue>
130 inline void apply(Event const& event, PriorityQueue& queue)
132 if (event.is_start_event())
135 queue.push(Event(event.interval(), false));
140 if (m_overlap_count == 0 && ! queue.empty())
143 BOOST_GEOMETRY_ASSERT(queue.top().is_start_event());
145 event_time_type next_event_time
146 = queue.top().interval().template get<0>();
147 difference_type gap = next_event_time - event.time();
150 m_max_gap_left = event.time();
151 m_max_gap_right = next_event_time;
157 event_time_type const& max_gap_left() const
159 return m_max_gap_left;
162 event_time_type const& max_gap_right() const
164 return m_max_gap_right;
167 difference_type max_gap() const
169 return m_max_gap_right - m_max_gap_left;
173 std::size_t m_overlap_count;
174 event_time_type m_max_gap_left, m_max_gap_right;
177 }} // namespace detail::max_interval_gap
178 #endif // DOXYGEN_NO_DETAIL
181 // Given a range of intervals I1, I2, ..., In, maximum_gap() returns
182 // the maximum length of an interval M that satisfies the following
185 // 1. M.left >= min(I1, I2, ..., In)
186 // 2. M.right <= max(I1, I2, ..., In)
187 // 3. intersection(interior(M), Ik) is the empty set for all k=1, ..., n
188 // 4. length(M) is maximal
190 // where M.left and M.right denote the left and right extreme values
191 // for the interval M, and length(M) is equal to M.right - M.left.
193 // If M does not exist (or, alternatively, M is identified as the
194 // empty set), 0 is returned.
196 // The algorithm proceeds for performing a sweep: the left endpoints
197 // are inserted into a min-priority queue with the priority being the
198 // value of the endpoint. The sweep algorithm maintains an "overlap
199 // counter" that counts the number of overlaping intervals at any
200 // specific sweep-time value.
201 // There are two types of events encountered during the sweep:
202 // (a) a start event: the left endpoint of an interval is found.
203 // In this case the overlap count is increased by one and the
204 // right endpoint of the interval in inserted into the event queue
205 // (b) an end event: the right endpoint of an interval is found.
206 // In this case the overlap count is decreased by one. If the
207 // updated overlap count is 0, then we could expect to have a gap
208 // in-between intervals. This gap is measured as the (absolute)
209 // distance of the current interval right endpoint (being
210 // processed) to the upcoming left endpoint of the next interval
211 // to be processed (if such an interval exists). If the measured
212 // gap is greater than the current maximum gap, it is recorded.
213 // The initial maximum gap is initialized to 0. This value is returned
214 // if no gap is found during the sweeping procedure.
216 template <typename RangeOfIntervals, typename T>
217 inline typename boost::range_value<RangeOfIntervals>::type::difference_type
218 maximum_gap(RangeOfIntervals const& range_of_intervals,
219 T& max_gap_left, T& max_gap_right)
221 typedef typename boost::range_value<RangeOfIntervals>::type interval_type;
222 typedef detail::max_interval_gap::sweep_event<interval_type> event_type;
224 // create a min-priority queue for the events
228 std::vector<event_type>,
229 detail::max_interval_gap::event_greater<event_type>
232 // define initialization and event-process visitors
233 detail::max_interval_gap::initialization_visitor init_visitor;
234 detail::max_interval_gap::event_visitor<event_type> sweep_visitor;
237 geometry::sweep(range_of_intervals,
242 max_gap_left = sweep_visitor.max_gap_left();
243 max_gap_right = sweep_visitor.max_gap_right();
244 return sweep_visitor.max_gap();
247 template <typename RangeOfIntervals>
248 inline typename boost::range_value<RangeOfIntervals>::type::difference_type
249 maximum_gap(RangeOfIntervals const& range_of_intervals)
251 typedef typename boost::range_value<RangeOfIntervals>::type interval_type;
253 typename interval_type::value_type left, right;
255 return maximum_gap(range_of_intervals, left, right);
259 }} // namespace boost::geometry
261 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_MAX_INTERVAL_GAP_HPP