numbox.core.vector

Overview

Generic growable numba vector backed by a numpy array.

Compared to numba.typed.List:

  • List supports arbitrary element types (including other structrefs) and exposes a richer API (append, pop, insert, remove, slicing).

  • Vector is restricted to scalar element types where str(elem_type) matches a numpy dtype (float64, int64, etc.). make_vector memoises instances by elem_type.key, so cached code keeps the same type identity across processes. Storage is a single numpy.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: StructRefProxy

The Python object for a vector of one numba scalar type, whose buffer grows as elements are appended.

Create one with the create function that make_vector() returns. Calling the class raises NotImplementedError in 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 size elements are set.

property size

The number of elements in the vector.

class numbox.core.vector.vector.VectorTypeClass(*args, **kwargs)[source]

Bases: StructRef

The numba type class of Vector.

Each element type has an instance of its own, a structref type with the fields buf and size.

preprocess_fields(fields)[source]

Return fields with each literal field type replaced by its non-literal type.

numbox.core.vector.vector.make_vector(elem_type)[source]

Return (create, vector_type): a jitted factory of vectors of elem_type, and their numba type.

create(capacity) returns an empty vector whose buffer holds capacity elements. It takes no buffer, so the buffer’s dtype always follows from elem_type. A capacity below 1 fails an assert in create. A buffer of capacity 0 would never grow, because each growth doubles it: vector_extend would loop forever, and vector_push would 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)), so elem_type must be a scalar type whose name NumPy accepts, such as float64 or int64. For another type, such as unicode_type, NumPy raises TypeError. The pair is stored in _vector_cache under elem_type.key, and a later call with the same key returns it.

numbox.core.vector.vector.vector_extend(dst, src)[source]

Append the elements of the vector src to the vector dst, doubling the capacity of dst until they fit.

numba converts the elements to the element type of dst as it does for an array assignment.

numbox.core.vector.vector.vector_push(v, val)[source]

Append val to the vector v, doubling the buffer’s capacity first if the buffer is full.

numba converts val to the element type as it does for an array assignment, so a float appended to an integer vector is truncated.