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

gps_track_collection.cpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c6b51901e256bed7a0bb3ed74d551432b3852eb (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "map/gps_track_collection.hpp"

#include "base/assert.hpp"

#include "std/algorithm.hpp"

namespace
{

size_t const kSecondsPerHour = 60 * 60;
size_t const kLinearSearchCount = 10;

// Simple rollbacker which restores deque state
template <typename T>
class Rollbacker
{
public:
  Rollbacker(deque<T> & cont)
    : m_cont(&cont)
    , m_size(cont.size())
  {}
  ~Rollbacker()
  {
    if (m_cont && m_cont->size() > m_size)
      m_cont->erase(m_cont->begin() + m_size, m_cont->end());
  }
  void Reset() { m_cont = nullptr; }
private:
  deque<T> * m_cont;
  size_t const m_size;
};

} // namespace

size_t const GpsTrackCollection::kInvalidId = numeric_limits<size_t>::max();

GpsTrackCollection::GpsTrackCollection(size_t maxSize, hours duration)
  : m_maxSize(maxSize)
  , m_duration(duration)
  , m_lastId(0)
{
}

size_t GpsTrackCollection::Add(TItem const & item, pair<size_t, size_t> & evictedIds)
{
  if (!m_items.empty() && m_items.back().m_timestamp > item.m_timestamp)
  {
    // Invalid timestamp order
    evictedIds = make_pair(kInvalidId, kInvalidId); // Nothing was evicted
    return kInvalidId; // Nothing was added
  }

  m_items.emplace_back(item);
  ++m_lastId;

  evictedIds = RemoveExtraItems();

  return m_lastId - 1;
}

pair<size_t, size_t> GpsTrackCollection::Add(vector<TItem> const & items, pair<size_t, size_t> & evictedIds)
{
  size_t startId = m_lastId;
  size_t added = 0;

  // Rollbacker ensure strong guarantee if exception happens while adding items
  Rollbacker<TItem> rollbacker(m_items);

  for (auto const & item : items)
  {
    if (!m_items.empty() && m_items.back().m_timestamp > item.m_timestamp)
      continue;

    m_items.emplace_back(item);
    ++added;
  }

  rollbacker.Reset();

  if (0 == added)
  {
    // Invalid timestamp order
    evictedIds = make_pair(kInvalidId, kInvalidId); // Nothing was evicted
    return make_pair(kInvalidId, kInvalidId); // Nothing was added
  }

  m_lastId += added;

  evictedIds = RemoveExtraItems();

  return make_pair(startId, startId + added - 1);
}

hours GpsTrackCollection::GetDuration() const
{
  return m_duration;
}

pair<size_t, size_t> GpsTrackCollection::SetDuration(hours duration)
{
  m_duration = duration;

  if (m_items.empty())
    return make_pair(kInvalidId, kInvalidId);

  return RemoveExtraItems();
}

pair<size_t, size_t> GpsTrackCollection::Clear(bool resetIds)
{
  if (m_items.empty())
  {
    if (resetIds)
      m_lastId = 0;

    return make_pair(kInvalidId, kInvalidId);
  }

  ASSERT_GREATER_OR_EQUAL(m_lastId, m_items.size(), ());

  // Range of evicted items
  auto const res = make_pair(m_lastId - m_items.size(), m_lastId - 1);

  m_items.clear();
  m_items.shrink_to_fit();

  if (resetIds)
    m_lastId = 0;

  return res;
}

size_t GpsTrackCollection::GetSize() const
{
  return m_items.size();
}

size_t GpsTrackCollection::GetMaxSize() const
{
  return m_maxSize;
}

bool GpsTrackCollection::IsEmpty() const
{
  return m_items.empty();
}

pair<double, double> GpsTrackCollection::GetTimestampRange() const
{
  if (m_items.empty())
    return make_pair(0, 0);

  ASSERT_LESS_OR_EQUAL(m_items.front().m_timestamp, m_items.back().m_timestamp, ());

  return make_pair(m_items.front().m_timestamp, m_items.back().m_timestamp);
}

pair<size_t, size_t> GpsTrackCollection::RemoveUntil(deque<TItem>::iterator i)
{
  auto const res = make_pair(m_lastId - m_items.size(),
                             m_lastId - m_items.size() + distance(m_items.begin(), i) - 1);
  m_items.erase(m_items.begin(), i);
  return res;
}

pair<size_t, size_t> GpsTrackCollection::RemoveExtraItems()
{
  if (m_items.empty())
    return make_pair(kInvalidId, kInvalidId); // Nothing to remove

  double const lowerBound = m_items.back().m_timestamp - m_duration.count() * kSecondsPerHour;

  ASSERT_GREATER_OR_EQUAL(m_lastId, m_items.size(), ());

  if (m_items.front().m_timestamp >= lowerBound)
  {
    // All items lie on right side of lower bound,
    // but need to remove items by size.
    if (m_items.size() <= m_maxSize)
      return make_pair(kInvalidId, kInvalidId); // Nothing to remove, all points survived.
    return RemoveUntil(m_items.begin() + m_items.size() - m_maxSize);
  }

  if (m_items.back().m_timestamp <= lowerBound)
  {
    // All items lie on left side of lower bound. Remove all items.
    return RemoveUntil(m_items.end());
  }

  bool found = false;
  auto i = m_items.begin();

  // First, try linear search for short distance. It is common case for sliding window
  // when new items will evict old items.
  for (size_t j = 0; j < kLinearSearchCount; ++i, ++j)
  {
    ASSERT(i != m_items.end(), ());

    if (i->m_timestamp > lowerBound)
    {
      found = true;
      break;
    }
  }

  // If item wasn't found by linear search, since m_items are sorted by timestamp, use lower_bound to find bound
  if (!found)
  {
    TItem t;
    t.m_timestamp = lowerBound;
    i = lower_bound(i, m_items.end(), t, [](TItem const & a, TItem const & b)->bool{ return a.m_timestamp < b.m_timestamp; });

    ASSERT(i != m_items.end(), ());
  }

  // If remaining part has size more than max size then cut off to leave max size
  size_t const remains = distance(i, m_items.end());
  if (remains > m_maxSize)
    i += remains - m_maxSize;

  return RemoveUntil(i);
}