-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppendArray.py
71 lines (54 loc) · 1.74 KB
/
AppendArray.py
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
import numpy as np
class AppendArray():
"""
A wrapper around a numpy array that supports the append operation, but does it
without making copies. It does this by doubling the size of the array every time
it fills up, amortizing the cost of appending.
This is a utility class, and does not need to be used by users of the simulator.
"""
def __init__(self, init_x, dt):
"""
Parameters
----------
init_x:
The initial length of the array
y:
The width of the array (number of data per row)
dt:
The numpy data type for a row
"""
self.data = np.zeros(init_x, dtype=dt)
self.full = 0
self.dt = dt
def append(self, val):
"""
Append a row to the array.
Parameters
----------
val:
The row (of type dt) to append to the array. The array will be expanded if needed.
"""
val = np.array(tuple(val), dtype=self.dt)
if self.full < len(self.data):
self.data[self.full] = val
self.full += 1
else:
self.data = np.lib.pad(self.data, (0,self.data.shape[0]),
'constant', constant_values=(0))
self.data[self.full] = val
self.full += 1
def shrink(self):
"""
Shrink the array to the size of however many elements are in it.
"""
self.data = self.data[:self.full]
def get(self):
"""
Get a copy of they numpy array associated with this _AppendArray
"""
return self.data[:self.full].copy()
def reset(self):
"""
Reset the contents of this array
"""
self.full = 0