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

gps_track_collection.hpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 045787a19f42fc36604d7a81cda21600d84931da (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
#pragma once

#include "platform/location.hpp"

#include "std/chrono.hpp"
#include "std/deque.hpp"
#include "std/limits.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"

class GpsTrackCollection final
{
public:
  static size_t const kInvalidId; // = numeric_limits<size_t>::max();

  using TItem = location::GpsTrackInfo;

  /// Constructor
  /// @param maxSize - max number of items in collection
  /// @param duration - duration in hours
  GpsTrackCollection(size_t maxSize, hours duration);

  /// Adds new point in the collection.
  /// @param item - item to be added.
  /// @param evictedIds - output, which contains range of identifiers evicted items or
  /// pair(kInvalidId,kInvalidId) if nothing was removed
  /// @returns the item unique identifier or kInvalidId if point has incorrect time.
  size_t Add(TItem const & item, pair<size_t, size_t> & evictedIds);

  /// Adds set of new points in the collection.
  /// @param items - set of items to be added.
  /// @param evictedIds - output, which contains range of identifiers evicted items or
  /// pair(kInvalidId,kInvalidId) if nothing was removed
  /// @returns range of identifiers of added items or pair(kInvalidId,kInvalidId) if nothing was added
  /// @note items which does not conform to timestamp sequence, is not added.
  pair<size_t, size_t> Add(vector<TItem> const & items, pair<size_t, size_t> & evictedIds);

  /// Get current duration in hours
  /// @returns current duration in hours
  hours GetDuration() const;

  /// Sets duration in hours.
  /// @param duration - new duration value
  /// @return range of item identifiers, which were removed or
  /// pair(kInvalidId,kInvalidId) if nothing was removed
  pair<size_t, size_t> SetDuration(hours duration);

  /// Removes all points from the collection.
  /// @param resetIds - if it is set to true, then new identifiers will start from 0,
  /// otherwise new identifiers will continue from returned value res.second + 1
  /// @return range of item identifiers, which were removed or
  /// pair(kInvalidId,kInvalidId) if nothing was removed
  pair<size_t, size_t> Clear(bool resetIds = true);

  /// Returns true if collection is empty, otherwise returns false.
  bool IsEmpty() const;

  /// Returns number of items in the collection
  size_t GetSize() const;

  /// Returns range of timestamps of collection, where res.first is lower bound and
  /// res.second is upper bound. If collection is empty, then returns pair(0, 0).
  pair<double, double> GetTimestampRange() const;

  /// Returns max size of collection
  size_t GetMaxSize() const;

  /// Enumerates items in the collection.
  /// @param f - callable object, which is called with params - item and item id,
  /// if f returns false, then enumeration is stopped.
  /// @param pos - position index to start enumeration
  template <typename F>
  void ForEach(F && f, size_t pos = 0) const
  {
    if (pos >= m_items.size())
      return;
    auto i = m_items.cbegin() + pos, iend = m_items.cend();
    size_t id = m_lastId - m_items.size() + pos;
    for (; i != iend; ++i, ++id)
    {
      TItem const & item = *i;
      size_t const itemId = id;
      if (!f(item, itemId))
        break;
    }
  }

private:
  // Removes items in range [m_items.begin(), i) and returnd
  // range of identifiers of removed items
  pair<size_t, size_t> RemoveUntil(deque<TItem>::iterator i);

  // Removes items extra by timestamp and max size
  pair<size_t, size_t> RemoveExtraItems();

  size_t const m_maxSize;

  hours m_duration;

  deque<TItem> m_items; // asc. sorted by timestamp

  size_t m_lastId;
};