forked from wjw12/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyArray.py
95 lines (94 loc) · 2.6 KB
/
PyArray.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import ctypes
class array:
def __init__(self,size):
assert size > 0, "Array size must be > 0"
self.size = size
PyArray = ctypes.py_object * size
self.elements = PyArray()
self.clear(None)
def __len__(self):
return self.size
def __getitem__(self,index):
assert index >= 0 and index < len(self), "Illegal index"
return self.elements[index]
def __setitem__(self,index,item):
assert index >= 0 and index < len(self), "Illegal index"
self.elements[index] = item
def clear(self,value):
for i in range(len(self)):
self.elements[i] = value
def __iter__(self):
return arrayIter(self,self.elements)
class arrayIter:
def __init__(self,theArray):
self.array = theArray
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < len(self.array):
item = self.array[self.index]
self.index += 1
return item
else:
raise StopIteration
class myArray2D(array):
def __init__(self,nRows,nCols):
array.__init__(self,nRows * nCols)
self.nRows = nRows
self.nCols = nCols
def __getitem__(self,indexTuple):
assert len(indexTuple) == 2, "Illegal Index"
row = indexTuple[0]
col = indexTuple[1]
assert row >= 0 and row < self.nRows \
and col >= 0 and col < self.nCols,\
"Illegal Index"
index = row * self.nCols + col
return array.__getitem__(self,index)
def __setitem__(self,indexTuple,item):
assert len(indexTuple) == 2, "Illegal Index"
row = indexTuple[0]
col = indexTuple[1]
assert row >= 0 and row < self.nRows \
and col >= 0 and col < self.nCols,\
"Illegal Index"
index = row * self.nCols + col
array.__setitem__(self,index,item)
class array2D:
def __init__(self,nRows,nCols):
self.nRows = nRows
self.nCols = nCols
self.rows = array(nRows)
for i in range(nRows):
self.rows[i] = array(nCols)
def numOfRows(self):
return self.nRows
def numOfCols(self):
return self.nCols
def __getitem__(self,indexTuple):
assert len(indexTuple) == 2, "Illegal Index"
row = indexTuple[0]
col = indexTuple[1]
assert row >= 0 and row < self.nRows \
and col >= 0 and col < self.nCols,\
"Illegal Index"
theArray = self.rows[row]
return theArray[col]
def __setitem__(self,indexTuple,item):
assert len(indexTuple) == 2, "Illegal Index"
row = indexTuple[0]
col = indexTuple[1]
assert row >= 0 and row < self.nRows \
and col >= 0 and col < self.nCols,\
"Illegal Index"
theArray = self.rows[row]
theArray[col] = item
a = array(10)
for i in range(10):
a[i] = 2**i
print a[i]
aa = myArray2D(5):
aa[i,j] = i * j
print aa[i,j]
print