Euphoria
last_n.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 #include "assert/assert.h"
6 #include "base/cint.h"
7 
8 namespace eu::core
9 {
10  // stores the "last N" values of some type, where N is configurable
11  template <typename T>
12  struct LastN
13  {
14  std::vector<T> d;
15  std::size_t max;
16 
17  explicit LastN(std::size_t s) : max(s) {}
18 
19  void
20  push(const T& t)
21  {
22  if(d.size() == max)
23  {
24  d.erase(d.begin());
25  }
26  d.push_back(t);
27  }
28 
29  [[nodiscard]] const T*
30  data() const
31  {
32  ASSERT(!d.empty());
33  return d.data();
34  }
35 
36  [[nodiscard]] int
37  size() const
38  {
39  return c_sizet_to_int(d.size());
40  }
41  };
42 }
43 
#define ASSERT(x)
Definition: assert.h:29
int c_sizet_to_int(size_t t)
Definition: cint.cc:11
std::size_t max
Definition: last_n.h:15
const T * data() const
Definition: last_n.h:30
int size() const
Definition: last_n.h:37
std::vector< T > d
Definition: last_n.h:14
void push(const T &t)
Definition: last_n.h:20
LastN(std::size_t s)
Definition: last_n.h:17