Vector
Module
Defines several vector-like classes.
DynamicVector
The DynamicVector
type is a dynamically-allocated vector.
It supports pushing and popping from the back resizing the underlying storage as needed. When it is deallocated, it frees its memory.
TODO: It should call its element destructors once we have traits. TODO: It should perform bound checks.
Parameters:
- type (
AnyType
): The type of the elements.
Fields:
capacity
The amount of elements that can fit in the vector without resizing it.
data
The underlying storage for the vector.
size
The number of elements in the vector.
Functions:
__init__
__init__(self: Self&)
Construct an empty vector.
__init__(self: Self&, capacity: Int)
Construct a vector with the given capacity.
Args:
- capacity (
Int
): The requested capacity of the vector.
__init__(self: Self&, pointer: Pointer[type], size: Int)
Construct a vector with the given pointer and size.
Args:
- pointer (
Pointer[type]
): The pointer to the buffer. - size (
Int
): The size of the buffer.
__copyinit__
__copyinit__(self: Self&, existing: Self)
Copy constructor.
This doesn’t copy the data, i.e. the copy is shallow.
Args:
- existing (
Self
): TheDynamicVector
to copy.
__getitem__
__getitem__(self: Self, i: Int) -> type
Get a vector element at the given index.
Args:
- i (
Int
): The index of the element.
Returns:
The element at the given index.
__setitem__
__setitem__(self: Self&, i: Int, value: type)
Set a vector element at the given index.
Args:
- i (
Int
): The index of the element. - value (
type
): The value to assign.
__len__
__len__(self: Self) -> Int
Get the number of elements in the vector.
Returns:
The number of elements in the vector.
deepcopy
deepcopy(self: Self) -> Self
Create a deepcopy of this vector.
Returns:
The created copy of this vector.
pop_back
pop_back(self: Self&) -> type
Pop a value from the back of this vector.
Returns:
The popped value.
push_back
push_back(self: Self&, value: type)
Append a value to this vector.
Args:
- value (
type
): The value to append.
reserve
reserve(self: Self&, new_capacity: Int)
Reserve the requested capacity.
If the current capacity is greater or equal, this is a no-op. Otherwise, the storage is reallocated and the date is moved.
Args:
- new_capacity (
Int
): The new capacity.
resize
resize(self: Self&, size: Int)
Resize the vector to the given new size.
If the new size is smaller than the current one, elements at the end are discarded. If the new size is larger than the current one, the vector is appended with non-initialized elements up to the requested size.
Args:
- size (
Int
): The new size.
InlinedFixedVector
The InlinedFixedVector
type is a dynamically-allocated vector with small- vector optimization.
The InlinedFixedVector
does not resize or implement bounds checks, it is initialized with both a small-vector size (statically known) and a dynamic (not known at compile time) number of slots, and when it is deallocated, it frees its memory.
TODO: It should call its element destructors once we have traits.
This data structure is useful for applications where the number of required elements is not known at compile time, but once known at runtime, is guaranteed to be equal to or less than a certain capacity.
Parameters:
- size (
Int
): The statically know-small vector size. - type (
AnyType
): The type of the elements.
Aliases:
static_data_type = StaticTuple[size, type]
static_size = size
Fields:
capacity
The amount of elements that can fit in the vector without resizing it.
current_size
The number of elements in the vector.
dynamic_data
The underlying dynamic storage, used to grow large vectors.
static_data
The underlying static storage, used for small vectors.
Functions:
__init__
__init__(self: Self&, capacity: Int)
Construct InlinedFixedVector
with the given capacity.
The dynamically allocated portion is capacity - size
.
Args:
- capacity (
Int
): The requested capacity of the vector.
__copyinit__
__copyinit__(self: Self&, existing: Self)
Copy constructor.
This doesn’t copy the data, i.e. the copy is shallow.
Args:
- existing (
Self
): TheInlinedFixedVector
to copy.
__getitem__
__getitem__(self: Self, i: Int) -> type
Get a vector element at the given index.
Args:
- i (
Int
): The index of the element.
Returns:
The element at the given index.
__setitem__
__setitem__(self: Self&, i: Int, value: type)
Set a vector element at the given index.
Args:
- i (
Int
): The index of the element. - value (
type
): The value to assign.
__len__
__len__(self: Self) -> Int
Get the number of elements in the vector.
Returns:
The number of elements in the vector.
append
append(self: Self&, value: type)
Append a value to this vector.
Args:
- value (
type
): The value to append.
deepcopy
deepcopy(self: Self) -> Self
Create a deepcopy of this vector.
Returns:
The created copy of this vector.
UnsafeFixedVector
The UnsafeFixedVector
type is a dynamically-allocated vector that does not resize or implement bounds checks.
It is initialized with a dynamic (not known at compile time) number of slots, and when it is deallocated, it frees its memory.
TODO: It should call its element destructors once we have traits.
This data structure is useful for applications where the number of required elements is not known at compile time, but once known at runtime, is guaranteed to be equal to or less than a certain capacity.
Parameters:
- type (
AnyType
): The type of the elements.
Fields:
capacity
The amount of elements that can fit in the vector.
data
The underlying storage for the vector.
size
The number of elements in the vector.
Functions:
__init__
__init__(self: Self&, capacity: Int)
Construct UnsafeFixedVector
with the given capacity.
Args:
- capacity (
Int
): The requested capacity of the vector.
__copyinit__
__copyinit__(self: Self&, existing: Self)
Copy constructor.
This doesn’t copy the data, i.e. the copy is shallow.
Args:
- existing (
Self
): TheUnsafeFixedVector
to copy.
__getitem__
__getitem__(self: Self, i: Int) -> type
Get a vector element at the given index.
Args:
- i (
Int
): The index of the element.
Returns:
The element at the given index.
__setitem__
__setitem__(self: Self, i: Int, value: type)
Set a vector element at the given index.
Args:
- i (
Int
): The index of the element. - value (
type
): The value to assign.
__len__
__len__(self: Self) -> Int
Get the number of elements in the vector.
Returns:
The number of elements in the vector.
append
append(self: Self&, value: type)
Append a value to this vector.
Args:
- value (
type
): The value to append.