echelon  0.8.0
map_indices.hpp
1 // Copyright (c) 2012-2014 Christopher Hinz
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef ECHELON_DETAIL_MAP_INDICES_HPP
7 #define ECHELON_DETAIL_MAP_INDICES_HPP
8 
9 #include <vector>
10 #include <cstddef>
11 #include <algorithm>
12 #include <initializer_list>
13 #include <cassert>
14 
15 #include <boost/iterator/zip_iterator.hpp>
16 #include <boost/tuple/tuple.hpp>
17 
18 namespace echelon
19 {
20 namespace detail
21 {
22 
23 template <typename... IndexTypes>
24 inline std::size_t map_indices(const std::vector<std::size_t>& shape, IndexTypes... indices)
25 {
26  assert(shape.size() == sizeof...(indices));
27 
28  const std::initializer_list<std::size_t> indices_ = {static_cast<std::size_t>(indices)...};
29 
30  auto first = boost::make_zip_iterator(boost::make_tuple(begin(shape), begin(indices_)));
31  auto last = boost::make_zip_iterator(boost::make_tuple(end(shape), end(indices_)));
32 
33  return std::accumulate(first, last, static_cast<std::size_t>(0),
34  [](std::size_t acc, const boost::tuple<std::size_t, std::size_t>& value)
35  { return boost::get<0>(value) * acc + boost::get<1>(value); });
36 }
37 }
38 }
39 
40 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10