Repository navigation
Expand file tree
/
Copy pathstack.py
More file actions
112 lines (85 loc) · 2.36 KB
/
Copy pathstack.py
File metadata and controls
112 lines (85 loc) · 2.36 KB
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
"""
This example demonstrates jitclasses and deferred type.
This is an extension to the simpler singly-linked-list example in
``linkedlist.py``.
Here, we make a better interface in the Stack class that encapsuate the
underlying linked-list.
"""
from __future__ import print_function, absolute_import
from numba.utils import OrderedDict
from numba import njit
from numba import jitclass
from numba import deferred_type, intp, optional
from numba.runtime import rtsys
linkednode_spec = OrderedDict()
linkednode_type = deferred_type()
linkednode_spec['data'] = data_type = deferred_type()
linkednode_spec['next'] = optional(linkednode_type)
@jitclass(linkednode_spec)
class LinkedNode(object):
def __init__(self, data):
self.data = data
self.next = None
linkednode_type.define(LinkedNode.class_type.instance_type)
stack_spec = OrderedDict()
stack_spec['head'] = optional(linkednode_type)
stack_spec['size'] = intp
@jitclass(stack_spec)
class Stack(object):
def __init__(self):
self.head = None
self.size = 0
def push(self, data):
new = LinkedNode(data)
new.next = self.head
self.head = new
self.size += 1
def pop(self):
old = self.head
if old is None:
raise ValueError("empty")
else:
self.head = old.next
self.size -= 1
return old.data
data_type.define(intp)
@njit
def pushpop(size):
"""
Creates a list of decending numbers from size-1 to 0.
"""
stack = Stack()
for i in range(size):
stack.push(i)
out = []
while stack.size > 0:
out.append(stack.pop())
return out
def test_pushpop(size):
"""
Test basic push pop operation on a Stack object
"""
result = pushpop(size)
print("== Result ==")
print(result)
assert result == list(reversed(range(size)))
def test_exception():
"""
Test exception raised from a jit method
"""
stack = Stack()
stack.push(1)
assert 1 == stack.pop()
try:
# Unfortunately, numba will leak when an exception is thrown.
stack.pop()
except ValueError as e:
assert 'empty' == str(e)
def runme():
size = 24
test_pushpop(size)
test_exception()
if __name__ == '__main__':
runme()
print("== Print memory allocation information == ")
print(rtsys.get_allocation_stats())