DPC++ Runtime
Runtime libraries for oneAPI DPC++
circular_buffer.hpp
Go to the documentation of this file.
1 //==---------------- circular_buffer.hpp - Circular buffer -----------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/detail/defines.hpp>
12 
13 #include <cstddef>
14 #include <deque>
15 #include <utility>
16 
17 namespace sycl {
18 inline namespace _V1 {
19 namespace detail {
20 
21 // A partial implementation of a circular buffer: once its capacity is full,
22 // new data overwrites the old.
23 template <typename T> class CircularBuffer {
24 public:
25  explicit CircularBuffer(std::size_t Capacity) : MCapacity{Capacity} {};
26 
27  using value_type = T;
28  using pointer = T *;
29  using const_pointer = const T *;
30  using reference = T &;
31  using const_reference = const T &;
32 
33  using iterator = typename std::deque<T>::iterator;
34  using const_iterator = typename std::deque<T>::const_iterator;
35 
36  iterator begin() { return MValues.begin(); }
37 
38  const_iterator begin() const { return MValues.begin(); }
39 
40  iterator end() { return MValues.end(); }
41 
42  const_iterator end() const { return MValues.end(); }
43 
44  reference front() { return MValues.front(); }
45 
46  const_reference front() const { return MValues.front(); }
47 
48  reference back() { return MValues.back(); }
49 
50  const_reference back() const { return MValues.back(); }
51 
52  reference operator[](std::size_t Idx) { return MValues[Idx]; }
53 
54  const_reference operator[](std::size_t Idx) const { return MValues[Idx]; }
55 
56  std::size_t size() const { return MValues.size(); }
57 
58  std::size_t capacity() const { return MCapacity; }
59 
60  bool empty() const { return MValues.empty(); };
61 
62  bool full() const { return MValues.size() == MCapacity; };
63 
64  void push_back(T Val) {
65  if (MValues.size() == MCapacity)
66  MValues.pop_front();
67  MValues.push_back(std::move(Val));
68  }
69 
70  void push_front(T Val) {
71  if (MValues.size() == MCapacity)
72  MValues.pop_back();
73  MValues.push_front(std::move(Val));
74  }
75 
76  void pop_back() { MValues.pop_back(); }
77 
78  void pop_front() { MValues.pop_front(); }
79 
80  void erase(const_iterator Pos) { MValues.erase(Pos); }
81 
82  void erase(const_iterator First, const_iterator Last) {
83  MValues.erase(First, Last);
84  }
85 
86  void clear() { MValues.clear(); }
87 
88 private:
89  // Deque is used as the underlying container for double-ended push/pop
90  // operations and built-in iterator support. Frequent memory allocations
91  // and deallocations are a concern, switching to an array/vector might be a
92  // worthwhile optimization.
93  std::deque<T> MValues;
94  const std::size_t MCapacity;
95 };
96 
97 } // namespace detail
98 } // namespace _V1
99 } // namespace sycl
const_reference operator[](std::size_t Idx) const
void erase(const_iterator First, const_iterator Last)
typename std::deque< T >::const_iterator const_iterator
reference operator[](std::size_t Idx)
typename std::deque< T >::iterator iterator
CircularBuffer(std::size_t Capacity)
void erase(const_iterator Pos)
Definition: access.hpp:18