26 #ifndef TILEDARRAY_PERM_INDEX_H__INCLUDED
27 #define TILEDARRAY_PERM_INDEX_H__INCLUDED
39 std::size_t* weights_;
52 : weights_(NULL), ndim_(perm.size()) {
61 weights_ =
static_cast<std::size_t*
>(
62 malloc((ndim_ + ndim_) *
sizeof(std::size_t)));
63 if (!weights_)
throw std::bad_alloc();
66 const auto* MADNESS_RESTRICT
const inv_perm = &inv_perm_.
data().front();
67 const auto* MADNESS_RESTRICT
const range_size = range.
extent_data();
68 const auto* MADNESS_RESTRICT
const range_weight = range.
stride_data();
71 std::size_t* MADNESS_RESTRICT
const input_weight = weights_;
72 std::size_t* MADNESS_RESTRICT
const output_weight = weights_ + ndim_;
76 for (
int i =
int(ndim_) - 1; i >= 0; --i) {
78 const auto inv_perm_i = inv_perm[i];
79 const auto weight = range_weight[i];
80 const auto size = range_size[inv_perm_i];
83 output_weight[inv_perm_i] =
volume;
85 input_weight[i] = weight;
93 weights_ =
static_cast<std::size_t*
>(
94 malloc((ndim_ + ndim_) *
sizeof(std::size_t)));
95 if (!weights_)
throw std::bad_alloc();
98 memcpy(weights_, other.weights_, (ndim_ + ndim_) *
sizeof(std::size_t));
109 if (ndim_ && (ndim_ != other.ndim_)) {
114 const std::size_t bytes = (other.ndim_ + other.ndim_) *
sizeof(std::size_t);
116 if (!weights_ && bytes) {
118 weights_ =
static_cast<std::size_t*
>(malloc(bytes));
119 if (!weights_)
throw std::bad_alloc();
124 memcpy(weights_, other.weights_, bytes);
132 int dim()
const {
return ndim_; }
137 const std::size_t*
data()
const {
return weights_; }
145 const std::size_t* MADNESS_RESTRICT
const input_weight = weights_;
146 const std::size_t* MADNESS_RESTRICT
const output_weight = weights_ + ndim_;
149 std::size_t perm_index = 0ul;
151 for (
unsigned int i = 0u; i < ndim_; ++i) {
152 const std::size_t input_weight_i = input_weight[i];
153 const std::size_t output_weight_i = output_weight[i];
154 perm_index += index / input_weight_i * output_weight_i;
155 index %= input_weight_i;
162 operator bool()
const {
return ndim_; }
168 #endif // MADNESS_PERM_INDEX_H__INCLUDED
169 TILEDARRAY_PERM_INDEX_H__INCLUDED