Skip to content

Commit ab2148d

Browse files
committed
change from_vector to from_values
1 parent 788e139 commit ab2148d

File tree

1 file changed

+100
-14
lines changed

1 file changed

+100
-14
lines changed

DynamicVector/DynamicVector.py

Lines changed: 100 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,39 @@ def __init__(self, dtype=np.int32, capacity=8, *, grow_use_add=None, grow_add=No
8686
self._zero = self._data[0]
8787

8888
@classmethod
89-
def from_vector(cls, vector, *, grow_use_add=None, grow_add=None):
89+
def from_values(cls, values, *, grow_use_add=None, grow_add=None):
9090
"""
9191
Create a DynamicVector from an existing vector.
9292
9393
Parameters:
94-
vector (sequence): The source array to initialize the vector.
94+
values (sequence): The source array to initialize the vector.
9595
grow_use_add (int, optional): Custom threshold to switch from multiplicative to additive growth.
9696
grow_add (int, optional): Custom value for additive growth.
9797
9898
Returns:
9999
DynamicVector: A new dynamic vector initialized with the values from the input vector.
100100
"""
101101
try:
102-
capacity = len(vector)
102+
capacity = len(values)
103103
except TypeError:
104-
return cls.from_iter(vector, grow_use_add, grow_add)
104+
return cls.from_iter(values, grow_use_add, grow_add)
105105

106106
try:
107-
dtype = vector.dtype
107+
dtype = values.dtype
108108
except AttributeError:
109109
try:
110-
dtype = type(vector[0])
110+
dtype = type(values[0])
111111
except IndexError:
112-
raise ValueError("Either pass variable with dtype attribute, or len(vector) must be greater than zero.")
112+
raise ValueError("Either pass variable with dtype attribute, or len(values) must be greater than zero.")
113113

114-
if isinstance(vector, DynamicVector):
114+
if isinstance(values, DynamicVector):
115115
if grow_use_add is None:
116-
grow_use_add = vector.grow_use_add
116+
grow_use_add = values.grow_use_add
117117
if grow_add is None:
118-
grow_add = vector.grow_add
118+
grow_add = values.grow_add
119119

120120
dyn = cls(dtype, capacity, grow_use_add=grow_use_add, grow_add=grow_add)
121-
dyn.extend(vector)
121+
dyn.extend(values)
122122
return dyn
123123

124124
@classmethod
@@ -184,6 +184,10 @@ def view(self) -> np.ndarray:
184184
but instead use `self.view` it as needed."""
185185
return self._data[: self._size]
186186

187+
# @view.setter
188+
# def view(self, value):
189+
# self._data[: self._size] = value
190+
187191
@property
188192
def dtype(self) -> np.dtype:
189193
"""Returns the data type of the vector."""
@@ -509,10 +513,14 @@ def __len__(self) -> int:
509513
return self._size
510514

511515
def __repr__(self):
512-
return repr(self.view)
516+
dyn = repr(self.view)
517+
dyn = dyn[dyn.find("(") :] # drop "array" from name
518+
return f"DynamicVector{dyn}"
513519

514520
def __str__(self):
515-
return str(self.view)
521+
dyn = repr(self.view)
522+
dyn = dyn[dyn.find("[") : dyn.find("]") + 1] # get core part of numpy array
523+
return f"DynamicVector({dyn})"
516524

517525
def __iter__(self):
518526
return iter(self.view)
@@ -801,4 +809,82 @@ def _reverse_in_place(array):
801809
x.append(i)
802810
print(x)
803811

804-
x = DynamicVector.from_vector([1, 2, 3, 4, 5])
812+
x = DynamicVector.from_values([1, 2, 3, 4, 5])
813+
814+
# Initialize with a list
815+
vec = DynamicVector.from_values([1, 2, 3]) # integer vector with three values
816+
817+
print(vec)
818+
print(repr(vec))
819+
# Output: DynamicVector([1, 2, 3])
820+
# Output: DynamicVector([1, 2, 3], dtype=int32)
821+
822+
# Access the underlying NumPy array via the 'view' property
823+
print(vec.view)
824+
print(vec[:])
825+
# Output: [1 2 3]
826+
# Output: [1 2 3] -> same as vec.view
827+
828+
# Perform NumPy operations
829+
vec += 1
830+
print(vec)
831+
# Output: DynamicVector([2, 3, 4])
832+
833+
vec[1] = 99
834+
print(vec)
835+
# Output: DynamicVector([ 2, 99, 4])
836+
837+
vec[1 : len(vec)] = 8 # set element 2 and 3 to the value of 8.
838+
print(vec)
839+
# Output: DynamicVector([2, 8, 8])
840+
841+
vec[:] = [2, 3, 4]
842+
print(vec)
843+
# Output: DynamicVector([2, 3, 4])
844+
845+
# Append elements dynamically
846+
vec.append(5) # Fast operation
847+
print(vec)
848+
# Output: DynamicVector([2, 3, 4, 5])
849+
850+
vec.extend([7, 8, 9]) # Fast operation
851+
print(vec)
852+
# Output: DynamicVector([1, 2, 3, 5, 7, 8, 9])
853+
854+
# Insert at a specific index
855+
vec.insert(1, 10)
856+
print(vec)
857+
# Output: DynamicVector([ 1, 10, 2, 3, 5, 7, 8, 9])
858+
859+
# Insert at a specific index
860+
vec.insert_values(3, [97, 98])
861+
print(vec)
862+
# Output: DynamicVector([ 1, 10, 2, 97, 98, 3, 5, 7, 8, 9])
863+
864+
# Remove and return the last element
865+
print(vec)
866+
last_elem = vec.pop() # Fast operation
867+
print(vec)
868+
print(last_elem)
869+
# Output: DynamicVector([ 1, 10, 2, 97, 98, 3, 5, 7, 8, 9])
870+
# Output: DynamicVector([ 1, 10, 2, 97, 98, 3, 5, 7, 8])
871+
# Output: 9
872+
873+
third_element = vec.pop(2)
874+
print(vec)
875+
print(third_element)
876+
# Output: DynamicVector([ 1, 10, 97, 98, 3, 5, 7, 8])
877+
# Output: 2
878+
879+
# Slice behaves like NumPy arrays
880+
sliced_vec = vec[1:3]
881+
print(sliced_vec)
882+
# Output: [10 97]
883+
884+
vec[2:5] = [51, 52, 53]
885+
print(vec)
886+
# Output: DynamicVector([ 1, 10, 51, 52, 53, 5, 7, 8])
887+
888+
vec[[1, 3, 5]] = [-1, -2, -3]
889+
print(vec)
890+
# Output: DynamicVector([ 1, -1, 51, -2, 53, -3, 7, 8])

0 commit comments

Comments
 (0)