Skip to content

Commit 7ec6eb9

Browse files
committed
add capacity argument to from_values and from_iter
1 parent cade8e3 commit 7ec6eb9

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

DynamicVector/DynamicVector.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,22 @@ def __init__(self, dtype=np.int32, capacity=8, *, grow_use_add=None, grow_add=No
226226
# self._zero = self._data[0]
227227

228228
@classmethod
229-
def from_values(cls, values, *, grow_use_add=None, grow_add=None):
229+
def from_values(cls, values, capacity=8, *, grow_use_add=None, grow_add=None):
230230
"""
231231
Create a DynamicVector from an existing vector.
232232
233233
Parameters:
234-
values (sequence): The source array to initialize the vector.
234+
values (sequence): The source array to initialize the vector.
235+
capacity (int, optional): Initial minimum capacity of the underlying storage vector. Defaults to max(8, len(values)).
235236
grow_use_add (int, optional): Custom threshold to switch from multiplicative to additive growth.
236-
grow_add (int, optional): Custom value for additive growth.
237+
grow_add (int, optional): Custom value for additive growth.
237238
238239
Returns:
239240
DynamicVector: A new dynamic vector initialized with the values from the input vector.
240241
"""
241242
try:
242-
capacity = len(values)
243+
if len(values) > capacity:
244+
capacity = len(values)
243245
except TypeError:
244246
return cls.from_iter(values, grow_use_add, grow_add)
245247

@@ -262,14 +264,15 @@ def from_values(cls, values, *, grow_use_add=None, grow_add=None):
262264
return dyn
263265

264266
@classmethod
265-
def from_iter(cls, iterator, *, grow_use_add=None, grow_add=None):
267+
def from_iter(cls, iterator, capacity=8, *, grow_use_add=None, grow_add=None):
266268
"""
267269
Create a DynamicVector from an iterator.
268270
269271
Parameters:
270-
iterator (iterator): The source iterator to initialize the vector.
272+
iterator (iterator): The source iterator to initialize the vector.
273+
capacity (int, optional): Initial minimum capacity of the underlying storage vector. Defaults to 8.
271274
grow_use_add (int, optional): Custom threshold to switch from multiplicative to additive growth.
272-
grow_add (int, optional): Custom value for additive growth.
275+
grow_add (int, optional): Custom value for additive growth.
273276
274277
Returns:
275278
DynamicVector: A new dynamic vector initialized with the values from the input iterator.
@@ -294,7 +297,7 @@ def from_iter(cls, iterator, *, grow_use_add=None, grow_add=None):
294297
if grow_add is None:
295298
grow_add = iterator.grow_add
296299

297-
dyn = cls(dtype, grow_use_add=grow_use_add, grow_add=grow_add)
300+
dyn = cls(dtype, capacity, grow_use_add=grow_use_add, grow_add=grow_add)
298301
dyn.append(value)
299302
for value in iterator:
300303
dyn.append(value)

0 commit comments

Comments
 (0)