|
Clustering
C++20 header-only: DBSCAN, HDBSCAN, k-means.
|
Represents a multidimensional array (NDArray) of a fixed number of dimensions N and element type T. More...
#include <clustering/ndarray.h>
Classes | |
| class | BaseAccessor |
| Inner class providing base functionality for element access in NDArray. More... | |
| class | ConstAccessor |
| Provides read-only access to NDArray elements. More... | |
| class | Accessor |
| Provides read-write access to NDArray elements. More... | |
Public Member Functions | |
| template<Layout L2 = L> | |
| NDArray (std::initializer_list< std::size_t > dims) | |
| Constructs a contiguous owned NDArray with specified dimensions. | |
| template<Layout L2 = L> | |
| NDArray (std::array< std::size_t, N > shape) | |
Constructs a contiguous owned NDArray from a runtime std::array of dimensions. | |
| NDArray (const NDArray &other) | |
Copy constructor; re-seats m_data against m_vec for owned storage. | |
| NDArray (NDArray &&other) noexcept | |
Move constructor; steals m_vec and re-seats m_data for owned storage. | |
| NDArray & | operator= (const NDArray &other) |
Copy assignment; re-seats m_data against m_vec for owned storage. | |
| NDArray & | operator= (NDArray &&other) noexcept |
Move assignment; steals m_vec and re-seats m_data for owned storage. | |
| template<Layout L2 = L> | |
| Accessor | operator[] (std::size_t index) noexcept |
| Provides access to the elements of the NDArray. | |
| template<Layout L2 = L> | |
| ConstAccessor | operator[] (std::size_t index) const noexcept |
| Provides read-only access to the elements of the NDArray. | |
| template<class... Ix> | |
| T & | operator() (Ix... ix) noexcept |
| Direct multi-index element access via strides. | |
| template<class... Ix> | |
| const T & | operator() (Ix... ix) const noexcept |
| Read-only multi-index element access via strides; mirrors the mutable overload. | |
| T & | flatIndex (std::size_t index) noexcept |
| Provides direct access to the flat underlying array at a specific index. | |
| const T & | flatIndex (std::size_t index) const noexcept |
| Provides read-only access to the flat underlying array at a specific index. | |
| size_t | dim (std::size_t index) const noexcept |
| Returns the size of a specific dimension of the NDArray. | |
| std::ptrdiff_t | strideAt (std::size_t index) const noexcept |
Returns the stride (in elements) for dimension index. | |
| bool | isContiguous () const noexcept |
| Reports whether the array's runtime layout is row-major contiguous with zero offset. | |
| bool | isMutable () const noexcept |
Reports whether writes through operator(), Accessor, or flatIndex are allowed. | |
| bool | isOwned () const noexcept |
| Reports whether the array owns its underlying buffer. | |
| const T * | data () const noexcept |
| Provides read-only access to the internal data array. | |
| T * | data () noexcept |
| Provides read-write access to the internal data array. | |
| T * | baseData () const noexcept |
| Returns the original (non-advanced) base pointer for storage-identity comparisons. | |
| template<std::size_t A> | |
| bool | isAligned () const noexcept |
Tests whether data() is aligned to A bytes. | |
| template<std::size_t A> | |
| T * | alignedData () noexcept |
Returns data() with an alignment hint of A bytes applied. | |
| template<std::size_t A> | |
| const T * | alignedData () const noexcept |
| Read-only overload of alignedData<A>; attaches the same alignment hint to the pointer. | |
| template<std::size_t M = N> | |
| NDArray< T, 2, Layout::MaybeStrided > | t () noexcept |
| Transposes a rank-2 NDArray into a borrowed view with swapped axes. | |
| template<std::size_t M = N> | |
| NDArray< T, 2, Layout::MaybeStrided > | t () const noexcept |
Read-only transpose; the returned view carries m_mutable = false. | |
| template<std::size_t M = N> | |
| NDArray< T, N - 1, L > | row (std::size_t i) noexcept |
Returns a borrowed view of row i with the leading dimension dropped. | |
| template<std::size_t M = N> | |
| NDArray< T, N - 1, L > | row (std::size_t i) const noexcept |
Read-only row view; mirrors the mutable overload and flips m_mutable off. | |
| template<std::size_t M = N> | |
| NDArray< T, 1, Layout::MaybeStrided > | col (std::size_t j) noexcept |
Returns a borrowed rank-1 view of column j of a rank-2 array. | |
| template<std::size_t M = N> | |
| NDArray< T, 1, Layout::MaybeStrided > | col (std::size_t j) const noexcept |
Read-only column view; mirrors the mutable overload and flips m_mutable off. | |
| NDArray< T, N, Layout::MaybeStrided > | slice (std::size_t axis, std::size_t begin, std::size_t end) noexcept |
| Borrowed half-open slice along a single axis. | |
| NDArray< T, N, Layout::MaybeStrided > | slice (std::size_t axis, std::size_t begin, std::size_t end) const noexcept |
Read-only single-axis slice; mirrors the mutable overload with m_mutable = false. | |
| NDArray< T, N, Layout::MaybeStrided > | slice (const std::array< Range, N > &ranges) noexcept |
Borrowed multi-axis slice; each Range applies to its corresponding axis. | |
| NDArray< T, N, Layout::MaybeStrided > | slice (const std::array< Range, N > &ranges) const noexcept |
Read-only multi-axis slice; mirrors the mutable overload with m_mutable = false. | |
| NDArray< T, N, Layout::MaybeStrided > | permute (const std::array< std::size_t, N > &perm) noexcept |
Borrowed view with axes reordered by perm. | |
| NDArray< T, N, Layout::MaybeStrided > | permute (const std::array< std::size_t, N > &perm) const noexcept |
Read-only permuted view; mirrors the mutable overload with m_mutable = false. | |
| template<std::size_t M> | |
| NDArray< T, M, Layout::Contig > | view (std::array< std::size_t, M > shape) noexcept |
Returns a borrowed contiguous rank-M view over the same buffer with shape shape. | |
| template<std::size_t M> | |
| NDArray< T, M, Layout::Contig > | view (std::array< std::size_t, M > shape) const noexcept |
Read-only rank-M view; mirrors the mutable overload with m_mutable = false. | |
| template<std::size_t M> | |
| NDArray< T, M, Layout::Contig > | reshape (std::array< std::size_t, M > shape) |
Returns a contiguous rank-M array with shape shape, copying only when needed. | |
| template<std::size_t M> | |
| NDArray< T, M, Layout::Contig > | reshape (std::array< std::size_t, M > shape) const |
Read-only rank-M reshape; aliases on contiguous sources, copies otherwise. | |
| NDArray< T, N, Layout::Contig > | contiguous () |
Returns a contiguous rank-N array with the same shape, copying only when needed. | |
| NDArray< T, N, Layout::Contig > | contiguous () const |
| Read-only contiguous view; aliases on contiguous sources, copies otherwise. | |
| NDArray< T, N, Layout::Contig > | clone () const |
| Returns a freshly-allocated owned contiguous array with deep-copied contents. | |
| std::string | debugDump () const |
| Returns a formatted string representing the contents of the NDArray. | |
Static Public Member Functions | |
| template<Layout L2 = L> | |
| static NDArray | borrow (T *ptr, std::array< std::size_t, N > shape) noexcept |
| Borrows a contiguous buffer as an NDArray without taking ownership. | |
| template<Layout L2 = L> | |
| static NDArray | borrow (const T *ptr, std::array< std::size_t, N > shape) noexcept |
| Borrows a read-only contiguous buffer as an NDArray. | |
| template<Layout L2 = L> | |
| static NDArray | borrow (T *ptr, std::array< std::size_t, N > shape, std::array< std::ptrdiff_t, N > strides) noexcept |
| Borrows a strided buffer as an NDArray without taking ownership. | |
| template<Layout L2 = L> | |
| static NDArray | borrow (const T *ptr, std::array< std::size_t, N > shape, std::array< std::ptrdiff_t, N > strides) noexcept |
Read-only strided borrow; flips m_mutable off so writes through the view assert. | |
| template<std::size_t M = N> | |
| static NDArray | borrow1D (T *ptr, std::size_t n) noexcept |
Rank-1 convenience borrow; avoids the std::array<size_t, 1>{n} boilerplate. | |
| template<std::size_t M = N> | |
| static NDArray | borrow1D (const T *ptr, std::size_t n) noexcept |
Read-only rank-1 convenience borrow; mirrors the mutable borrow1D. | |
| template<Layout L2 = L> | |
| static NDArray | borrowBytes (T *ptr, std::array< std::size_t, N > shape, std::array< std::ptrdiff_t, N > stridesInBytes, bool isMutable) noexcept |
| Borrow a buffer whose strides are expressed in bytes (NumPy's convention). | |
| template<std::size_t M = N> | |
| static NDArray | fromSpan (std::span< T > s) noexcept |
Explicit std::span adapter for rank-1 borrows. | |
| template<std::size_t M = N> | |
| static NDArray | fromSpan (std::span< const T > s) noexcept |
Read-only span adapter; delegates to the read-only borrow overload. | |
Friends | |
| template<class U, std::size_t M, Layout LL> | |
| class | NDArray |
| bool | operator== (const NDArray &, const NDArray &)=delete |
| bool | operator!= (const NDArray &, const NDArray &)=delete |
Represents a multidimensional array (NDArray) of a fixed number of dimensions N and element type T.
NDArray is a template class that provides a high-level representation of a multi-dimensional array. It offers element access, dimension information, and debug utilities. The array's dimensions are defined at compile time for type safety and efficiency.
| T | The type of elements stored in the NDArray. |
| N | The number of dimensions of the NDArray. |
| L | Layout tag; Layout::Contig (default) enables the operator[] chain. |
|
inline |
Constructs a contiguous owned NDArray with specified dimensions.
Only available for Layout::Contig; the MaybeStrided instantiation has no default constructor and is produced exclusively by strided-view factory methods.
| dims | Initializer list specifying the dimensions of the NDArray. |
|
inlineexplicit |
Constructs a contiguous owned NDArray from a runtime std::array of dimensions.
Mirrors the initializer-list constructor but accepts a shape already materialized as a std::array, the form consumed by reshape, contiguous, and clone when they need to allocate a fresh buffer.
| shape | Dimensions of the NDArray, one entry per axis. |
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
Read-only overload of alignedData<A>; attaches the same alignment hint to the pointer.
|
inlinenoexcept |
Returns data() with an alignment hint of A bytes applied.
Calls __builtin_assume_aligned so downstream SIMD intrinsics (e.g. _mm256_load_ps) can assume an A -byte aligned pointer and emit aligned-load instructions under -O2. The debug assert guards against feeding an unaligned borrow into this path.
| A | Required alignment in bytes. |
|
inlinenodiscardnoexcept |
Returns the original (non-advanced) base pointer for storage-identity comparisons.
All view verbs propagate the source's base pointer unchanged; only a fresh owned allocation (or the clone / non-contiguous reshape paths) installs a new base. sameStorage uses this to decide whether two arrays share the underlying buffer.
|
inlinestaticnoexcept |
|
inlinestaticnoexcept |
|
inlinestaticnoexcept |
Borrows a contiguous buffer as an NDArray without taking ownership.
Available only when L == Layout::Contig. The returned array shares the caller's buffer, carries contiguous strides, and is writable through operator() and Accessor.
| ptr | Non-owning pointer to the first element. Must stay alive for the view's lifetime. |
| shape | Dimensions, one entry per axis. |
|
inlinestaticnoexcept |
|
inlinestaticnoexcept |
|
inlinestaticnoexcept |
|
inlinestaticnoexcept |
Borrow a buffer whose strides are expressed in bytes (NumPy's convention).
Byte strides are divided by sizeof(T) to recover element strides; non-divisible entries are undefined and asserted in debug. The result always carries Layout::MaybeStrided so arbitrary byte strides can be represented without a runtime contiguity check gating the Contig type-level invariant.
| ptr | Non-owning pointer to the first element. |
| shape | Dimensions, one entry per axis. |
| stridesInBytes | Byte offset between successive elements along each axis. |
| isMutable | Whether writes through the view are permitted. |
|
inline |
Returns a freshly-allocated owned contiguous array with deep-copied contents.
Always allocates; unlike contiguous, never aliases the source buffer. Equivalent to NumPy's ndarray.copy: the caller receives an independent owner with matching values and row-major layout.
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
Returns a contiguous rank-N array with the same shape, copying only when needed.
Already-contiguous sources are aliased into a borrowed view sharing storage; strided sources allocate a dense owned copy. The return type drops the MaybeStrided tag so downstream hot paths can resume the operator[] chain.
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
Provides read-write access to the internal data array.
Asserts in debug that the array is mutable: a raw pointer grabbed from a read-only borrow would otherwise corrupt the source buffer without tripping any other write-path guard.
|
inline |
Returns a formatted string representing the contents of the NDArray.
This method is primarily used for debugging purposes, providing a visual representation of the array's structure and contents. Walks the array through m_data, m_offset, and m_strides so Borrowed views report their actual viewable contents rather than the empty m_vec.
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinestaticnoexcept |
|
inlinestaticnoexcept |
|
inlinenoexcept |
|
inlinenodiscardnoexcept |
Reports whether the array's runtime layout is row-major contiguous with zero offset.
For Layout::Contig arrays this is always true (the type encodes the guarantee). For Layout::MaybeStrided arrays the answer is computed from m_strides and m_offset.
|
inlinenodiscardnoexcept |
|
inlinenodiscardnoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Direct multi-index element access via strides.
Available for all layouts.
| Ix | Integral index pack, must have exactly N elements. |
| ix | Indices, one per dimension. |
m_offset + sum_k ix_k * m_strides[k].
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
Provides read-only access to the elements of the NDArray.
| index | Index in the first dimension. |
|
inlinenoexcept |
Provides access to the elements of the NDArray.
Only defined for Layout::Contig. A MaybeStrided array has no operator[]; use the variadic operator()(i, j, ...) to read through strides.
| index | Index in the first dimension. |
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
Returns a contiguous rank-M array with shape shape, copying only when needed.
Aliases the existing buffer when the source is runtime-contiguous; allocates an owned dense copy otherwise. Callers that require no-hidden-allocation guarantees should use view instead, which asserts on non-contiguous input without the allocation fallback.
| M | Target rank. May differ from N. |
| shape | Dimensions of the result, one entry per axis. |
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Borrowed half-open slice along a single axis.
Returned view always carries MaybeStrided; callers that need the Contig guarantee back (e.g. axis-0 slice of a contiguous source) can round-trip through contiguous().
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Transposes a rank-2 NDArray into a borrowed view with swapped axes.
The returned view reuses the source buffer; sameStorage returns true against the source. Result is always Layout::MaybeStrided because transposition breaks row-major contiguity for any contiguous source with more than one column.
|
inlinenoexcept |
|
inlinenoexcept |
Returns a borrowed contiguous rank-M view over the same buffer with shape shape.
Strict no-alloc primitive: the source must be runtime-contiguous (debug-asserted). Callers that want the copy-on-nonconfig fallback should use reshape instead. The element count of shape must equal the source's element count.
| M | Target rank. May differ from N. |
| shape | Dimensions of the returned view, one entry per axis. |
|
friend |
|
friend |