-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.hpp
143 lines (118 loc) · 3.54 KB
/
Stack.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/********************************************************************************
* Purpose: *
* Stack data structure. First in Last Out *
* Author: *
* Anilcan Gulkaya 2023 anilcangulkaya7@gmail.com *
********************************************************************************/
#pragma once
#include "Memory.hpp"
AX_NAMESPACE
template<typename T, typename AllocatorT = Allocator<T>>
class Stack
{
public:
using Iterator = T*;
using ConstIterator = const T*;
static const int InitialSize = AllocatorT::InitialSize;
public:
int size = 0;
int capacity = 0;
T* ptr = nullptr;
AllocatorT allocator{};
public:
Stack() : size(0), capacity(0), ptr(nullptr)
{ }
~Stack()
{
Clear();
}
explicit Stack(int _size) : size(0), capacity(CalculateArrayGrowth(_size))
{
ptr = allocator.AllocateUninitialized(capacity);
}
// copy constructor
Stack(const Stack& other)
: size(other.size), capacity(other.capacity)
{
if (&other != this)
{
if (other.capacity > capacity)
GrowIfNecessarry(other.capacity - capacity);
Copy(ptr, other.ptr, other.Size());
size = other.Size();
}
}
// move constructor
Stack(Stack&& other)
{
if (ptr != nullptr)
allocator.Deallocate(ptr, capacity);
size = other.size;
capacity = other.capacity;
ptr = other.ptr;
other.ptr = nullptr;
other.size = other.capacity = 0;
}
T& operator[](int index) { ASSERT(index >= capacity); return ptr[index]; }
const T& operator[](int index) const { ASSERT(index >= capacity); return ptr[index]; }
T* begin() { return ptr; }
T* end() { return ptr + size; }
const T* cbegin() const { return ptr; }
const T* cend() const { return ptr + size; }
const T& Top() const { return ptr[size - 1]; }
T& Top() { return ptr[size - 1]; }
bool Any() const { return size > 0; }
bool Empty() const { return size == 0; }
void Clear()
{
if (ptr)
{
allocator.Deallocate(ptr, capacity);
ptr = nullptr;
size = capacity = 0;
}
}
template<typename ... Args>
T& Emplace(Args&&... args)
{
GrowIfNecessarry();
new (ptr + size) T(Forward<Args>(args)...);
return ptr[size++];
}
void PushArray(const T* arr, int cnt)
{
GrowIfNecessarry(cnt);
Copy(ptr + size, arr, cnt);
size += cnt;
}
void Push(const T& value)
{
GrowIfNecessarry();
ptr[size++] = value;
}
T Pop()
{
ASSERTR(size != 0, return);
return Forward<T>(ptr[--size]);
}
bool TryPop(T& out)
{
if (size > 0)
out = Forward<T>(ptr[--size]);
return size > 0;
}
private:
void GrowIfNecessarry(int add = 1)
{
if (size + add >= capacity)
{
int newSize = size + add <= InitialSize ? InitialSize : CalculateArrayGrowth(capacity);
if (ptr) ptr = allocator.Reallocate(ptr, capacity, newSize);
else ptr = allocator.Allocate(newSize);
capacity = newSize;
}
}
};
template<typename T, int size>
using FixedStack = Stack<T, StackAllocator<T, size>>;
AX_END_NAMESPACE