Cytnx v0.7.4
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
29 // if type is singl, _min/_max/_step are not used
30 // if type is all , _min/_max/_step/loc are not used
31 // if type is range, loc are not used.
32 // if type is tilend, loc/_max are not used.
33 // if type is Qns, only qns_list are used.
34
35 enum : cytnx_int64{
36 none,
37 Singl,
38 All,
39 Range,
40 Tilend,
41 Step,
42 Tn,
43 list,
44 Qns
45 };
46
47 Accessor(): _type(Accessor::none){};
49
50 // single constructor
67 explicit Accessor(const cytnx_int64 &loc);
68 //explicit Accessor(const Tensor &tn);// construct from Tensor, should be 1d with dtype integer.
69
70
71
72 template<class T>
73 explicit Accessor(const std::initializer_list<T> &list){
74 std::vector<T> tmp = list;
75 this->_type = this->list;
76 this->idx_list = std::vector<cytnx_int64>(tmp.begin(),tmp.end());
77 //std::cout << "VV" << this->idx_list.size() << std::endl;
78 };// construct from vector/list, should be 1d with dtype integer.
79
80 template<class T>
81 explicit Accessor(const std::vector<T> &list){
82 this->_type = this->list;
83 this->idx_list = std::vector<cytnx_int64>(list.begin(),list.end());
84 };// construct from vector/list, should be 1d with dtype integer.
85
86
87
89
90
91 // all constr. ( use string to dispatch )
92 explicit Accessor(const std::string &str);
93
94
95
96
97 // range constr.
98 Accessor(const cytnx_int64 &min, const cytnx_int64 &max, const cytnx_int64 &step);
99
100
101 //copy constructor:
102 Accessor(const Accessor& rhs);
103 //copy assignment:
104 Accessor& operator=(const Accessor& rhs);
106
107 int type() const{
108 return this->_type;
109 }
110
111
112
113
114 //handy generator function :
128 static Accessor all(){
129 return Accessor(std::string(":"));
130 };
131
132
150 static Accessor range(const cytnx_int64 &min,
151 const cytnx_int64 &max,
152 const cytnx_int64 &step=1){
153 return Accessor(min,max,step);
154 };
155
156 static Accessor tilend(const cytnx_int64 &min, const cytnx_int64 &step=1){
157 cytnx_error_msg(step==0,"[ERROR] cannot have _step=0 for tilend%s","\n");
158 Accessor out;
159 out._type = Accessor::Tilend;
160 out._min = min;
161 out._step = step;
162 return out;
163 };
164
166 cytnx_error_msg(step==0,"[ERROR] cannot have _step=0 for _step%s","\n");
167 Accessor out;
168 out._type = Accessor::Step;
169 //out._min = 0;
170 out._step = step;
171 return out;
172 };
173
174 static Accessor qns(const std::vector< std::vector<cytnx_int64> > &qns){
175 cytnx_error_msg(qns.size()==0,"[ERROR] cannot have empty qnums.%s","\n");
176 Accessor out;
177
178 out._type = Accessor::Qns;
179 out.qns_list = qns;
180 return out;
181 }
182
184 // get the real len from dim
185 // if type is all, pos will be null, and len == dim
186 // if type is range, pos will be the locator, and len == len(pos)
187 // if type is singl, pos will be pos, and len == 0
188 void get_len_pos(const cytnx_uint64 &dim, cytnx_uint64 &len, std::vector<cytnx_uint64> &pos) const;
190 };//class Accessor
191
193 //layout:
194 std::ostream& operator<<(std::ostream& os, const Accessor &in);
195
196 // elements resolver
197 template<class T>
198 void _resolve_elems(std::vector<cytnx::Accessor> &cool, const T& a){
199 cool.push_back(cytnx::Accessor(a));
200 }
201
202 template<class T, class ... Ts>
203 void _resolve_elems(std::vector<cytnx::Accessor> &cool,const T&a, const Ts&... args){
204 cool.push_back(cytnx::Accessor(a));
205 _resolve_elems(cool,args...);
206 }
207
208 template<class T,class ... Ts>
209 std::vector<cytnx::Accessor> Indices_resolver(const T&a, const Ts&... args){
210 //std::cout << a << std::endl;;
211 std::vector<cytnx::Accessor> idxs;
212 _resolve_elems(idxs,a,args...);
213 //cout << idxs << endl;
214 return idxs;
215 }
217
218}// namespace cytnx
219
220#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:156
static Accessor step(const cytnx_int64 &step)
Definition Accessor.hpp:165
static Accessor all()
access the whole rank, this is similar to [:] in python
Definition Accessor.hpp:128
Accessor(const std::initializer_list< T > &list)
Definition Accessor.hpp:73
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:150
static Accessor qns(const std::vector< std::vector< cytnx_int64 > > &qns)
Definition Accessor.hpp:174
int type() const
Definition Accessor.hpp:107
Accessor(const std::vector< T > &list)
Definition Accessor.hpp:81
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
std::ostream & operator<<(std::ostream &os, const Scalar &in)
Definition Scalar.cpp:14
uint64_t cytnx_uint64
Definition Type.hpp:22
int64_t cytnx_int64
Definition Type.hpp:25