PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Stack.h
Go to the documentation of this file.
1
9
10#ifndef PBAT_COMMON_STACK_H
11#define PBAT_COMMON_STACK_H
12
13#include "pbat/HostDevice.h"
14
15namespace pbat {
16namespace common {
17
24template <class T, auto kCapacity = 64>
25class Stack
26{
27 public:
31 PBAT_HOST_DEVICE Stack() : stack{}, size{0} {}
37 PBAT_HOST_DEVICE void Push(T value) { stack[size++] = value; }
43 PBAT_HOST_DEVICE T Pop() { return stack[--size]; }
49 PBAT_HOST_DEVICE T const& Top() const { return stack[size - 1]; }
55 PBAT_HOST_DEVICE auto Size() const { return size; }
61 PBAT_HOST_DEVICE bool IsEmpty() const { return size == 0; }
67 PBAT_HOST_DEVICE bool IsFull() const { return size == kCapacity; }
71 PBAT_HOST_DEVICE void Clear() { size = 0; }
80 PBAT_HOST_DEVICE T& operator[](auto i) { return stack[i]; }
89 PBAT_HOST_DEVICE T const& operator[](auto i) const { return stack[i]; }
95 PBAT_HOST_DEVICE T* begin() { return stack; }
101 PBAT_HOST_DEVICE T* end() { return stack + size; }
102
103 private:
104 T stack[kCapacity];
105 int size;
106};
107
108} // namespace common
109} // namespace pbat
110
111#endif // PBAT_COMMON_STACK_H
PBAT_HOST_DEVICE T const & operator[](auto i) const
Read-only access element at index i.
Definition Stack.h:89
PBAT_HOST_DEVICE Stack()
Construct empty Stack.
Definition Stack.h:31
PBAT_HOST_DEVICE bool IsEmpty() const
Check if the stack is empty.
Definition Stack.h:61
PBAT_HOST_DEVICE T * begin()
Pointer to the beginning of the stack.
Definition Stack.h:95
PBAT_HOST_DEVICE auto Size() const
Get the number of elements in the stack.
Definition Stack.h:55
PBAT_HOST_DEVICE void Clear()
Clear the stack.
Definition Stack.h:71
PBAT_HOST_DEVICE void Push(T value)
Add element to the stack.
Definition Stack.h:37
PBAT_HOST_DEVICE T * end()
Pointer to the end of the stack.
Definition Stack.h:101
PBAT_HOST_DEVICE T const & Top() const
Get the top element of the stack.
Definition Stack.h:49
PBAT_HOST_DEVICE T Pop()
Remove the top element from the stack.
Definition Stack.h:43
PBAT_HOST_DEVICE bool IsFull() const
Check if the stack is full.
Definition Stack.h:67
PBAT_HOST_DEVICE T & operator[](auto i)
Access element at index i.
Definition Stack.h:80
Common functionality.
Definition ArgSort.h:20
The main namespace of the library.
Definition Aliases.h:15