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