DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
vector3D.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: vector3D.hpp
5// Author: crdrisko
6// Date: 04/07/2020-11:08:41
7// Description: A vector representing a quantity with x, y, and z components
8
9#ifndef DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_MATH_CONTAINERS_VECTOR3D_HPP
10#define DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_MATH_CONTAINERS_VECTOR3D_HPP
11
12#include <array>
13#include <cstddef>
14#include <type_traits>
15#include <utility>
16
18
19namespace CppUtils::Math
20{
32 template<typename T, typename = std::enable_if_t<std::is_default_constructible_v<T>>>
33 class Vector3D : private Operators::CompletelyComparable<Vector3D<T>>
34 {
35 public:
37 using value_type = T;
38 using size_type = std::size_t;
39 using difference_type = std::ptrdiff_t;
40 using reference = typename std::array<T, 3>::reference;
41 using const_reference = typename std::array<T, 3>::const_reference;
42 using pointer = typename std::array<T, 3>::pointer;
43 using const_pointer = typename std::array<T, 3>::const_pointer;
44 using iterator = typename std::array<T, 3>::iterator;
45 using const_iterator = typename std::array<T, 3>::const_iterator;
46
48 using container_type = std::array<T, 3>;
49
50 private:
51 std::array<T, 3> data {};
52
53 public:
55 constexpr Vector3D() noexcept = default;
56 constexpr Vector3D(T x_, T y_, T z_) noexcept : data {{x_, y_, z_}} {}
57 constexpr explicit Vector3D(const container_type& data_) noexcept : data {data_} {}
58
60 constexpr friend bool operator==(const Vector3D<T>& lhs_, const Vector3D<T>& rhs_) { return lhs_.data == rhs_.data; }
61 constexpr friend bool operator<(const Vector3D<T>& lhs_, const Vector3D<T>& rhs_) { return lhs_.data < rhs_.data; }
62
64 constexpr reference at(size_type pos_) { return data.at(pos_); }
65 constexpr const_reference at(size_type pos_) const { return data.at(pos_); }
66
67 constexpr reference operator[](size_type pos_) noexcept { return data[pos_]; }
68 constexpr const_reference operator[](size_type pos_) const noexcept { return data[pos_]; }
69
71 constexpr iterator begin() noexcept { return data.begin(); }
72 constexpr const_iterator begin() const noexcept { return data.begin(); }
73 constexpr const_iterator cbegin() const noexcept { return data.cbegin(); }
74
75 constexpr iterator end() noexcept { return data.end(); }
76 constexpr const_iterator end() const noexcept { return data.end(); }
77 constexpr const_iterator cend() const noexcept { return data.cend(); }
78
80 constexpr bool empty() const noexcept { return false; }
81 constexpr size_type size() const noexcept { return 3; }
82 constexpr size_type max_size() const noexcept { return 3; }
83
85 constexpr void fill(const_reference value_) { data.fill(value_); }
86 constexpr void swap(Vector3D<T>& other_) { data.swap(other_.data); }
87 };
88
92 template<std::size_t Index, typename T>
93 decltype(auto) get(Vector3D<T>& vec)
94 {
95 static_assert(Index < 3, "Index must be within 0 and 2, inclusive.");
96
97 if constexpr (Index == 0)
98 return vec[0];
99 else if constexpr (Index == 1)
100 return vec[1];
101 else
102 return vec[2];
103 }
104
108 template<std::size_t Index, typename T>
109 decltype(auto) get(const Vector3D<T>& vec)
110 {
111 static_assert(Index < 3, "Index must be within 0 and 2, inclusive.");
112
113 if constexpr (Index == 0)
114 return vec[0];
115 else if constexpr (Index == 1)
116 return vec[1];
117 else
118 return vec[2];
119 }
120
124 template<std::size_t Index, typename T>
125 decltype(auto) get(Vector3D<T>&& vec)
126 {
127 static_assert(Index < 3, "Index must be within 0 and 2, inclusive.");
128
129 if constexpr (Index == 0)
130 return std::move(vec[0]);
131 else if constexpr (Index == 1)
132 return std::move(vec[1]);
133 else
134 return std::move(vec[2]);
135 }
136} // namespace CppUtils::Math
137
138
140template<typename T>
141struct std::tuple_size<CppUtils::Math::Vector3D<T>>
142{
143 static constexpr int value = 3;
144};
145
146template<std::size_t Index, typename T>
147struct std::tuple_element<Index, CppUtils::Math::Vector3D<T>>
148{
149 using type = T;
150};
151
152#endif
Definition vector3D.hpp:34
constexpr iterator begin() noexcept
Iterators - reuse the definitions from the internal array.
Definition vector3D.hpp:71
constexpr void swap(Vector3D< T > &other_)
Definition vector3D.hpp:86
constexpr const_iterator cend() const noexcept
Definition vector3D.hpp:77
std::size_t size_type
Definition vector3D.hpp:38
std::ptrdiff_t difference_type
Definition vector3D.hpp:39
constexpr const_reference operator[](size_type pos_) const noexcept
Definition vector3D.hpp:68
typename std::array< T, 3 >::reference reference
Definition vector3D.hpp:40
constexpr const_reference at(size_type pos_) const
Could throw: array::at.
Definition vector3D.hpp:65
typename std::array< T, 3 >::pointer pointer
Definition vector3D.hpp:42
constexpr Vector3D(const container_type &data_) noexcept
Definition vector3D.hpp:57
constexpr bool empty() const noexcept
Capacity - these are fixed due to our definition of the internal array.
Definition vector3D.hpp:80
std::array< T, 3 > data
Definition vector3D.hpp:51
constexpr size_type max_size() const noexcept
Definition vector3D.hpp:82
constexpr Vector3D() noexcept=default
Constructors.
typename std::array< T, 3 >::iterator iterator
Definition vector3D.hpp:44
typename std::array< T, 3 >::const_pointer const_pointer
Definition vector3D.hpp:43
constexpr const_iterator cbegin() const noexcept
Definition vector3D.hpp:73
constexpr const_iterator end() const noexcept
Definition vector3D.hpp:76
constexpr friend bool operator<(const Vector3D< T > &lhs_, const Vector3D< T > &rhs_)
Definition vector3D.hpp:61
constexpr const_iterator begin() const noexcept
Definition vector3D.hpp:72
constexpr friend bool operator==(const Vector3D< T > &lhs_, const Vector3D< T > &rhs_)
Comparison operators - reuse the definitions from the internal array.
Definition vector3D.hpp:60
constexpr size_type size() const noexcept
Definition vector3D.hpp:81
typename std::array< T, 3 >::const_reference const_reference
Definition vector3D.hpp:41
constexpr reference at(size_type pos_)
Element access - reuse the definitions from the internal array.
Definition vector3D.hpp:64
constexpr iterator end() noexcept
Definition vector3D.hpp:75
std::array< T, 3 > container_type
With this member type, we are now both a container and container adapter.
Definition vector3D.hpp:48
typename std::array< T, 3 >::const_iterator const_iterator
Definition vector3D.hpp:45
constexpr reference operator[](size_type pos_) noexcept
Could throw: array::at.
Definition vector3D.hpp:67
constexpr void fill(const_reference value_)
Operations - reuse the definitions from the internal array.
Definition vector3D.hpp:85
T value_type
Member types.
Definition vector3D.hpp:37
Definition comparisonOperators.hpp:55
Definition backwardsDifferenceMethod.hpp:20
decltype(auto) get(Vector3D< T > &vec)
Definition vector3D.hpp:93
Definition fatalException.hpp:20
static constexpr int value
Definition vector3D.hpp:143