@@ -23,7 +23,7 @@ class OneDimensionalArray(Array):
23
23
A valid object type.
24
24
size: int
25
25
The number of elements in the array.
26
- elements: list/tuple
26
+ elements: list
27
27
The elements in the array, all should
28
28
be of same type.
29
29
init: a python type
@@ -73,12 +73,18 @@ def __new__(cls, dtype=NoneType, *args, **kwargs):
73
73
obj = object .__new__ (cls )
74
74
obj ._dtype = dtype
75
75
if len (args ) == 2 :
76
- if _check_type (args [0 ], ( list , tuple ) ) and \
76
+ if _check_type (args [0 ], list ) and \
77
77
_check_type (args [1 ], int ):
78
- size , data = args [1 ], [dtype (arg ) for arg in args [0 ]]
79
- elif _check_type (args [1 ], (list , tuple )) and \
78
+ for i in range (len (args [0 ])):
79
+ if dtype != type (args [0 ][i ]):
80
+ args [0 ][i ] = dtype (args [0 ][i ])
81
+ size , data = args [1 ], [arg for arg in args [0 ]]
82
+ elif _check_type (args [1 ], list ) and \
80
83
_check_type (args [0 ], int ):
81
- size , data = args [0 ], [dtype (arg ) for arg in args [1 ]]
84
+ for i in range (len (args [1 ])):
85
+ if dtype != type (args [1 ][i ]):
86
+ args [1 ][i ] = dtype (args [1 ][i ])
87
+ size , data = args [0 ], [arg for arg in args [1 ]]
82
88
else :
83
89
raise TypeError ("Expected type of size is int and "
84
90
"expected type of data is list/tuple." )
@@ -93,8 +99,11 @@ def __new__(cls, dtype=NoneType, *args, **kwargs):
93
99
init = kwargs .get ('init' , None )
94
100
obj ._data = [init for i in range (args [0 ])]
95
101
elif _check_type (args [0 ], (list , tuple )):
102
+ for i in range (len (args [0 ])):
103
+ if dtype != type (args [0 ][i ]):
104
+ args [0 ][i ] = dtype (args [0 ][i ])
96
105
obj ._size , obj ._data = len (args [0 ]), \
97
- [dtype ( arg ) for arg in args [0 ]]
106
+ [arg for arg in args [0 ]]
98
107
else :
99
108
raise TypeError ("Expected type of size is int and "
100
109
"expected type of data is list/tuple." )
@@ -110,7 +119,9 @@ def __setitem__(self, idx, elem):
110
119
if elem is None :
111
120
self ._data [idx ] = None
112
121
else :
113
- self ._data [idx ] = self ._dtype (elem )
122
+ if type (elem ) != self ._dtype :
123
+ elem = self ._dtype (elem )
124
+ self ._data [idx ] = elem
114
125
115
126
def fill (self , elem ):
116
127
elem = self ._dtype (elem )
@@ -237,8 +248,40 @@ def delete(self, idx):
237
248
self [idx ] != None :
238
249
self [idx ] = None
239
250
self ._num -= 1
240
- self ._modify ()
251
+ return self ._modify ()
241
252
242
253
@property
243
254
def size (self ):
244
255
return self ._size
256
+
257
+ class ArrayForTrees (DynamicOneDimensionalArray ):
258
+ """
259
+ Utility dynamic array for storing nodes of a tree.
260
+
261
+ See Also
262
+ ========
263
+
264
+ pydatastructs.linear_data_structures.arrays.DynamicOneDimensionalArray
265
+ """
266
+ def _modify (self ):
267
+ if self ._num / self ._size < self ._load_factor :
268
+ new_indices = dict ()
269
+ arr_new = OneDimensionalArray (self ._dtype , 2 * self ._num + 1 )
270
+ j = 0
271
+ for i in range (self ._last_pos_filled + 1 ):
272
+ if self [i ] != None :
273
+ arr_new [j ] = self [i ]
274
+ new_indices [self [i ].key ] = j
275
+ j += 1
276
+ for i in range (j ):
277
+ if arr_new [i ].left != None :
278
+ arr_new [i ].left = new_indices [self [arr_new [i ].left ].key ]
279
+ if arr_new [i ].right != None :
280
+ arr_new [i ].right = new_indices [self [arr_new [i ].right ].key ]
281
+ if arr_new [i ].parent != None :
282
+ arr_new [i ].parent = new_indices [self [arr_new [i ].parent ].key ]
283
+ self ._last_pos_filled = j - 1
284
+ self ._data = arr_new ._data
285
+ self ._size = arr_new ._size
286
+ return new_indices
287
+ return None
0 commit comments