summaryrefslogtreecommitdiff
path: root/JttyReceiveHistory.hpp
blob: ebb11f76c3b6d477178d3fe7ceb42e747eaf8b68 (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
#ifndef JTTY_RECEIVE_HISTORY_HPP
#define JTTY_RECEIVE_HISTORY_HPP

#include <QtGlobal>
#include <algorithm>
#include <deque>
#include <vector>

namespace Jtty {
class ReceiveHistory
{
public:
  static constexpr qint64 capacity = 180 * 12000;
  struct Block {
    quint64 reception;
    qint64 first;
    std::vector<short> samples;
    qint64 end() const { return first + qint64(samples.size()); }
  };

  explicit ReceiveHistory(qint64 limit = capacity) : limit_(std::max<qint64>(1, limit)) {}

  bool append(quint64 reception, qint64 first, short const* samples, int count)
  {
    if (count <= 0 || first < 0 || count > limit_) return false;
    if (!blocks_.empty() && blocks_.back().reception == reception
        && blocks_.back().end() != first) return false;
    blocks_.push_back({reception, first, {samples, samples + count}});
    size_ += count;
    while (size_ > limit_) {
      auto& front = blocks_.front();
      auto const remove = std::min(size_ - limit_, qint64(front.samples.size()));
      if (remove == qint64(front.samples.size())) blocks_.pop_front();
      else {
        front.samples.erase(front.samples.begin(), front.samples.begin() + remove);
        front.first += remove;
      }
      size_ -= remove;
    }
    return true;
  }

  std::vector<short> snapshot(quint64 reception, qint64 first, qint64 end) const
  {
    std::vector<short> result;
    if (first < 0 || end <= first || end - first > limit_) return result;
    result.reserve(end - first);
    qint64 next = first;
    for (auto const& block : blocks_) {
      if (block.reception != reception || block.end() <= next) continue;
      if (block.first > next) return {};
      auto const stop = std::min(end, block.end());
      result.insert(result.end(), block.samples.begin() + (next - block.first),
                    block.samples.begin() + (stop - block.first));
      next = stop;
      if (next == end) return result;
    }
    return {};
  }

  qint64 first(quint64 reception) const
  {
    for (auto const& block : blocks_) if (block.reception == reception) return block.first;
    return -1;
  }
  qint64 end(quint64 reception) const
  {
    for (auto it = blocks_.rbegin(); it != blocks_.rend(); ++it)
      if (it->reception == reception) return it->end();
    return -1;
  }
  qint64 size() const { return size_; }
  void clear() { blocks_.clear(); size_ = 0; }

private:
  qint64 limit_;
  qint64 size_ = 0;
  std::deque<Block> blocks_;
};
}
#endif