akreon-linalg
Fixed-size linear algebra and numerical methods in C++26.
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1/*
2 * Copyright © 2025-2026 Akreon LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
9
10#include <array>
11#include <ranges>
12#include <span>
13#include <type_traits>
14
15#include "algebra_util.hpp"
16
17namespace linalg {
18
35template <typename T, std::size_t N>
36struct Vector {
42 template <typename... Coords>
43 constexpr Vector(Coords... coords)
44 requires(sizeof...(coords) == N)
45 : coordinates{coords...} {}
46
48 constexpr Vector() : coordinates{} {}
49
51 constexpr auto begin() const { return coordinates.begin(); }
52
54 constexpr auto end() const { return coordinates.end(); }
55
61 constexpr const T& operator()(std::size_t idx) const {
62 AK_ASSERT(idx < N && "Vector const access index out of bounds");
63 return coordinates[idx];
64 }
65
71 constexpr T& operator()(std::size_t idx) {
72 AK_ASSERT(idx < N && "Vector mutable access index out of bounds");
73 return coordinates[idx];
74 }
75
80 constexpr bool operator==(const Vector& other) const {
81 return is_equal(other);
82 }
83
88 constexpr bool operator!=(const Vector& other) const {
89 return !is_equal(other);
90 }
91
95 constexpr auto view() { return std::span<T, N>{coordinates}; }
96
98 constexpr auto size() const { return N; }
99
100private:
102 constexpr bool is_equal(const Vector& other) const {
103 for (std::size_t i = 0; i < N; ++i)
104 if (other.coordinates[i] != coordinates[i]) return false;
105 return true;
106 }
107
108 std::array<T, N> coordinates;
109};
110
118template <typename T, typename... Ts>
119Vector(T, Ts...) -> Vector<std::common_type_t<T, Ts...>, 1 + sizeof...(Ts)>;
120
121} // namespace linalg
#define AK_ASSERT(x)
Definition assert.hpp:13
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
Owning, fixed-size vector container.
Definition vector.hpp:36
constexpr auto begin() const
Const iterator to the first coordinate.
Definition vector.hpp:51
constexpr auto end() const
Const iterator one past the final coordinate.
Definition vector.hpp:54
constexpr Vector()
Construct a value-initialized vector.
Definition vector.hpp:48
constexpr auto view()
Obtain a mutable fixed-extent view of contiguous coordinate storage.
Definition vector.hpp:95
constexpr T & operator()(std::size_t idx)
Read or modify a coordinate at a zero-based index.
Definition vector.hpp:71
constexpr bool operator==(const Vector &other) const
Compare coordinates for exact element-wise equality.
Definition vector.hpp:80
constexpr bool operator!=(const Vector &other) const
Compare coordinates for element-wise inequality.
Definition vector.hpp:88
constexpr auto size() const
Return the compile-time coordinate count N.
Definition vector.hpp:98
constexpr const T & operator()(std::size_t idx) const
Read a coordinate at a zero-based index.
Definition vector.hpp:61
constexpr Vector(Coords... coords)
Construct from exactly N coordinate values.
Definition vector.hpp:43