Welcome to mirror list, hosted at ThFree Co, Russian Federation.

interval_set.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 775112c48a6df18573e1e14216e4ee9223f18edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once

#include "std/algorithm.hpp"
#include "std/set.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"

// todo(@m) Move to search/base?
namespace search
{
// This class represents a set of disjoint intervals in the form
// [begin, end).  Note that neighbour intervals are always coalesced,
// so while [0, 1), [1, 2) and [2, 3) are disjoint, after addition to
// the set they will be stored as a single [0, 3).
template <typename Elem>
class IntervalSet
{
public:
  using Interval = pair<Elem, Elem>;

  // Adds an |interval| to the set, coalescing adjacent intervals if needed.
  //
  // Complexity: O(num of intervals intersecting with |interval| +
  // log(total number of intervals)).
  void Add(Interval const & interval);

  // Subtracts set from an |interval| and appends result to
  // |difference|.
  //
  // Complexity: O(num of intervals intersecting with |interval| +
  // log(total number of intervals)).
  void SubtractFrom(Interval const & interval, vector<Interval> & difference) const;

  // Returns all elements of the set as a set of intervals.
  //
  // Complexity: O(1).
  inline set<Interval> const & Elems() const { return m_intervals; }

private:
  using Iterator = typename set<Interval>::iterator;

  // Calculates range of intervals that have non-empty intersection with a given |interval|.
  void Cover(Interval const & interval, Iterator & begin, Iterator & end) const;

  // This is a set of disjoint intervals.
  set<Interval> m_intervals;
};

template <typename Elem>
void IntervalSet<Elem>::Add(Interval const & interval)
{
  // Skips empty intervals.
  if (interval.first == interval.second)
    return;

  Iterator begin;
  Iterator end;
  Cover(interval, begin, end);

  Elem from = interval.first;
  Elem to = interval.second;

  // Updates |from| and |to| in accordance with corner intervals (if any).
  if (begin != end)
  {
    if (begin->first < from)
      from = begin->first;

    auto last = end;
    --last;
    if (last->second > to)
      to = last->second;
  }

  // Now all elements [from, to) can be added to the set as a single
  // interval which will replace all intervals in [begin, end). But
  // note that it can be possible to merge new interval with its
  // neighbors, so following code checks it.
  if (begin != m_intervals.begin())
  {
    auto prevBegin = begin;
    --prevBegin;
    if (prevBegin->second == from)
    {
      begin = prevBegin;
      from = prevBegin->first;
    }
  }
  if (end != m_intervals.end() && end->first == to)
  {
    to = end->second;
    ++end;
  }

  m_intervals.erase(begin, end);
  m_intervals.emplace(from, to);
}

template <typename Elem>
void IntervalSet<Elem>::SubtractFrom(Interval const & interval,
                                      vector<Interval> & difference) const
{
  Iterator begin;
  Iterator end;

  Cover(interval, begin, end);

  Elem from = interval.first;
  Elem const to = interval.second;

  for (auto it = begin; it != end && from < to; ++it)
  {
    if (it->first > from)
    {
      difference.emplace_back(from, it->first);
      from = it->second;
    }
    else
    {
      from = max(from, it->second);
    }
  }

  if (from < to)
    difference.emplace_back(from, to);
}

template <typename Elem>
void IntervalSet<Elem>::Cover(Interval const & interval, Iterator & begin, Iterator & end) const
{
  Elem const & from = interval.first;
  Elem const & to = interval.second;

  begin = m_intervals.lower_bound(make_pair(from, from));
  if (begin != m_intervals.begin())
  {
    auto prev = begin;
    --prev;
    if (prev->second > from)
      begin = prev;
  }

  end = m_intervals.lower_bound(make_pair(to, to));
}
}  // namespace search