circular-buffer.h
1.6 KB
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
// sherpa-onnx/csrc/circular-buffer.h
//
// Copyright (c) 2023 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_CIRCULAR_BUFFER_H_
#define SHERPA_ONNX_CSRC_CIRCULAR_BUFFER_H_
#include <cstdint>
#include <vector>
namespace sherpa_onnx {
class CircularBuffer {
public:
// Capacity of this buffer. Should be large enough.
// If it is full, we just print a message and exit the program.
explicit CircularBuffer(int32_t capacity);
// Push an array
//
// @param p Pointer to the start address of the array
// @param n Number of elements in the array
//
// Note: If n + Size() > capacity, we print an error message and exit.
void Push(const float *p, int32_t n);
// @param start_index Should in the range [head_, tail_)
// @param n Number of elements to get
// @return Return a vector of size n containing the requested elements
std::vector<float> Get(int32_t start_index, int32_t n) const;
// Remove n elements from the buffer
//
// @param n Should be in the range [0, size_]
void Pop(int32_t n);
// Number of elements in the buffer.
int32_t Size() const { return tail_ - head_; }
// Current position of the head
int32_t Head() const { return head_; }
// Current position of the tail
int32_t Tail() const { return tail_; }
void Reset() {
head_ = 0;
tail_ = 0;
}
void Resize(int32_t new_capacity);
private:
std::vector<float> buffer_;
int32_t head_ = 0; // linear index; always increasing; never wraps around
int32_t tail_ = 0; // linear index, always increasing; never wraps around.
};
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_CIRCULAR_BUFFER_H_