| 30 | }; |
| 31 | |
| 32 | struct index_cache : public std::unique_ptr<block_t[]> { |
| 33 | using ptr_t = std::unique_ptr<block_t[]>; |
| 34 | |
| 35 | struct dim_visitor { |
| 36 | mutable std::size_t stride; |
| 37 | mutable block_t* b; |
| 38 | template <typename Axis> |
| 39 | void operator()(const Axis& a) const noexcept { |
| 40 | b->dim = dim_t{0, a.size(), stride}; |
| 41 | ++b; |
| 42 | stride *= a.shape(); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | template <typename H> |
| 47 | void set(const H& h) { |
| 48 | if (!(*this) || h.dim() != ptr_t::get()->state.dim) { |
| 49 | ptr_t::reset(new block_t[h.dim() + 1]); |
| 50 | ptr_t::get()->state.dim = h.dim(); |
| 51 | ptr_t::get()->state.idx = 0; |
| 52 | } |
| 53 | h.for_each_axis(dim_visitor{1, ptr_t::get() + 1}); |
| 54 | } |
| 55 | |
| 56 | void set_idx(std::size_t idx) { |
| 57 | auto& s = ptr_t::get()->state; |
| 58 | if (idx == s.idx) return; |
| 59 | s.idx = idx; |
| 60 | auto d = s.dim; |
| 61 | auto b = (ptr_t::get() + 1) + d; |
| 62 | while ((--b, --d)) { |
| 63 | b->dim.idx = idx / b->dim.stride; |
| 64 | idx -= b->dim.idx * b->dim.stride; |
| 65 | b->dim.idx -= (b->dim.idx > b->dim.size) * (b->dim.size + 2); |
| 66 | } |
| 67 | b->dim.idx = idx; |
| 68 | b->dim.idx -= (b->dim.idx > b->dim.size) * (b->dim.size + 2); |
| 69 | } |
| 70 | |
| 71 | int get(unsigned d) const { return (ptr_t::get() + 1 + d)->dim.idx; } |
| 72 | }; |
| 73 | } |
| 74 | } |
| 75 | } |
nothing calls this directly
no outgoing calls
no test coverage detected