block/include/nil/crypto3/detail/make_array.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_MAKE_ARRAY_HPP
26 #define CRYPTO3_MAKE_ARRAY_HPP
27 
28 #include <array>
29 #include <iterator>
30 
31 namespace nil {
32  namespace crypto3 {
33  namespace detail {
34  template<std::size_t... Indices>
35  struct indices {
36  using next = indices<Indices..., sizeof...(Indices)>;
37  };
38  template<std::size_t N>
39  struct build_indices {
40  using type = typename build_indices<N - 1>::type::next;
41  };
42  template<>
43  struct build_indices<0> {
44  using type = indices<>;
45  };
46  template<std::size_t N>
47  using BuildIndices = typename build_indices<N>::type;
48 
49  template<typename Iterator>
50  using ValueType = typename std::iterator_traits<Iterator>::value_type;
51 
52  // internal overload with indices tag
53 
54  template<std::size_t... I,
55  typename InputIterator,
56  typename Array = std::array<ValueType<InputIterator>, sizeof...(I)>>
57 
58  Array make_array(InputIterator first, indices<I...>) {
59  return Array {{(void(I), *first++)...}};
60  }
61  } // namespace detail
62 
63  // externally visible interface
64  template<std::size_t N, typename RandomAccessIterator>
65  std::array<detail::ValueType<RandomAccessIterator>, N> make_array(RandomAccessIterator first,
66  RandomAccessIterator last) {
67  // last is not relevant if we're assuming the size is N
68  // I'll assert it is correct anyway
69  assert(last - first == N);
70  return make_array(first, detail::BuildIndices<N> {});
71  }
72  } // namespace crypto3
73 } // namespace nil
74 
75 #endif // CRYPTO3_MAKE_ARRAY_HPP
Array make_array(InputIterator first, indices< I... >)
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:58
typename build_indices< N >::type BuildIndices
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:47
typename std::iterator_traits< Iterator >::value_type ValueType
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:50
std::array< detail::ValueType< RandomAccessIterator >, N > make_array(RandomAccessIterator first, RandomAccessIterator last)
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:65
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:39
typename build_indices< N - 1 >::type::next type
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:40
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:35