-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstack.py
More file actions
40 lines (31 loc) · 911 Bytes
/
stack.py
File metadata and controls
40 lines (31 loc) · 911 Bytes
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
import taichi as ti
import taichi.math as tm
@ti.data_oriented
class Stack:
def __init__(self, dtype, max_stack) -> None:
self._field = ti.field(dtype=dtype, shape=(max_stack, ))
self.index = ti.field(dtype=ti.int32, shape=())
self.index[None] = -1
@ti.func
def push(self, val):
self.index[None] += 1
self._field[self.index[None]] = val
@ti.func
def pop(self):
val = self._field[self.index[None]]
self.index[None] -= 1
return val
@ti.func
def clear(self):
self.index[None] = -1
@ti.func
def length(self):
return self.index[None] + 1
@ti.func
def is_empty(self):
return self.index[None] < 0
def to_list(self):
ret = []
for i in range(self.index[None] + 1):
ret.append(self._field[i])
return ret