Skip to content

Commit 083d6d5

Browse files
committed
improve main class docstring
1 parent 7c91186 commit 083d6d5

File tree

1 file changed

+147
-10
lines changed

1 file changed

+147
-10
lines changed

DynamicVector/DynamicVector.py

Lines changed: 147 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,157 @@
3737
class DynamicVector:
3838
"""
3939
A dynamic vector implementation using NumPy arrays, with dynamic resizing capabilities.
40-
The vector grows in capacity using powers of two until it exceeds a certain threshold,
41-
after which it grows by a fixed amount.
40+
The dynamic vector includes fast appending and popping, like a list,
41+
while retaining numpy array index support and vector operations.
42+
43+
The dynamic vector supports most of the python list and numpy.ndarray methods.
44+
45+
The storage of the vector automatically grows in capacity by doubling it
46+
until the capacity exceeds a certain threshold (`grow_use_add`),
47+
after which it grows by a fixed amount (`grow_add`).
48+
49+
For example, given a dynamic vector that has an initial capacity of 8
50+
and contains seven values (size=7). If another value is appended (size=8),
51+
then the capacity is increased to 16 (from 2*8) to hold the extra value.
52+
53+
Args:
54+
dtype (np.dtype, optional): numpy.dtype (data type) of the vector elements. Defaults to np.int32.
55+
capacity (int, optional): Initial capacity of the vector. Defaults to 8.
56+
grow_use_add (int, optional): Threshold to switch from multiplicative to additive growth. Defaults to 8192.
57+
grow_add (int, optional): Additive increment in additive growth mode. Defaults to 2048.
4258
4359
Attributes:
44-
_size (int): The current number of elements in the vector.
45-
_cap (int): The current capacity of the underlying array.
46-
_data (np.ndarray): The underlying NumPy array that stores the elements.
47-
_dtype (np.dtype): The data type of the array elements.
60+
size (int): The size of the dynamic vector.
61+
view (np.ndarray): A np.ndarray view of the dynamic vector.
62+
dtype (np.dtype): The numpy.dtype (data type) of the vector.
63+
64+
capacity (int): The capacity of the underlying vector storage.
65+
grow_use_add (int): Threshold that growth switches from multiplicative to additive growth.
66+
grow_add (int): Additive increment in additive growth mode.
67+
68+
Constructors:
69+
from_values(values): Create a DynamicVector from an iterable of values.
70+
71+
from_iter(iterator): Create a DynamicVector from an iterator.
72+
73+
Methods:
74+
75+
append(value):
76+
Append a value to the end of the vector.
77+
78+
extend(values):
79+
Append multiple values to the vector.
80+
81+
insert(index, value):
82+
Insert a value at a specified index.
83+
84+
insert_values(index, values):
85+
Insert multiple values at a specified index.
86+
87+
pop(index=-1):
88+
Remove and return an element at a specified index (default is the last).
89+
90+
remove(value, remove_all=False, from_right=False):
91+
Remove one or more occurrences of a value.
92+
93+
drop(index=-1):
94+
Remove an element at a specified index.
95+
96+
count(value):
97+
Count occurrences of a value.
98+
99+
sort(reverse=False, kind=None):
100+
Sort the vector in ascending or descending order.
101+
102+
reverse():
103+
Reverse the order of elements in the vector.
104+
105+
contains(value):
106+
Check if a value exists in the vector.
107+
108+
copy(min_capacity=8):
109+
Create a copy of the vector.
110+
111+
clear():
112+
Remove all elements from the vector.
113+
114+
abs(where=True):
115+
Compute the absolute value of all elements.
116+
117+
where(value):
118+
Get the indices of elements equal to a value.
119+
120+
index(value, from_right=False):
121+
Get the index of a value in the vector.
122+
123+
resize(size):
124+
Resize the vector to a specified size.
125+
126+
increase_size(increase_by):
127+
Increase the size of the vector by a specified amount.
128+
129+
set_capacity(min_capacity):
130+
Ensure the vector's capacity is at least a given value.
131+
132+
force_capacity(min_capacity):
133+
Set the capacity to the smallest power of two exceeding min_capacity.
134+
135+
is_equal(other):
136+
Check if all elements are equal to those in another vector or value.
137+
138+
is_less(other):
139+
Check if all elements are less than those in another vector or value.
140+
141+
is_greater(other):
142+
Check if all elements are greater than those in another vector or value.
143+
144+
is_less_or_equal(other):
145+
Check if all elements are less than or equal to those in another vector or value.
146+
147+
is_greater_or_equal(other):
148+
Check if all elements are greater than or equal to those in another vector or value.
149+
150+
is_not_equal(other):
151+
Check if all elements are not equal to those in another vector or value.
152+
153+
154+
Example usage:
155+
>>>
156+
>>> from DynamicVector import DynamicVector
157+
>>>
158+
>>> vec = DynamicVector(dtype=np.int32, capacity=4)
159+
>>>
160+
>>> vec.append(10)
161+
>>> vec.append(20)
162+
>>>
163+
>>> vec.extend([30, 40, 50])
164+
>>>
165+
>>> print(vec)
166+
DynamicVector([10, 20, 30, 40, 50])
167+
>>>
168+
>>> print(vec[2]) # vec[2] returns np.int32(20)
169+
30
170+
>>> print(vec[1:4]) # vec[1:4] returns a np.ndarray view of vector
171+
[20 30 40]
172+
>>>
173+
>>> vec.sort() # inplace sort
174+
>>>
175+
>>> print(f"Size: {vec.size}, Capacity: {vec.capacity}")
176+
Size: 5, Capacity: 8
177+
>>>
178+
>>> vec.pop() # Remove the last element
179+
>>>
180+
>>> vec.clear() # Clear the vector (vec.size = 0)
181+
182+
Notes:
183+
- The vector automatically resizes when needed, and it uses multiplicative growth until a specified threshold.
184+
- After reaching the threshold, the growth becomes additive.
48185
"""
49186

50-
_size: int
51-
_cap: int
52-
_data: np.ndarray
53-
_dtype: np.dtype
187+
_size: int # The current number of elements in the vector.
188+
_cap: int # The current capacity of the underlying np.ndarray.
189+
_data: np.ndarray # The underlying NumPy array that stores the elements.
190+
_dtype: np.dtype # The data type of the array elements.
54191

55192
def __init__(self, dtype=np.int32, capacity=8, *, grow_use_add=None, grow_add=None):
56193
"""

0 commit comments

Comments
 (0)