-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_implementation.py
More file actions
35 lines (26 loc) · 985 Bytes
/
array_implementation.py
File metadata and controls
35 lines (26 loc) · 985 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
import ctypes
class MyArray:
@classmethod
def resize(cls, source: ctypes.POINTER(ctypes.c_int), newSize: int): # type: ignore
# Check if newSize is valid
if newSize <= 0 or source is None:
return
# Allocate a new array of size newSize
newArray = (ctypes.c_int * newSize)()
# Copy the contents of the old array into the new array
ctypes.memmove(
newArray,
source.contents,
ctypes.sizeof(ctypes.c_int) * len(source.contents),
)
# Update the reference to the source array with the new array
source.contents = newArray
# print(list(source.contents))
print(list(newArray))
if __name__ == "__main__":
sourceArr = (ctypes.c_int * 3)(4654, 921, 762)
print(len(sourceArr))
sourceArrPointer = ctypes.pointer(sourceArr)
MyArray.resize(sourceArrPointer, 7)
print(list(sourceArrPointer.contents))
print(list(sourceArr))