Ensign
Low-rank framework for multi-core CPUs and GPUs
Loading...
Searching...
No Matches
index.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <iterator>
9#include <vector>
10
11#include <generic/common.hpp>
12#include <generic/storage.hpp>
13
14namespace Ensign {
15
17namespace IndexFunction {
18template <class InputIt, class InputItInt>
19Index vec_index_to_comb_index(InputIt first, InputIt last, InputItInt first_int)
20{
21 Index comb_index = 0;
22 Index stride = 1;
23 for (; first != last; ++first, ++first_int) {
24 comb_index += *first * stride;
25 stride *= *first_int;
26 }
27 return comb_index;
28}
29
30template <class InputItInt, class OutputIt>
31void comb_index_to_vec_index(Index comb_index, InputItInt first, OutputIt d_first,
32 OutputIt d_last)
33{
34 assert(d_first != d_last);
35 for (; d_first != std::next(d_last, -1); ++first, ++d_first) {
36 *d_first = comb_index % *first;
37 comb_index = Index(comb_index / *first);
38 }
39 *(std::next(d_last, -1)) = comb_index;
40}
41
42template <class InputIt, class OutputIt>
43void incr_vec_index(InputIt first, OutputIt d_first, OutputIt d_last)
44{
45 assert(d_first != d_last);
46 for (; d_first != std::next(d_last, -1); ++first, ++d_first) {
47 ++(*d_first);
48 if (*d_first < *first)
49 return;
50 *d_first = typename std::iterator_traits<OutputIt>::value_type(0);
51 }
52 ++(*(std::next(d_last, -1)));
53}
54
55} // namespace IndexFunction
56
57} // namespace Ensign
Provides shared types, platform headers, and basic definitions.
ptrdiff_t Index
Definition common.hpp:83
Index vec_index_to_comb_index(InputIt first, InputIt last, InputItInt first_int)
Definition index.hpp:19
void comb_index_to_vec_index(Index comb_index, InputItInt first, OutputIt d_first, OutputIt d_last)
Definition index.hpp:31
void incr_vec_index(InputIt first, OutputIt d_first, OutputIt d_last)
Definition index.hpp:43
Core namespace for Ensign's numerical algorithms and data structures.
Definition common.hpp:96
Provides contiguous multidimensional arrays for CPU and GPU storage.