Skip to content

Commit 60a25cb

Browse files
committed
add zeros constructor method
1 parent 7ec6eb9 commit 60a25cb

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

DynamicVector/DynamicVector.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,63 @@ def from_iter(cls, iterator, capacity=8, *, grow_use_add=None, grow_add=None):
303303
dyn.append(value)
304304
return dyn
305305

306+
@classmethod
307+
def zeros(cls, *args, size=None, dtype=None, capacity=None, grow_use_add=None, grow_add=None):
308+
"""
309+
Create a DynamicVector composed of `size` zero values with a specified data type (dtype).
310+
If dtype is not given, then it is set to `np.int32`.
311+
312+
DynamicVector.zeros(size, dtype=np.int32, capacity=8, *, grow_use_add=None, grow_add=None)
313+
314+
`zeros` can be called with a varying number of positional arguments:
315+
316+
- `zeros(size)`: Creates a np.int32 vector that can hold `size` zeros
317+
- `zeros(size, dtype)`: Creates a `dtype` vector that can hold `size` zeros
318+
- `zeros(size, dtype, capacity)` Creates a `dtype` vector that can hold `size` zeros
319+
with a minimum internal `capacity`.
320+
321+
`zeros` can also use keyword argument `size`, `dtype` and `capacity`.
322+
However, if a positional argument of the same name is used, as described above,
323+
then only the keywords that are not one of the position arguments names can be used.
324+
Such as:
325+
326+
- `zeros(size)`: Cannot use the keyword `size`.
327+
- `zeros(size, dtype)`: Cannot use the keywords `size`, `dtype`.
328+
- `zeros(size, dtype, capacity)` Cannot use the keywords `size`, `dtype`, `capacity`.
329+
330+
Parameters:
331+
size (int): The number of zeros in the vector (size of DynamicVector).
332+
dtype (type, optional): The type of the zeros in the vector. Defaults to `np.int32`.
333+
capacity (int, optional): Initial minimum capacity of the underlying storage vector. Defaults to max(8, size).
334+
335+
Returns:
336+
DynamicVector of zeros with the given size, dtype, and capacity.
337+
338+
Examples:
339+
DynamicVector.zeros(5) -> DynamicVector([0, 1, 2, 3, 4])
340+
DynamicVector.zeros(2, 5) -> DynamicVector([2, 3, 4])
341+
DynamicVector.zeros(2, 10, step=2) -> DynamicVector([2, 4, 6, 8])
342+
DynamicVector.zeros(0, 5, step=0.5) -> DynamicVector([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5])
343+
"""
344+
narg = len(args)
345+
if size is None and narg == 0:
346+
raise TypeError("DynamicVector.zeros() must specify at least one argument or use the `size` keyword.")
347+
if narg > 3:
348+
raise TypeError(f"arange() takes from 0 to 3 positional arguments but {narg} were given")
349+
350+
if narg in (0, 1) and dtype is None:
351+
dtype = np.int32
352+
353+
if narg in (0, 1, 2) and capacity is None:
354+
capacity = 8
355+
356+
if capacity < size:
357+
capacity = size
358+
359+
dyn = cls(dtype, capacity, grow_use_add=grow_use_add, grow_add=grow_add)
360+
dyn._size = size
361+
return dyn
362+
306363
@property
307364
def size(self) -> int:
308365
"""Returns the current size of the vector."""

0 commit comments

Comments
 (0)