v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
ring-buffer.h
Go to the documentation of this file.
1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_RING_BUFFER_H_
6#define V8_BASE_RING_BUFFER_H_
7
8#include <cstdint>
9
10namespace v8::base {
11
12template <typename T, uint8_t _SIZE = 10>
13class RingBuffer final {
14 public:
15 static constexpr uint8_t kSize = _SIZE;
16
17 constexpr RingBuffer() = default;
18
19 RingBuffer(const RingBuffer&) = delete;
20 RingBuffer& operator=(const RingBuffer&) = delete;
21
22 constexpr void Push(const T& value) {
23 elements_[pos_++] = value;
24 if (pos_ == kSize) {
25 pos_ = 0;
26 is_full_ = true;
27 }
28 }
29
30 constexpr uint8_t Size() const { return is_full_ ? kSize : pos_; }
31
32 constexpr bool Empty() const { return Size() == 0; }
33
34 constexpr void Clear() {
35 pos_ = 0;
36 is_full_ = false;
37 }
38
39 template <typename Callback>
40 constexpr T Reduce(Callback callback, const T& initial) const {
41 T result = initial;
42 for (uint8_t i = pos_; i > 0; --i) {
44 }
45 if (!is_full_) {
46 return result;
47 }
48 for (uint8_t i = kSize; i > pos_; --i) {
50 }
51 return result;
52 }
53
54 private:
56 uint8_t pos_ = 0;
57 bool is_full_ = false;
58};
59
60} // namespace v8::base
61
62#endif // V8_BASE_RING_BUFFER_H_
static constexpr uint8_t kSize
Definition ring-buffer.h:15
constexpr void Clear()
Definition ring-buffer.h:34
constexpr bool Empty() const
Definition ring-buffer.h:32
constexpr T Reduce(Callback callback, const T &initial) const
Definition ring-buffer.h:40
RingBuffer(const RingBuffer &)=delete
RingBuffer & operator=(const RingBuffer &)=delete
constexpr uint8_t Size() const
Definition ring-buffer.h:30
constexpr void Push(const T &value)
Definition ring-buffer.h:22
constexpr RingBuffer()=default
TNode< Object > callback
ZoneVector< RpoNumber > & result
std::unique_ptr< ValueMirror > value