Cytnx v1.0.0
Loading...
Searching...
No Matches
Accessor.hpp
Go to the documentation of this file.
1#ifndef CYTNX_ACCESSOR_H_
2#define CYTNX_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 }; // 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);
86
87 // check equality
88 bool operator==(const Accessor &rhs) const;
90
91 int type() const { return this->_type; }
92
93 // handy generator function :
107 static Accessor all() { return Accessor(std::string(":")); };
108
127 static Accessor range(const cytnx_int64 &min, const cytnx_int64 &max,
128 const cytnx_int64 &step = 1) {
129 return Accessor(min, max, step);
130 };
131
132 static Accessor tilend(const cytnx_int64 &min, const cytnx_int64 &step = 1) {
133 cytnx_error_msg(step == 0, "[ERROR] Cannot have _step=0 for tilend%s", "\n");
134 Accessor out;
135 out._type = Accessor::Tilend;
136 out._min = min;
137 out._step = step;
138 return out;
139 };
140
141 static Accessor step(const cytnx_int64 &step) {
142 cytnx_error_msg(step == 0, "[ERROR] Cannot have _step=0 for _step%s", "\n");
143 Accessor out;
144 out._type = Accessor::Step;
145 // out._min = 0;
146 out._step = step;
147 return out;
148 };
149
150 static Accessor qns(const std::vector<std::vector<cytnx_int64>> &qns) {
151 cytnx_error_msg(qns.size() == 0, "[ERROR] Cannot have empty qnums.%s", "\n");
152 Accessor out;
153
154 out._type = Accessor::Qns;
155 out.qns_list = qns;
156 return out;
157 }
158
160 // get the real len from dim
161 // if type is all, pos will be null, and len == dim
162 // if type is range, pos will be the locator, and len == len(pos)
163 // if type is singl, pos will be pos, and len == 0
164 void get_len_pos(const cytnx_uint64 &dim, cytnx_uint64 &len,
165 std::vector<cytnx_uint64> &pos) const;
167 }; // class Accessor
168
170 // layout:
171 std::ostream &operator<<(std::ostream &os, const Accessor &in);
172
173 // elements resolver
174 template <class T>
175 void _resolve_elems(std::vector<cytnx::Accessor> &cool, const T &a) {
176 cool.push_back(cytnx::Accessor(a));
177 }
178
179 template <class T, class... Ts>
180 void _resolve_elems(std::vector<cytnx::Accessor> &cool, const T &a, const Ts &...args) {
181 cool.push_back(cytnx::Accessor(a));
182 _resolve_elems(cool, args...);
183 }
184
185 template <class T, class... Ts>
186 std::vector<cytnx::Accessor> Indices_resolver(const T &a, const Ts &...args) {
187 std::vector<cytnx::Accessor> idxs;
188 _resolve_elems(idxs, a, args...);
189 return idxs;
190 }
192
193} // namespace cytnx
194
195#endif // CYTNX_ACCESSOR_H_
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:132
static Accessor step(const cytnx_int64 &step)
Definition Accessor.hpp:141
static Accessor all()
access the whole rank, this is similar to [:] in python
Definition Accessor.hpp:107
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:127
static Accessor qns(const std::vector< std::vector< cytnx_int64 > > &qns)
Definition Accessor.hpp:150
int type() const
Definition Accessor.hpp:91
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:115
Definition Accessor.hpp:12
Tensor operator==(const Tensor &Lt, const Tensor &Rt)
The comparison operator for Tensor.