5 #ifndef TTG_UTIL_MULTIINDEX_H
6 #define TTG_UTIL_MULTIINDEX_H
12 template <std::
size_t Rank,
typename Int =
int>
14 static constexpr
const std::size_t
max_index = 1 << 21;
17 template <
typename Integer,
typename = std::enable_if_t<std::is_
integral_v<Int>>>
19 std::copy(ilist.begin(), ilist.end(), data_.begin());
22 template <
typename... Ints,
typename = std::enable_if_t<(std::is_integral_v<Ints> && ...)>>
23 MultiIndex(Ints... ilist) : data_{{
static_cast<Int
>(ilist)...}} {
27 static_assert(Rank == 1 || Rank == 2 || Rank == 3,
28 "MultiIndex<Rank>::MultiIndex(hash) only implemented for Rank={1,2,3}");
30 assert(
hash < max_index);
34 (*this)[0] =
hash / max_index;
35 (*this)[1] =
hash % max_index;
36 }
else if (Rank == 3) {
37 (*this)[0] =
hash / max_index_square;
38 (*this)[1] = (
hash % max_index_square) / max_index;
39 (*this)[2] =
hash % max_index;
43 static_assert(Rank == 1 || Rank == 2 || Rank == 3,
"MultiIndex<Rank>::hash only implemented for Rank={1,2,3}");
44 if constexpr (Rank == 1)
46 else if constexpr (Rank == 2) {
47 return (*
this)[0] * max_index + (*this)[1];
48 }
else if constexpr (Rank == 3) {
49 return ((*
this)[0] * max_index + (*
this)[1]) * max_index + (*this)[2];
54 if (idx >= Rank) assert(idx < Rank);
58 template <
typename Archive>
66 for (
const auto &idx : data_) {
67 result = result && (idx < max_index);
72 std::array<Int, Rank> data_;
75 return lhs.data_ == rhs.data_;
82 template <std::
size_t Rank>
85 for (
size_t i = 0; i != Rank; ++i) os << key[i] << (i + 1 != Rank ?
"," :
"");
92 #ifdef TTG_SERIALIZATION_SUPPORTS_MADNESS
95 template <
class Archive, std::
size_t Rank>
96 struct ArchiveStoreImpl<Archive,
ttg::MultiIndex<Rank>> {
98 for (
size_t i = 0; i != Rank; ++i) ar << mi[i];
102 template <
class Archive, std::
size_t Rank>
103 struct ArchiveLoadImpl<Archive,
ttg::MultiIndex<Rank>> {
105 for (
size_t i = 0; i != Rank; ++i) ar >> mi[i];
111 static_assert(madness::is_serializable_v<madness::archive::BufferOutputArchive,
ttg::MultiIndex<3>>);
top-level TTG namespace contains runtime-neutral functionality
std::array< int, 3 > version()
std::ostream & operator<<(std::ostream &os, const MultiIndex< Rank > &key)
MultiIndex(std::size_t hash)
MultiIndex(std::initializer_list< Integer > ilist)
MultiIndex(Ints... ilist)
void serialize(Archive &ar, const unsigned int version=0)
static constexpr const std::size_t max_index_square
friend bool operator==(const MultiIndex< Rank > &lhs, const MultiIndex< Rank > &rhs)
static constexpr const std::size_t max_index
friend bool operator!=(const MultiIndex< Rank > &lhs, const MultiIndex< Rank > &rhs)
const auto & operator[](std::size_t idx) const
Computes hash values for objects of type T.