numbox.core.vector
Overview
Generic growable numba vector backed by a numpy array.
Compared to numba.typed.List:
Listsupports arbitrary element types (including other structrefs) and exposes a richer API (append,pop,insert,remove, slicing).Vectoris restricted to scalar element types wherestr(elem_type)matches a numpy dtype (float64,int64, etc.).make_vectormemoises instances byelem_type.key, so cached code keeps the same type identity across processes. Storage is a singlenumpy.ndarray, so per-element overhead is the scalar itself plus amortised geometric growth.
Modules
numbox.core.vector.vector
A growable vector of one numba scalar type for jitted code, stored in a NumPy array.
make_vector() returns a jitted factory of vectors of one element type, and their numba type. In jitted code,
len(v), v[i] and v[i] = x work on a vector, vector_push() appends an element and
vector_extend() appends the elements of another vector. If the elements to append do not fit, the buffer is
replaced by one whose capacity is the old capacity doubled until they fit.
- class numbox.core.vector.vector.Vector(*args, **kwargs)[source]
Bases:
StructRefProxyThe Python object for a vector of one numba scalar type, whose buffer grows as elements are appended.
Create one with the
createfunction thatmake_vector()returns. Calling the class raisesNotImplementedErrorin Python, and jitted code that calls it fails to compile.- property buf
The NumPy array that holds the elements.
Its length is the capacity of the vector, and only its first
sizeelements are set.
- property size
The number of elements in the vector.
- class numbox.core.vector.vector.VectorTypeClass(*args, **kwargs)[source]
Bases:
StructRefThe numba type class of
Vector.Each element type has an instance of its own, a structref type with the fields
bufandsize.
- numbox.core.vector.vector.make_vector(elem_type)[source]
Return
(create, vector_type): a jitted factory of vectors ofelem_type, and their numba type.create(capacity)returns an empty vector whose buffer holdscapacityelements. It takes no buffer, so the buffer’s dtype always follows fromelem_type. A capacity below 1 fails anassertincreate. A buffer of capacity 0 would never grow, because each growth doubles it:vector_extendwould loop forever, andvector_pushwould write past the end of the buffer. A vector grows and never shrinks, so a positive capacity stays positive.The buffer’s dtype is
numpy.dtype(str(elem_type)), soelem_typemust be a scalar type whose name NumPy accepts, such asfloat64orint64. For another type, such asunicode_type, NumPy raisesTypeError. The pair is stored in_vector_cacheunderelem_type.key, and a later call with the same key returns it.