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: a6db719bbb6b9f6fcda43735c73c61d1e8df2682 (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
#pragma once

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

namespace my
{
// 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 TElem>
class IntervalSet
{
public:
  using TInterval = pair<TElem, TElem>;

  // 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(TInterval 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(TInterval const & interval, vector<TInterval> & difference) const;

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

private:
  using TIterator = typename set<TInterval>::iterator;

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

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

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

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

  TElem from = interval.first;
  TElem 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 TElem>
void IntervalSet<TElem>::SubtractFrom(TInterval const & interval,
                                      vector<TInterval> & difference) const
{
  TIterator begin;
  TIterator end;

  Cover(interval, begin, end);

  TElem from = interval.first;
  TElem 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 TElem>
void IntervalSet<TElem>::Cover(TInterval const & interval, TIterator & begin, TIterator & end) const
{
  TElem const & from = interval.first;
  TElem 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 my