Cytnx v0.7.3
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
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
33 enum : cytnx_int64{
34 none,
35 Singl,
36 All,
37 Range,
38 Tilend,
39 Step,
40 Tn,
41 list
42 };
43
44 Accessor(): _type(Accessor::none){};
46
47 // single constructor
64 explicit Accessor(const cytnx_int64 &loc);
65 //explicit Accessor(const Tensor &tn);// construct from Tensor, should be 1d with dtype integer.
66
67
68
69 template<class T>
70 explicit Accessor(const std::initializer_list<T> &list){
71 std::vector<T> tmp = list;
72 this->_type = this->list;
73 this->idx_list = std::vector<cytnx_int64>(tmp.begin(),tmp.end());
74 //std::cout << "VV" << this->idx_list.size() << std::endl;
75 };// construct from vector/list, should be 1d with dtype integer.
76
77 template<class T>
78 explicit Accessor(const std::vector<T> &list){
79 this->_type = this->list;
80 this->idx_list = std::vector<cytnx_int64>(list.begin(),list.end());
81 };// construct from vector/list, should be 1d with dtype integer.
82
83
84
86
87
88 // all constr. ( use string to dispatch )
89 explicit Accessor(const std::string &str);
90
91
92
93
94 // range constr.
95 Accessor(const cytnx_int64 &min, const cytnx_int64 &max, const cytnx_int64 &step);
96
97
98 //copy constructor:
99 Accessor(const Accessor& rhs);
100 //copy assignment:
101 Accessor& operator=(const Accessor& rhs);
103
104 int type() const{
105 return this->_type;
106 }
107
108
109
110
111 //handy generator function :
125 static Accessor all(){
126 return Accessor(std::string(":"));
127 };
128
129
147 static Accessor range(const cytnx_int64 &min,
148 const cytnx_int64 &max,
149 const cytnx_int64 &step=1){
150 return Accessor(min,max,step);
151 };
152
153 static Accessor tilend(const cytnx_int64 &min, const cytnx_int64 &step=1){
154 cytnx_error_msg(step==0,"[ERROR] cannot have _step=0 for tilend%s","\n");
155 Accessor out;
156 out._type = Accessor::Tilend;
157 out._min = min;
158 out._step = step;
159 return out;
160 };
161
163 cytnx_error_msg(step==0,"[ERROR] cannot have _step=0 for _step%s","\n");
164 Accessor out;
165 out._type = Accessor::Step;
166 //out._min = 0;
167 out._step = step;
168 return out;
169 };
170
172 // get the real len from dim
173 // if type is all, pos will be null, and len == dim
174 // if type is range, pos will be the locator, and len == len(pos)
175 // if type is singl, pos will be pos, and len == 0
176 void get_len_pos(const cytnx_uint64 &dim, cytnx_uint64 &len, std::vector<cytnx_uint64> &pos) const;
178 };//class Accessor
179
181 //layout:
182 std::ostream& operator<<(std::ostream& os, const Accessor &in);
183
184 // elements resolver
185 template<class T>
186 void _resolve_elems(std::vector<cytnx::Accessor> &cool, const T& a){
187 cool.push_back(cytnx::Accessor(a));
188 }
189
190 template<class T, class ... Ts>
191 void _resolve_elems(std::vector<cytnx::Accessor> &cool,const T&a, const Ts&... args){
192 cool.push_back(cytnx::Accessor(a));
193 _resolve_elems(cool,args...);
194 }
195
196 template<class T,class ... Ts>
197 std::vector<cytnx::Accessor> Indices_resolver(const T&a, const Ts&... args){
198 //std::cout << a << std::endl;;
199 std::vector<cytnx::Accessor> idxs;
200 _resolve_elems(idxs,a,args...);
201 //cout << idxs << endl;
202 return idxs;
203 }
205
206}// namespace cytnx
207
208#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:153
static Accessor step(const cytnx_int64 &step)
Definition Accessor.hpp:162
static Accessor all()
access the whole rank, this is similar to [:] in python
Definition Accessor.hpp:125
Accessor(const std::initializer_list< T > &list)
Definition Accessor.hpp:70
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:147
int type() const
Definition Accessor.hpp:104
Accessor(const std::vector< T > &list)
Definition Accessor.hpp:78
#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