-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.py
34 lines (34 loc) · 1.19 KB
/
array.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
import ctypes
class Array:
def __init__(self,size):
assert size > 0, "Array size must be > 0"
self._size = size
PyArrayType = ctypes.py_object * size
self._elements = PyArrayType()
self.clear( None )
def __len__( self ):
return self._size
def __getitem__( self, index ):
assert index >= 0 and index < len(self), "Array subscript out of range"
return self._elements[ index ]
def __setitem__( self, index, value ):
assert index >= 0 and index < len(self), "Array subscript out of range"
self._elements[ index ] = value
def clear( self, value ):
for i in range( len(self) ) :
self._elements[i] = value
def __iter__( self ):
return _ArrayIterator( self._elements )
class _ArrayIterator:
def __init__( self, theArray ):
self._arrayRef = theArray
self._curNdx = 0
def __iter__( self ):
return self
def __next__( self ):
if self._curNdx < len( self._arrayRef ) :
entry = self._arrayRef[ self._curNdx ]
self._curNdx += 1
return entry
else :
raise StopIteration