Cytnx v0.9.1
Loading...
Searching...
No Matches
Accessor.hpp
Go to the documentation of this file.
1#ifndef __Accessor_H_
2#define __Accessor_H_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6//#include "Tensor.hpp"
7#include <vector>
8#include <cstring>
9#include <string>
10#include <iostream>
11#include <initializer_list>
12namespace cytnx {
16 class Accessor {
17 private:
18 cytnx_int64 _type;
19
20 public:
22 cytnx_int64 _min{}, _max{}, _step{};
23 cytnx_int64 loc{};
24 std::vector<cytnx_int64> idx_list;
25
26 std::vector<std::vector<cytnx_int64>> qns_list;
27
28 // if type is singl, _min/_max/_step are not used
29 // if type is all , _min/_max/_step/loc are not used
30 // if type is range, loc are not used.
31 // if type is tilend, loc/_max are not used.
32 // if type is Qns, only qns_list are used.
33
34 enum : cytnx_int64 { none, Singl, All, Range, Tilend, Step, Tn, list, Qns };
35
36 Accessor() : _type(Accessor::none){};
38
39 // single constructor
56 explicit Accessor(const cytnx_int64 &loc);
57 // explicit Accessor(const Tensor &tn);// construct from Tensor, should be 1d with dtype
58 // integer.
59
60 template <class T>
61 explicit Accessor(const std::initializer_list<T> &list) {
62 std::vector<T> tmp = list;
63 this->_type = this->list;
64 this->idx_list = std::vector<cytnx_int64>(tmp.begin(), tmp.end());
65 // std::cout << "VV" << this->idx_list.size() << std::endl;
66 }; // construct from vector/list, should be 1d with dtype integer.
67
68 template <class T>
69 explicit Accessor(const std::vector<T> &list) {
70 this->_type = this->list;
71 this->idx_list = std::vector<cytnx_int64>(list.begin(), list.end());
72 }; // construct from vector/list, should be 1d with dtype integer.
73
75
76 // all constr. ( use string to dispatch )
77 explicit Accessor(const std::string &str);
78
79 // range constr.
80 Accessor(const cytnx_int64 &min, const cytnx_int64 &max, const cytnx_int64 &step);
81
82 // copy constructor:
83 Accessor(const Accessor &rhs);
84 // copy assignment:
85 Accessor &operator=(const Accessor &rhs);
87
88 int type() const { return this->_type; }
89
90 // handy generator function :
104 static Accessor all() { return Accessor(std::string(":")); };
105
123 static Accessor range(const cytnx_int64 &min, const cytnx_int64 &max,
124 const cytnx_int64 &step = 1) {
125 return Accessor(min, max, step);
126 };
127
128 static Accessor tilend(const cytnx_int64 &min, const cytnx_int64 &step = 1) {
129 cytnx_error_msg(step == 0, "[ERROR] cannot have _step=0 for tilend%s", "\n");
130 Accessor out;
131 out._type = Accessor::Tilend;
132 out._min = min;
133 out._step = step;
134 return out;
135 };
136
137 static Accessor step(const cytnx_int64 &step) {
138 cytnx_error_msg(step == 0, "[ERROR] cannot have _step=0 for _step%s", "\n");
139 Accessor out;
140 out._type = Accessor::Step;
141 // out._min = 0;
142 out._step = step;
143 return out;
144 };
145
146 static Accessor qns(const std::vector<std::vector<cytnx_int64>> &qns) {
147 cytnx_error_msg(qns.size() == 0, "[ERROR] cannot have empty qnums.%s", "\n");
148 Accessor out;
149
150 out._type = Accessor::Qns;
151 out.qns_list = qns;
152 return out;
153 }
154
156 // get the real len from dim
157 // if type is all, pos will be null, and len == dim
158 // if type is range, pos will be the locator, and len == len(pos)
159 // if type is singl, pos will be pos, and len == 0
160 void get_len_pos(const cytnx_uint64 &dim, cytnx_uint64 &len,
161 std::vector<cytnx_uint64> &pos) const;
163 }; // class Accessor
164
166 // layout:
167 std::ostream &operator<<(std::ostream &os, const Accessor &in);
168
169 // elements resolver
170 template <class T>
171 void _resolve_elems(std::vector<cytnx::Accessor> &cool, const T &a) {
172 cool.push_back(cytnx::Accessor(a));
173 }
174
175 template <class T, class... Ts>
176 void _resolve_elems(std::vector<cytnx::Accessor> &cool, const T &a, const Ts &...args) {
177 cool.push_back(cytnx::Accessor(a));
178 _resolve_elems(cool, args...);
179 }
180
181 template <class T, class... Ts>
182 std::vector<cytnx::Accessor> Indices_resolver(const T &a, const Ts &...args) {
183 // std::cout << a << std::endl;;
184 std::vector<cytnx::Accessor> idxs;
185 _resolve_elems(idxs, a, args...);
186 // cout << idxs << endl;
187 return idxs;
188 }
190
191} // namespace cytnx
192
193#endif
object that mimic the python slice to access elements in C++ [this is for c++ API only].
Definition Accessor.hpp:16
static Accessor tilend(const cytnx_int64 &min, const cytnx_int64 &step=1)
Definition Accessor.hpp:128
static Accessor step(const cytnx_int64 &step)
Definition Accessor.hpp:137
static Accessor all()
access the whole rank, this is similar to [:] in python
Definition Accessor.hpp:104
Accessor(const std::initializer_list< T > &list)
Definition Accessor.hpp:61
static Accessor range(const cytnx_int64 &min, const cytnx_int64 &max, const cytnx_int64 &step=1)
access the range at assigned rank, this is similar to min:max:step in python
Definition Accessor.hpp:123
static Accessor qns(const std::vector< std::vector< cytnx_int64 > > &qns)
Definition Accessor.hpp:146
int type() const
Definition Accessor.hpp:88
Accessor(const cytnx_int64 &loc)
access the specific index at the assigned rank in Tensor.
Accessor(const std::vector< T > &list)
Definition Accessor.hpp:69
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
uint64_t cytnx_uint64
Definition Type.hpp:45
int64_t cytnx_int64
Definition Type.hpp:48