vector.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2020-2021 Ilias Khairullin <ilias@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_ALGEBRA_VECTOR_CLASS_HPP
28 #define CRYPTO3_ALGEBRA_VECTOR_CLASS_HPP
29 
32 
33 #include <array>
34 #include <cstddef>
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace algebra {
39 
49  template<typename T, std::size_t N>
50  struct vector {
51  static_assert(N != 0, "vector must contain at least one element");
52  // CRYPTO3_DETAIL_ASSERT_ARITHMETIC(T)
53 
54  using value_type = T;
55  using size_type = std::size_t;
56  static constexpr size_type size = N;
57 
60 
66  constexpr T &operator[](size_type i) noexcept {
67  return array[i];
68  }
69 
71  constexpr const T &operator[](size_type i) const noexcept {
72  return array[i];
73  }
75 
78 
83  constexpr T *begin() noexcept {
84  return array;
85  }
86 
92  constexpr T *end() noexcept {
93  return array + N;
94  }
95 
97  constexpr const T *cbegin() const noexcept {
98  return array;
99  }
100 
102  constexpr const T *cend() const noexcept {
103  return array + N;
104  }
106 
107  private:
108  T array[N];
109  };
110 
123  template<typename... Args>
124  constexpr decltype(auto) make_vector(Args... args) {
125  return vector {args...};
126  }
127 
130 
139  template<typename T, typename... U>
140  vector(T, U...) -> vector<std::enable_if_t<(std::is_same_v<T, U> && ...), T>, 1 + sizeof...(U)>;
141 
150  template<typename T, std::size_t N>
151  vector(const T (&)[N]) -> vector<T, N>;
152 
154 
157  } // namespace algebra
158  } // namespace crypto3
159 } // namespace nil
160 #endif // CRYPTO3_ALGEBRA_VECTOR_CLASS_HPP
vector(const T(&)[N]) -> vector< T, N >
deduction guide for aggregate initialization
constexpr decltype(auto) make_vector(Args... args)
constructs a vector from arguments
Definition: vector.hpp:124
vector(T, U...) -> vector< std::enable_if_t<(std::is_same_v< T, U > &&...), T >, 1+sizeof...(U)>
deduction guide for uniform initialization
Definition: pair.hpp:31
A container representing a vector.
Definition: vector.hpp:50
constexpr T * end() noexcept
returns an iterator to the end
Definition: vector.hpp:92
constexpr const T * cbegin() const noexcept
returns an iterator to the beginning
Definition: vector.hpp:97
static constexpr size_type size
size of the vector
Definition: vector.hpp:56
constexpr const T * cend() const noexcept
returns an iterator to the end
Definition: vector.hpp:102
constexpr T * begin() noexcept
returns an iterator to the beginning
Definition: vector.hpp:83
constexpr const T & operator[](size_type i) const noexcept
access specified element
Definition: vector.hpp:71
std::size_t size_type
Definition: vector.hpp:55
T value_type
Definition: vector.hpp:54
constexpr T & operator[](size_type i) noexcept
access specified element
Definition: vector.hpp:66