libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
Stack.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <stddef.h>
5#include <initializer_list>
6
7namespace daisy
8{
10template <typename T>
12{
13 protected:
15 : buffer_(buffer), bufferSize_(bufferSize), bufferHead_(0)
16 {
17 }
18
20 size_t bufferSize,
21 std::initializer_list<T> valuesToAdd)
22 : buffer_(buffer), bufferSize_(bufferSize), bufferHead_(0)
23 {
25 }
26
27 public:
30 {
31 bufferHead_ = 0;
32 if(!other.IsEmpty())
33 {
34 const auto numCopy = (other.GetNumElements() < bufferSize_)
35 ? other.GetNumElements()
36 : bufferSize_;
37 for(size_t i = 0; i < numCopy; i++)
38 buffer_[i] = other[i];
39 bufferHead_ = other.GetNumElements();
40 }
41 return *this;
42 }
43
45
48 bool PushBack(const T& elementToAdd)
49 {
50 if(!IsFull())
51 {
52 buffer_[bufferHead_++] = elementToAdd;
53 return true;
54 }
55 return false;
56 }
57
59 int PushBack(std::initializer_list<T> valuesToAdd)
60 {
61 int numAdded = 0;
62 for(const auto& v : valuesToAdd)
63 {
64 if(IsFull())
65 return numAdded;
66
67 PushBack(v);
68 numAdded++;
69 }
70 return numAdded;
71 }
72
75 {
76 if(IsEmpty())
77 return T();
78 else
79 {
80 return buffer_[--bufferHead_];
81 }
82 }
83
85 void Clear() { bufferHead_ = 0; }
86
89 T& operator[](uint32_t idx) { return buffer_[idx]; }
92 const T& operator[](uint32_t idx) const { return buffer_[idx]; }
93
96 {
97 if(idx >= bufferHead_)
98 return false;
99
100 for(uint32_t i = idx; i < bufferHead_ - 1; i++)
101 {
102 buffer_[i] = buffer_[i + 1];
103 }
104 bufferHead_--;
105 return true;
106 }
107
112 {
113 int numRemoved = 0;
114 int idx = bufferHead_ - 1;
115 while(idx >= 0)
116 {
117 if(buffer_[idx] == element)
118 {
119 numRemoved++;
120 Remove(idx);
121 // was that the last element?
122 if(decltype(bufferHead_)(idx) == bufferHead_)
123 idx--;
124 }
125 else
126 idx--;
127 }
128 return numRemoved;
129 }
130
132 bool Insert(uint32_t idx, const T& item)
133 {
134 if(bufferHead_ >= bufferSize_)
135 return false;
136 if(idx > bufferHead_)
137 return false;
138 if(idx == bufferHead_)
139 {
140 buffer_[bufferHead_++] = item;
141 return true;
142 }
143
144 for(uint32_t i = bufferHead_ - 1; i >= idx; i--)
145 {
146 buffer_[i + 1] = buffer_[i];
147 }
148 buffer_[idx] = item;
149 bufferHead_++;
150 return true;
151 }
152
154 bool Contains(const T& element)
155 {
156 int idx = bufferHead_ - 1;
157 while(idx >= 0)
158 {
159 if(buffer_[idx] == element)
160 return true;
161 idx--;
162 }
163 return false;
164 }
165
167 size_t CountEqualTo(const T& element)
168 {
169 size_t result = 0;
170 int idx = bufferHead_ - 1;
171 while(idx >= 0)
172 {
173 if(buffer_[idx] == element)
174 result++;
175 idx--;
176 }
177 return result;
178 }
179
181 bool IsEmpty() const { return bufferHead_ == 0; }
182
184 bool IsFull() const { return bufferHead_ == bufferSize_; }
185
187 size_t GetNumElements() const { return bufferHead_; }
188
190 size_t GetCapacity() const { return bufferSize_; }
191
192 private:
193 T* buffer_;
194 const size_t bufferSize_;
195 size_t bufferHead_;
196};
197
200template <typename T, size_t capacity>
201class Stack : public StackBase<T>
202{
203 public:
205 Stack() : StackBase<T>(buffer_, capacity) {}
206
208 explicit Stack(std::initializer_list<T> valuesToAdd)
209 : StackBase<T>(buffer_, capacity, valuesToAdd)
210 {
211 }
212
214 template <size_t otherCapacity>
216 : StackBase<T>(buffer_, capacity)
217 {
218 *this = other;
219 }
220
222 template <size_t otherCapacity>
228
229 private:
230 T buffer_[capacity];
231};
232
233} // namespace daisy
Definition leddriver.h:33
Definition Stack.h:12
const T & operator[](uint32_t idx) const
Definition Stack.h:92
bool Remove(uint32_t idx)
Definition Stack.h:95
~StackBase()
Definition Stack.h:44
bool PushBack(const T &elementToAdd)
Definition Stack.h:48
int RemoveAllEqualTo(const T &element)
Definition Stack.h:111
bool Insert(uint32_t idx, const T &item)
Definition Stack.h:132
size_t GetNumElements() const
Definition Stack.h:187
StackBase(T *buffer, size_t bufferSize)
Definition Stack.h:14
StackBase< T > & operator=(const StackBase< T > &other)
Definition Stack.h:29
T PopBack()
Definition Stack.h:74
size_t GetCapacity() const
Definition Stack.h:190
T & operator[](uint32_t idx)
Definition Stack.h:89
int PushBack(std::initializer_list< T > valuesToAdd)
Definition Stack.h:59
bool IsEmpty() const
Definition Stack.h:181
bool Contains(const T &element)
Definition Stack.h:154
size_t CountEqualTo(const T &element)
Definition Stack.h:167
void Clear()
Definition Stack.h:85
StackBase(T *buffer, size_t bufferSize, std::initializer_list< T > valuesToAdd)
Definition Stack.h:19
bool IsFull() const
Definition Stack.h:184
Definition Stack.h:202
Stack()
Definition Stack.h:205
Stack(const Stack< T, otherCapacity > &other)
Definition Stack.h:215
Stack(std::initializer_list< T > valuesToAdd)
Definition Stack.h:208
Stack< T, capacity > & operator=(const Stack< T, otherCapacity > &other)
Definition Stack.h:223
Hardware defines and helpers for daisy field platform.
Definition index.h:2