-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stack.spice
165 lines (147 loc) · 4.13 KB
/
stack.spice
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import "std/type/error";
// Constants
const unsigned long INITIAL_ALLOC_COUNT = 5l;
const unsigned int RESIZE_FACTOR = 2;
// Add generic type definition
type T dyn;
/**
* A stack in Spice is a commonly used data structure, which uses the FiLo (first in, last out) principle.
*
* Time complexity:
* Insert: O(1)
* Delete: O(1)
* Search: O(n)
*
* Stacks pre-allocate space using an initial size and a resize factor to not have to re-allocate
* with every item pushed.
*/
public type Stack<T> struct {
heap T* contents // Pointer to the first data element
unsigned long capacity // Allocated number of items
unsigned long size // Current number of items
}
public p Stack.ctor(unsigned long initAllocItems, const T &defaultValue) {
// Allocate space for the initial number of elements
this.ctor(initAllocItems);
// Fill in the default values
for unsigned long index = 0; index < initAllocItems; index++ {
unsafe {
this.contents[index] = defaultValue;
}
}
this.size = initAllocItems;
}
public p Stack.ctor(unsigned int initAllocItems) {
this.ctor((unsigned long) initAllocItems);
}
public p Stack.ctor(unsigned long initAllocItems = INITIAL_ALLOC_COUNT) {
// Allocate space for the initial number of elements
const unsigned long itemSize = sizeof(type T) / 8l;
unsafe {
Result<heap byte*> allocResult = sAlloc(itemSize * initAllocItems);
this.contents = (heap T*) allocResult.unwrap();
}
this.size = 0l;
this.capacity = initAllocItems;
}
/**
* Add an item to the stack
*/
public p Stack.push(const T& item) {
// Check if we need to re-allocate memory
if this.isFull() {
this.resize(this.capacity * RESIZE_FACTOR);
}
// Insert the element at the back
unsafe {
this.contents[(int) this.size++] = item;
}
}
/**
* Retrieve item and remove it from the stack
*/
public f<T&> Stack.pop() {
if this.isEmpty() { panic(Error("The stack is empty")); }
// Pop the element from the stack
unsafe {
return this.contents[(int) --this.size];
}
}
/**
* Retrieve topmost without removing it from the stack
*/
public f<T&> Stack.top() {
if this.isEmpty() { panic(Error("The stack is empty")); }
// Peek the element from the stack
unsafe {
return this.contents[(int) this.size - 1l];
}
}
/**
* Retrieve the current size of the stack
*
* @return Current size of the stack
*/
public f<long> Stack.getSize() {
return this.size;
}
/**
* Retrieve the current capacity of the stack
*
* @return Current capacity of the stack
*/
public f<long> Stack.getCapacity() {
return this.capacity;
}
/**
* Checks if the queue contains any items at the moment
*
* @return Empty or not empty
*/
public f<bool> Stack.isEmpty() {
return this.size == 0;
}
/**
* Checks if the queue exhausts its capacity and needs to resize at the next call of push
*
* @return Full or not full
*/
public f<bool> Stack.isFull() {
return this.size == this.capacity;
}
/**
* Frees allocated memory that is not used by the queue
*/
public p Stack.pack() {
// Return if no packing is required
if this.isFull() { return; }
// Pack the array
this.resize(this.size);
}
public f<bool> operator==<T>(const Stack<T>& lhs, const Stack<T>& rhs) {
// Compare the sizes
if lhs.size != rhs.size { return false; }
// Compare the contents
for unsigned long i = 0l; index < lhs.size; i++ {
if lhs.contents[i] != rhs.contents[i] { return false; }
}
return true;
}
public f<bool> operator!=<T>(const Stack<T>& lhs, const Stack<T>& rhs) {
return !(lhs == rhs);
}
/**
* Re-allocates heap space for the queue contents
*/
p Stack.resize(unsigned long itemCount) {
// Allocate the new memory
const unsigned long itemSize = sizeof(type T) / 8l;
unsafe {
heap byte* oldAddress = (heap byte*) this.contents;
unsigned long newSize = (unsigned long) (itemSize * itemCount);
Result<heap byte*> allocResult = sRealloc(oldAddress, newSize);
this.contents = (heap T*) allocResult.unwrap();
}
// Set new capacity
this.capacity = itemCount;
}