SPH
CircularArray.h
Go to the documentation of this file.
1 #pragma once
2 
4 
6 
7 template <typename T>
8 class CircularArray {
9 private:
10  Array<T> queue;
11  Size head = 0;
12 
13 public:
14  CircularArray(const Size maxSize)
15  : queue(0, maxSize) {}
16 
17  void push(const T& value) {
18  if (queue.size() < queue.capacity()) {
19  queue.push(value);
20  } else {
21  queue[head] = value;
22  head = this->wrap(1);
23  }
24  }
25 
26  void push(T&& value) {
27  if (queue.size() < queue.capacity()) {
28  queue.push(std::move(value));
29  } else {
30  queue[head] = std::move(value);
31  head = this->wrap(1);
32  }
33  }
34 
35  Size size() const {
36  return queue.size();
37  }
38 
39  INLINE const T& operator[](const Size i) const {
40  SPH_ASSERT(i < queue.size());
41  return queue[this->wrap(i)];
42  }
43 
44  INLINE T& operator[](const Size i) {
45  SPH_ASSERT(i < queue.size());
46  return queue[this->wrap(i)];
47  }
48 
49 private:
50  Size wrap(const Size i) const {
51  return (i + head) % queue.size();
52  }
53 };
54 
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
INLINE TCounter capacity() const noexcept
Definition: Array.h:197
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE const T & operator[](const Size i) const
Definition: CircularArray.h:39
Size size() const
Definition: CircularArray.h:35
INLINE T & operator[](const Size i)
Definition: CircularArray.h:44
void push(T &&value)
Definition: CircularArray.h:26
CircularArray(const Size maxSize)
Definition: CircularArray.h:14
void push(const T &value)
Definition: CircularArray.h:17