Cytnx v0.7.6
Loading...
Searching...
No Matches
Storage.hpp
Go to the documentation of this file.
1#ifndef _H_Storage_
2#define _H_Storage_
3
4#include <iostream>
5#include <fstream>
6#include <cstdlib>
7#include <cstdio>
8#include <cstring>
9#include <initializer_list>
10#include <typeinfo>
11#include <vector>
12#include <complex>
13
14
15#include "Type.hpp"
16#include "Device.hpp"
18#include "cytnx_error.hpp"
19#include "Scalar.hpp"
20
21
22#define STORAGE_DEFT_SZ 2
23
24namespace cytnx{
25
27 class Storage_base : public intrusive_ptr_base<Storage_base> {
28 public:
29 void* Mem;
30 //std::vector<unsigned int> shape;
31
32 unsigned long long len; // default 0
33 unsigned long long cap; // default 0
34 unsigned int dtype; // default 0, Void
35 int device; // default -1, on cpu
36
37 Storage_base(): cap(0),len(0), Mem(NULL),dtype(0), device(-1){};
38 //Storage_base(const std::initializer_list<unsigned int> &init_shape);
39 //Storage_base(const std::vector<unsigned int> &init_shape);
40 Storage_base(const unsigned long long &len_in,const int &device);
41
42 Storage_base(Storage_base &Rhs);
43 Storage_base& operator=(Storage_base &Rhs);
44 boost::intrusive_ptr<Storage_base> astype(const unsigned int &dtype);
45
46 //void Init(const std::initializer_list<unsigned int> &init_shape);
47 std::string dtype_str() const ;
48 std::string device_str() const;
49 const unsigned long long &capacity() const{
50 return this->cap;
51 }
52 const unsigned long long &size() const{
53 return this->len;
54 }
55 ~Storage_base();
56
57
58 template<class T>
59 T& at(const cytnx_uint64 &idx) const;
60
61 template<class T>
62 T& back() const;
63
64 template<class T>
65 T* data() const;
66
67 void* data() const{
68 return this->Mem;
69 }
70
71 void _cpy_bool(void *ptr, const std::vector<cytnx_bool> &vin);
72
73 void print();
74 void print_info();
75 /*
76 This function is design to check the type mismatch.
77 Handy for developer to exclude the assign of double
78 C pointer into a non-DoubleStorage.
79
80 For example:
81 float *cptr = (float*)calloc(4,sizeof(float));
82
83 intrusive_ptr<Storage> array(new DoubleStorage());
84 array->_Init_byptr((void*)cptr,4); // This is fatal, since we alloc cptr as float,
85 // but apon free, DoubleStorage will free 2x
86 // of memory!!!!
87
88 array->_Init_byptr_safe(cptr,4); // This is design to avoid the above problem
89 // by checking the type of input pointer with
90 // the type of Storage before call _Init_byptr.
91 // [Note] this will intorduce overhead!!.
92
93 */
94 template<class T>
95 void _Init_byptr_safe(T *rawptr, const unsigned long long &len_in){
96 //check:
97 if(this->dtype==Type.Float){
98 cytnx_error_msg(typeid(T) != typeid(cytnx_float),"%s","[ERROR _Init_byptr_safe type not match]");
99 }else if(this->dtype==Type.Double){
100 cytnx_error_msg(typeid(T) != typeid(cytnx_double),"%s","[ERROR _Init_byptr_safe type not match]");
101 }else if(this->dtype==Type.Uint64){
102 cytnx_error_msg(typeid(T) != typeid(cytnx_uint64),"%s","[ERROR _Init_byptr_safe type not match]");
103 }else if(this->dtype==Type.Uint32){
104 cytnx_error_msg(typeid(T) != typeid(cytnx_uint32),"%s","[ERROR _Init_byptr_safe type not match]");
105 }else if(this->dtype==Type.Int64){
106 cytnx_error_msg(typeid(T) != typeid(cytnx_int64),"%s","[ERROR _Init_byptr_safe type not match]");
107 }else if(this->dtype==Type.Int32){
108 cytnx_error_msg(typeid(T) != typeid(cytnx_int32),"%s","[ERROR _Init_byptr_safe type not match]");
109 }else if(this->dtype==Type.ComplexDouble){
110 cytnx_error_msg(typeid(T) != typeid(cytnx_complex128),"%s","[ERROR _Init_byptr_safe type not match]");
111 }else if(this->dtype==Type.ComplexFloat){
112 cytnx_error_msg(typeid(T) != typeid(cytnx_complex64),"%s","[ERROR _Init_byptr_safe type not match]");
113 }else if(this->dtype==Type.Int16){
114 cytnx_error_msg(typeid(T) != typeid(cytnx_int16),"%s","[ERROR _Init_byptr_safe type not match]");
115 }else if(this->dtype==Type.Uint16){
116 cytnx_error_msg(typeid(T) != typeid(cytnx_uint16),"%s","[ERROR _Init_byptr_safe type not match]");
117 }else if(this->dtype==Type.Bool){
118 cytnx_error_msg(typeid(T) != typeid(cytnx_bool),"%s","[ERROR _Init_byptr_safe type not match]");
119 }else{
120 cytnx_error_msg(1,"[FATAL] ERROR%s","\n");
121 }
122
123 this->_Init_byptr((void*)rawptr,len_in);
124 }
125
126
127
128 void GetElem_byShape_v2(boost::intrusive_ptr<Storage_base> &out, const std::vector<cytnx_uint64> &shape, const std::vector<std::vector<cytnx_uint64> > &locators,const cytnx_uint64 &Nunit);
129 void GetElem_byShape(boost::intrusive_ptr<Storage_base> &out, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &len, const std::vector<std::vector<cytnx_uint64> > &locators);
130 void SetElem_byShape(boost::intrusive_ptr<Storage_base> &in, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &len, const std::vector<std::vector<cytnx_uint64> > &locators, const bool &is_scalar);
131 void SetElem_byShape_v2(boost::intrusive_ptr<Storage_base> &in, const std::vector<cytnx_uint64> &shape, const std::vector<std::vector<cytnx_uint64> > &locators,const cytnx_uint64 &Nunit, const bool &is_scalar);
132 // these is the one that do the work, and customize with Storage_base
133 //virtual void Init(const std::vector<unsigned int> &init_shape);
134 virtual void Init(const unsigned long long &len_in, const int &device=-1);
135 virtual void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
136
137 // this function will return a new storage with the same type as the one
138 // that initiate this function.
139 virtual boost::intrusive_ptr<Storage_base> _create_new_sametype();
140
141 // [future] this will move the memory to device / cpu
142 virtual void to_(const int &device);
143 virtual boost::intrusive_ptr<Storage_base> to(const int &device);
144
145 virtual boost::intrusive_ptr<Storage_base> clone();
146
147 // this will perform permute on the underlying memory.
148 virtual boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
149 virtual void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
150 virtual void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
151 virtual void print_elems();
152
153 virtual boost::intrusive_ptr<Storage_base> real();
154 virtual boost::intrusive_ptr<Storage_base> imag();
155
156 //generators:
157 virtual void fill(const cytnx_complex128 &val);
158 virtual void fill(const cytnx_complex64 &val);
159 virtual void fill(const cytnx_double &val);
160 virtual void fill(const cytnx_float &val);
161 virtual void fill(const cytnx_int64 &val);
162 virtual void fill(const cytnx_uint64 &val);
163 virtual void fill(const cytnx_int32 &val);
164 virtual void fill(const cytnx_uint32 &val);
165 virtual void fill(const cytnx_int16 &val);
166 virtual void fill(const cytnx_uint16 &val);
167 virtual void fill(const cytnx_bool &val);
168 virtual void set_zeros();
169 virtual void resize(const cytnx_uint64 &newsize);
170
171
172 virtual void append(const Scalar &val);
173 virtual void append(const cytnx_complex128 &val);
174 virtual void append(const cytnx_complex64 &val);
175 virtual void append(const cytnx_double &val);
176 virtual void append(const cytnx_float &val);
177 virtual void append(const cytnx_int64 &val);
178 virtual void append(const cytnx_uint64 &val);
179 virtual void append(const cytnx_int32 &val);
180 virtual void append(const cytnx_uint32 &val);
181 virtual void append(const cytnx_int16 &val);
182 virtual void append(const cytnx_uint16 &val);
183 virtual void append(const cytnx_bool &val);
184
185 virtual Scalar get_item(const cytnx_uint64 &in) const;
186
187 virtual void set_item(const cytnx_uint64 &idx,const Scalar &val);
188 virtual void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
189 virtual void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
190 virtual void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
191 virtual void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
192 virtual void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
193 virtual void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
194 virtual void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
195 virtual void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
196 virtual void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
197 virtual void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
198 virtual void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
199
200
201 };
203
205 class FloatStorage : public Storage_base{
206 public:
207 FloatStorage(){this->dtype=Type.Float;};
208 void Init(const unsigned long long &len_in, const int &device=-1);
209 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
210 boost::intrusive_ptr<Storage_base> _create_new_sametype();
211 boost::intrusive_ptr<Storage_base> clone();
212 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
213 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
214 void to_(const int &device);
215 boost::intrusive_ptr<Storage_base> to(const int &device);
216 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
217 void print_elems();
218
219 boost::intrusive_ptr<Storage_base> real();
220 boost::intrusive_ptr<Storage_base> imag();
221
222
223 //generators:
224 void fill(const cytnx_complex128 &val);
225 void fill(const cytnx_complex64 &val);
226 void fill(const cytnx_double &val);
227 void fill(const cytnx_float &val);
228 void fill(const cytnx_int64 &val);
229 void fill(const cytnx_uint64 &val);
230 void fill(const cytnx_int32 &val);
231 void fill(const cytnx_uint32 &val);
232 void fill(const cytnx_int16 &val);
233 void fill(const cytnx_uint16 &val);
234 void fill(const cytnx_bool &val);
235 void set_zeros();
236 void resize(const cytnx_uint64 &newsize);
237
238 void append(const Scalar &val);
239 void append(const cytnx_complex128 &val);
240 void append(const cytnx_complex64 &val);
241 void append(const cytnx_double &val);
242 void append(const cytnx_float &val);
243 void append(const cytnx_int64 &val);
244 void append(const cytnx_uint64 &val);
245 void append(const cytnx_int32 &val);
246 void append(const cytnx_uint32 &val);
247 void append(const cytnx_int16 &val);
248 void append(const cytnx_uint16 &val);
249 void append(const cytnx_bool &val);
250 Scalar get_item(const cytnx_uint64 &in) const;
251
252
253 void set_item(const cytnx_uint64 &idx,const Scalar &val);
254 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
255 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
256 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
257 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
258 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
259 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
260 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
261 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
262 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
263 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
264 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
265
266
267 };
269
271 class DoubleStorage: public Storage_base{
272 public:
273 DoubleStorage(){this->dtype=Type.Double;};
274 void Init(const unsigned long long &len_in,const int &device=-1);
275 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
276 boost::intrusive_ptr<Storage_base> _create_new_sametype();
277 boost::intrusive_ptr<Storage_base> clone();
278 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
279 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
280 void to_(const int &device);
281 boost::intrusive_ptr<Storage_base> to(const int &device);
282 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
283 void print_elems();
284
285 boost::intrusive_ptr<Storage_base> real();
286 boost::intrusive_ptr<Storage_base> imag();
287
288 //generators:
289 void fill(const cytnx_complex128 &val);
290 void fill(const cytnx_complex64 &val);
291 void fill(const cytnx_double &val);
292 void fill(const cytnx_float &val);
293 void fill(const cytnx_int64 &val);
294 void fill(const cytnx_uint64 &val);
295 void fill(const cytnx_int32 &val);
296 void fill(const cytnx_uint32 &val);
297 void fill(const cytnx_int16 &val);
298 void fill(const cytnx_uint16 &val);
299 void fill(const cytnx_bool &val);
300 void set_zeros();
301 void resize(const cytnx_uint64 &newsize);
302
303 void append(const Scalar &val);
304 void append(const cytnx_complex128 &val);
305 void append(const cytnx_complex64 &val);
306 void append(const cytnx_double &val);
307 void append(const cytnx_float &val);
308 void append(const cytnx_int64 &val);
309 void append(const cytnx_uint64 &val);
310 void append(const cytnx_int32 &val);
311 void append(const cytnx_uint32 &val);
312 void append(const cytnx_int16 &val);
313 void append(const cytnx_uint16 &val);
314 void append(const cytnx_bool &val);
315 Scalar get_item(const cytnx_uint64 &in) const;
316
317 void set_item(const cytnx_uint64 &idx,const Scalar &val);
318 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
319 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
320 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
321 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
322 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
323 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
324 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
325 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
326 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
327 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
328 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
329
330 };
332
334 class ComplexDoubleStorage: public Storage_base{
335 public:
336 ComplexDoubleStorage(){this->dtype=Type.ComplexDouble;};
337 void Init(const unsigned long long &len_in, const int &device=-1);
338 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
339 boost::intrusive_ptr<Storage_base> _create_new_sametype();
340 boost::intrusive_ptr<Storage_base> clone();
341 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
342 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
343 void to_(const int &device);
344 boost::intrusive_ptr<Storage_base> to(const int &device);
345 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
346 void print_elems();
347
348 boost::intrusive_ptr<Storage_base> real();
349 boost::intrusive_ptr<Storage_base> imag();
350
351 //generators:
352 void fill(const cytnx_complex128 &val);
353 void fill(const cytnx_complex64 &val);
354 void fill(const cytnx_double &val);
355 void fill(const cytnx_float &val);
356 void fill(const cytnx_int64 &val);
357 void fill(const cytnx_uint64 &val);
358 void fill(const cytnx_int32 &val);
359 void fill(const cytnx_uint32 &val);
360 void fill(const cytnx_int16 &val);
361 void fill(const cytnx_uint16 &val);
362 void fill(const cytnx_bool &val);
363 void set_zeros();
364 void resize(const cytnx_uint64 &newsize);
365
366 void append(const Scalar &val);
367 void append(const cytnx_complex128 &val);
368 void append(const cytnx_complex64 &val);
369 void append(const cytnx_double &val);
370 void append(const cytnx_float &val);
371 void append(const cytnx_int64 &val);
372 void append(const cytnx_uint64 &val);
373 void append(const cytnx_int32 &val);
374 void append(const cytnx_uint32 &val);
375 void append(const cytnx_int16 &val);
376 void append(const cytnx_uint16 &val);
377 void append(const cytnx_bool &val);
378 Scalar get_item(const cytnx_uint64 &in) const;
379
380
381 void set_item(const cytnx_uint64 &idx,const Scalar &val);
382 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
383 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
384 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
385 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
386 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
387 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
388 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
389 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
390 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
391 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
392 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
393
394 };
396
398 class ComplexFloatStorage: public Storage_base{
399 public:
400 ComplexFloatStorage(){this->dtype=Type.ComplexFloat;};
401 void Init(const unsigned long long &len_in,const int &device=-1);
402 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
403 boost::intrusive_ptr<Storage_base> _create_new_sametype();
404 boost::intrusive_ptr<Storage_base> clone();
405 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
406 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
407 void to_(const int &device);
408 boost::intrusive_ptr<Storage_base> to(const int &device);
409 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
410 void print_elems();
411
412 boost::intrusive_ptr<Storage_base> real();
413 boost::intrusive_ptr<Storage_base> imag();
414
415 //generators:
416 void fill(const cytnx_complex128 &val);
417 void fill(const cytnx_complex64 &val);
418 void fill(const cytnx_double &val);
419 void fill(const cytnx_float &val);
420 void fill(const cytnx_int64 &val);
421 void fill(const cytnx_uint64 &val);
422 void fill(const cytnx_int32 &val);
423 void fill(const cytnx_uint32 &val);
424 void fill(const cytnx_int16 &val);
425 void fill(const cytnx_uint16 &val);
426 void fill(const cytnx_bool &val);
427 void set_zeros();
428 void resize(const cytnx_uint64 &newsize);
429
430
431 void append(const Scalar &val);
432 void append(const cytnx_complex128 &val);
433 void append(const cytnx_complex64 &val);
434 void append(const cytnx_double &val);
435 void append(const cytnx_float &val);
436 void append(const cytnx_int64 &val);
437 void append(const cytnx_uint64 &val);
438 void append(const cytnx_int32 &val);
439 void append(const cytnx_uint32 &val);
440 void append(const cytnx_int16 &val);
441 void append(const cytnx_uint16 &val);
442 void append(const cytnx_bool &val);
443 Scalar get_item(const cytnx_uint64 &in) const;
444
445 void set_item(const cytnx_uint64 &idx,const Scalar &val);
446 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
447 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
448 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
449 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
450 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
451 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
452 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
453 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
454 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
455 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
456 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
457
458
459 };
461
463 class Int64Storage : public Storage_base{
464 public:
465 Int64Storage(){this->dtype=Type.Int64;};
466 void Init(const unsigned long long &len_in, const int &device=-1);
467 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
468 boost::intrusive_ptr<Storage_base> _create_new_sametype();
469 boost::intrusive_ptr<Storage_base> clone();
470 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
471 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
472 void to_(const int &device);
473 boost::intrusive_ptr<Storage_base> to(const int &device);
474 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
475 void print_elems();
476
477 boost::intrusive_ptr<Storage_base> real();
478 boost::intrusive_ptr<Storage_base> imag();
479
480
481 //generators:
482 void fill(const cytnx_complex128 &val);
483 void fill(const cytnx_complex64 &val);
484 void fill(const cytnx_double &val);
485 void fill(const cytnx_float &val);
486 void fill(const cytnx_int64 &val);
487 void fill(const cytnx_uint64 &val);
488 void fill(const cytnx_int32 &val);
489 void fill(const cytnx_uint32 &val);
490 void fill(const cytnx_int16 &val);
491 void fill(const cytnx_uint16 &val);
492 void fill(const cytnx_bool &val);
493 void set_zeros();
494 void resize(const cytnx_uint64 &newsize);
495
496
497 void append(const Scalar &val);
498 void append(const cytnx_complex128 &val);
499 void append(const cytnx_complex64 &val);
500 void append(const cytnx_double &val);
501 void append(const cytnx_float &val);
502 void append(const cytnx_int64 &val);
503 void append(const cytnx_uint64 &val);
504 void append(const cytnx_int32 &val);
505 void append(const cytnx_uint32 &val);
506 void append(const cytnx_int16 &val);
507 void append(const cytnx_uint16 &val);
508 void append(const cytnx_bool &val);
509 Scalar get_item(const cytnx_uint64 &in) const;
510
511
512 void set_item(const cytnx_uint64 &idx,const Scalar &val);
513 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
514 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
515 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
516 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
517 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
518 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
519 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
520 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
521 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
522 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
523 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
524
525
526 };
528
530 class Uint64Storage : public Storage_base{
531 public:
532 Uint64Storage(){this->dtype=Type.Uint64;};
533 void Init(const unsigned long long &len_in, const int &device=-1);
534 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
535 boost::intrusive_ptr<Storage_base> _create_new_sametype();
536 boost::intrusive_ptr<Storage_base> clone();
537 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
538 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
539 void to_(const int &device);
540 boost::intrusive_ptr<Storage_base> to(const int &device);
541 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
542 void print_elems();
543
544
545 boost::intrusive_ptr<Storage_base> real();
546 boost::intrusive_ptr<Storage_base> imag();
547
548 //generators:
549 void fill(const cytnx_complex128 &val);
550 void fill(const cytnx_complex64 &val);
551 void fill(const cytnx_double &val);
552 void fill(const cytnx_float &val);
553 void fill(const cytnx_int64 &val);
554 void fill(const cytnx_uint64 &val);
555 void fill(const cytnx_int32 &val);
556 void fill(const cytnx_uint32 &val);
557 void fill(const cytnx_int16 &val);
558 void fill(const cytnx_uint16 &val);
559 void fill(const cytnx_bool &val);
560 void set_zeros();
561 void resize(const cytnx_uint64 &newsize);
562
563
564 void append(const Scalar &val);
565 void append(const cytnx_complex128 &val);
566 void append(const cytnx_complex64 &val);
567 void append(const cytnx_double &val);
568 void append(const cytnx_float &val);
569 void append(const cytnx_int64 &val);
570 void append(const cytnx_uint64 &val);
571 void append(const cytnx_int32 &val);
572 void append(const cytnx_uint32 &val);
573 void append(const cytnx_int16 &val);
574 void append(const cytnx_uint16 &val);
575 void append(const cytnx_bool &val);
576 Scalar get_item(const cytnx_uint64 &in) const;
577
578
579 void set_item(const cytnx_uint64 &idx,const Scalar &val);
580 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
581 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
582 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
583 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
584 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
585 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
586 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
587 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
588 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
589 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
590 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
591
592
593 };
596 class Int32Storage : public Storage_base{
597 public:
598 Int32Storage(){this->dtype=Type.Int32;};
599 void Init(const unsigned long long &len_in, const int &device=-1);
600 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
601 boost::intrusive_ptr<Storage_base> _create_new_sametype();
602 boost::intrusive_ptr<Storage_base> clone();
603 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
604 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
605 void to_(const int &device);
606 boost::intrusive_ptr<Storage_base> to(const int &device);
607 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
608 void print_elems();
609
610 boost::intrusive_ptr<Storage_base> real();
611 boost::intrusive_ptr<Storage_base> imag();
612
613 //generators:
614 void fill(const cytnx_complex128 &val);
615 void fill(const cytnx_complex64 &val);
616 void fill(const cytnx_double &val);
617 void fill(const cytnx_float &val);
618 void fill(const cytnx_int64 &val);
619 void fill(const cytnx_uint64 &val);
620 void fill(const cytnx_int32 &val);
621 void fill(const cytnx_uint32 &val);
622 void fill(const cytnx_int16 &val);
623 void fill(const cytnx_uint16 &val);
624 void fill(const cytnx_bool &val);
625 void set_zeros();
626 void resize(const cytnx_uint64 &newsize);
627 void append(const Scalar &val);
628 void append(const cytnx_complex128 &val);
629 void append(const cytnx_complex64 &val);
630 void append(const cytnx_double &val);
631 void append(const cytnx_float &val);
632 void append(const cytnx_int64 &val);
633 void append(const cytnx_uint64 &val);
634 void append(const cytnx_int32 &val);
635 void append(const cytnx_uint32 &val);
636 void append(const cytnx_int16 &val);
637 void append(const cytnx_uint16 &val);
638 void append(const cytnx_bool &val);
639 Scalar get_item(const cytnx_uint64 &in) const;
640
641
642 void set_item(const cytnx_uint64 &idx,const Scalar &val);
643 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
644 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
645 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
646 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
647 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
648 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
649 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
650 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
651 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
652 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
653 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
654
655
656 };
658
660 class Uint32Storage : public Storage_base{
661 public:
662 Uint32Storage(){this->dtype=Type.Uint32;};
663 void Init(const unsigned long long &len_in, const int &device=-1);
664 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
665 boost::intrusive_ptr<Storage_base> _create_new_sametype();
666 boost::intrusive_ptr<Storage_base> clone();
667 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
668 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
669 void to_(const int &device);
670 boost::intrusive_ptr<Storage_base> to(const int &device);
671 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
672 void print_elems();
673
674 boost::intrusive_ptr<Storage_base> real();
675 boost::intrusive_ptr<Storage_base> imag();
676
677
678 //generators:
679 void fill(const cytnx_complex128 &val);
680 void fill(const cytnx_complex64 &val);
681 void fill(const cytnx_double &val);
682 void fill(const cytnx_float &val);
683 void fill(const cytnx_int64 &val);
684 void fill(const cytnx_uint64 &val);
685 void fill(const cytnx_int32 &val);
686 void fill(const cytnx_uint32 &val);
687 void fill(const cytnx_int16 &val);
688 void fill(const cytnx_uint16 &val);
689 void fill(const cytnx_bool &val);
690 void set_zeros();
691 void resize(const cytnx_uint64 &newsize);
692 void append(const Scalar &val);
693 void append(const cytnx_complex128 &val);
694 void append(const cytnx_complex64 &val);
695 void append(const cytnx_double &val);
696 void append(const cytnx_float &val);
697 void append(const cytnx_int64 &val);
698 void append(const cytnx_uint64 &val);
699 void append(const cytnx_int32 &val);
700 void append(const cytnx_uint32 &val);
701 void append(const cytnx_int16 &val);
702 void append(const cytnx_uint16 &val);
703 void append(const cytnx_bool &val);
704 Scalar get_item(const cytnx_uint64 &in) const;
705
706 void set_item(const cytnx_uint64 &idx,const Scalar &val);
707 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
708 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
709 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
710 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
711 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
712 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
713 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
714 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
715 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
716 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
717 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
718
719 };
721
723 class Uint16Storage : public Storage_base{
724 public:
725 Uint16Storage(){this->dtype=Type.Uint16;};
726 void Init(const unsigned long long &len_in, const int &device=-1);
727 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
728 boost::intrusive_ptr<Storage_base> _create_new_sametype();
729 boost::intrusive_ptr<Storage_base> clone();
730 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
731 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
732 void to_(const int &device);
733 boost::intrusive_ptr<Storage_base> to(const int &device);
734 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
735 void print_elems();
736
737 boost::intrusive_ptr<Storage_base> real();
738 boost::intrusive_ptr<Storage_base> imag();
739
740 //generators:
741 void fill(const cytnx_complex128 &val);
742 void fill(const cytnx_complex64 &val);
743 void fill(const cytnx_double &val);
744 void fill(const cytnx_float &val);
745 void fill(const cytnx_int64 &val);
746 void fill(const cytnx_uint64 &val);
747 void fill(const cytnx_int32 &val);
748 void fill(const cytnx_uint32 &val);
749 void fill(const cytnx_int16 &val);
750 void fill(const cytnx_uint16 &val);
751 void fill(const cytnx_bool &val);
752 void set_zeros();
753 void resize(const cytnx_uint64 &newsize);
754
755 void append(const Scalar &val);
756 void append(const cytnx_complex128 &val);
757 void append(const cytnx_complex64 &val);
758 void append(const cytnx_double &val);
759 void append(const cytnx_float &val);
760 void append(const cytnx_int64 &val);
761 void append(const cytnx_uint64 &val);
762 void append(const cytnx_int32 &val);
763 void append(const cytnx_uint32 &val);
764 void append(const cytnx_int16 &val);
765 void append(const cytnx_uint16 &val);
766 void append(const cytnx_bool &val);
767 Scalar get_item(const cytnx_uint64 &in) const;
768
769
770 void set_item(const cytnx_uint64 &idx,const Scalar &val);
771 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
772 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
773 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
774 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
775 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
776 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
777 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
778 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
779 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
780 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
781 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
782
783
784
785 };
787
789 class Int16Storage : public Storage_base{
790 public:
791 Int16Storage(){this->dtype=Type.Int16;};
792 void Init(const unsigned long long &len_in, const int &device=-1);
793 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
794 boost::intrusive_ptr<Storage_base> _create_new_sametype();
795 boost::intrusive_ptr<Storage_base> clone();
796 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
797 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
798 void to_(const int &device);
799 boost::intrusive_ptr<Storage_base> to(const int &device);
800 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
801 void print_elems();
802
803 boost::intrusive_ptr<Storage_base> real();
804 boost::intrusive_ptr<Storage_base> imag();
805
806 //generators:
807 void fill(const cytnx_complex128 &val);
808 void fill(const cytnx_complex64 &val);
809 void fill(const cytnx_double &val);
810 void fill(const cytnx_float &val);
811 void fill(const cytnx_int64 &val);
812 void fill(const cytnx_uint64 &val);
813 void fill(const cytnx_int32 &val);
814 void fill(const cytnx_uint32 &val);
815 void fill(const cytnx_int16 &val);
816 void fill(const cytnx_uint16 &val);
817 void fill(const cytnx_bool &val);
818 void set_zeros();
819 void resize(const cytnx_uint64 &newsize);
820 void append(const Scalar &val);
821 void append(const cytnx_complex128 &val);
822 void append(const cytnx_complex64 &val);
823 void append(const cytnx_double &val);
824 void append(const cytnx_float &val);
825 void append(const cytnx_int64 &val);
826 void append(const cytnx_uint64 &val);
827 void append(const cytnx_int32 &val);
828 void append(const cytnx_uint32 &val);
829 void append(const cytnx_int16 &val);
830 void append(const cytnx_uint16 &val);
831 void append(const cytnx_bool &val);
832 Scalar get_item(const cytnx_uint64 &in) const;
833
834
835 void set_item(const cytnx_uint64 &idx,const Scalar &val);
836 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
837 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
838 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
839 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
840 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
841 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
842 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
843 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
844 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
845 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
846 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
847
848
849
850
851 };
853
855 class BoolStorage : public Storage_base{
856 public:
857 BoolStorage(){this->dtype=Type.Bool;};
858 void Init(const unsigned long long &len_in, const int &device=-1);
859 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device=-1, const bool &iscap=false, const unsigned long long &cap_in=0);
860 boost::intrusive_ptr<Storage_base> _create_new_sametype();
861 boost::intrusive_ptr<Storage_base> clone();
862 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
863 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
864 void to_(const int &device);
865 boost::intrusive_ptr<Storage_base> to(const int &device);
866 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
867 void print_elems();
868
869 boost::intrusive_ptr<Storage_base> real();
870 boost::intrusive_ptr<Storage_base> imag();
871
872 //generators:
873 void fill(const cytnx_complex128 &val);
874 void fill(const cytnx_complex64 &val);
875 void fill(const cytnx_double &val);
876 void fill(const cytnx_float &val);
877 void fill(const cytnx_int64 &val);
878 void fill(const cytnx_uint64 &val);
879 void fill(const cytnx_int32 &val);
880 void fill(const cytnx_uint32 &val);
881 void fill(const cytnx_int16 &val);
882 void fill(const cytnx_uint16 &val);
883 void fill(const cytnx_bool &val);
884 void set_zeros();
885 void resize(const cytnx_uint64 &newsize);
886 void append(const Scalar &val);
887 void append(const cytnx_complex128 &val);
888 void append(const cytnx_complex64 &val);
889 void append(const cytnx_double &val);
890 void append(const cytnx_float &val);
891 void append(const cytnx_int64 &val);
892 void append(const cytnx_uint64 &val);
893 void append(const cytnx_int32 &val);
894 void append(const cytnx_uint32 &val);
895 void append(const cytnx_int16 &val);
896 void append(const cytnx_uint16 &val);
897 void append(const cytnx_bool &val);
898 Scalar get_item(const cytnx_uint64 &in) const;
899
900
901 void set_item(const cytnx_uint64 &idx,const Scalar &val);
902 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
903 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
904 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
905 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
906 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
907 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
908 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
909 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
910 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
911 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
912 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
913
914
915
916 };
918
919
921 typedef boost::intrusive_ptr<Storage_base> (*pStorage_init)();
923
925 class Storage_init_interface: public Type_class{
926 public:
927 std::vector<pStorage_init> USIInit;
928 Storage_init_interface();
929 };
930 extern Storage_init_interface __SII;
932
934 class Storage{
935 private:
936 //Interface:
937 //Storage_init_interface __SII;
938
939 public:
941 boost::intrusive_ptr<Storage_base> _impl;
943
944
945
962 void Init(const unsigned long long &size,const unsigned int &dtype=Type.Double, int device=-1){
963 cytnx_error_msg(dtype>=N_Type,"%s","[ERROR] invalid argument: dtype");
964 this->_impl = __SII.USIInit[dtype]();
965 this->_impl->Init(size,device);
966 }
967 Storage(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1): _impl(new Storage_base()){
969 }
970 Storage(): _impl(new Storage_base()){};
972 Storage(boost::intrusive_ptr<Storage_base> in_impl){
973 this->_impl = in_impl;
974 }
975 Storage(const Storage &rhs){
976 this->_impl = rhs._impl;
977 }
978
979 template<class Tp>
980 Storage(const std::vector<Tp> &rhs){
981 this->_from_vector(rhs,-1);
982 }
983 template<class Tp>
984 Storage(const std::initializer_list<Tp> &rhs){
985 this->_from_vector(std::vector<Tp>(rhs),-1);
986 }
987
988
989 Storage& operator=(const Storage &rhs){
990 this->_impl = rhs._impl;
991 return *this;
992 }
993
994
995
997
999 void _Save(std::fstream &f) const;
1000 void _Load(std::fstream &f);
1001 void _Loadbinary(std::fstream &f, const unsigned int &dtype, const cytnx_uint64 &Nelem);
1002 void _Savebinary(std::fstream &f) const;
1003
1005
1014 void Save(const std::string &fname) const;
1015 void Save(const char* fname) const;
1016 void Tofile(const std::string &fname) const;
1017 void Tofile(const char* fname) const;
1018 void Tofile(std::fstream &f) const;
1019
1028 static Storage Load(const std::string &fname);
1029 static Storage Load(const char* fname);
1030 static Storage Fromfile(const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1);
1031 static Storage Fromfile(const char* fname, const unsigned int &dtype, const cytnx_int64 &count=-1);
1032
1052 Storage astype(const unsigned int &new_type) const{
1053 return this->_impl->astype(new_type);
1054 }
1055
1061 const unsigned int &dtype() const{
1062 return this->_impl->dtype;
1063 }
1064
1070 const std::string dtype_str() const{
1071 std::string out = this->_impl->dtype_str();
1072 return out;
1073 }
1079 const int &device() const{
1080 return this->_impl->device;
1081 }
1082
1088 const std::string device_str() const{
1089 std::string out = this->_impl->device_str();
1090 return out;
1091 }
1092
1100 template<class T>
1101 void append(const T &val){
1102 return this->_impl->append(val);
1103 }
1104
1106 template<class T> // this is c++ only
1107 T& at(const cytnx_uint64 &idx) const{
1108 return this->_impl->at<T>(idx);
1109 }
1110
1111 const Scalar::Sproxy at(const cytnx_uint64 &idx) const{
1112 Scalar::Sproxy out(this->_impl,idx);
1113 return out;
1114 }
1115 Scalar::Sproxy at(const cytnx_uint64 &idx){
1116 Scalar::Sproxy out(this->_impl,idx);
1117 return out;
1118 }
1119
1120 template<class T> // this is c++ only
1121 T& back() const{
1122 return this->_impl->back<T>();
1123 }
1124
1125 const Scalar::Sproxy back() const{
1126 Scalar::Sproxy out(this->_impl,this->size()-1);
1127 return out;
1128 }
1129 Scalar::Sproxy back(){
1130 Scalar::Sproxy out(this->_impl,this->size()-1);
1131 return out;
1132 }
1133
1134
1135 template<class T> // this is c++ only
1136 T* data() const{
1137 return this->_impl->data<T>();
1138 }
1139
1140 void* data() const{
1141 return this->_impl->data();
1142 }
1144
1151 this->_impl->resize(newsize);
1152 }
1153
1154
1161 void to_(const int &device){
1162 this->_impl->to_(device);
1163 }
1164
1174 Storage to(const int &device){
1175 return Storage(this->_impl->to(device));
1176 }
1193 return Storage(this->_impl->clone());
1194 }
1195
1201 const unsigned long long &size() const{
1202 return this->_impl->len;
1203 }
1204
1210 const unsigned long long &capacity() const{
1211 return this->_impl->cap;
1212 }
1213
1218 void print_info() const{
1219 this->_impl->print_info();
1220 }
1222 // this is a redundant function
1223 void print() const{
1224 this->_impl->print();
1225 }
1227
1236 this->_impl->set_zeros();
1237 }
1238
1255 bool operator==(const Storage &rhs);
1256 bool operator!=(const Storage &rhs);
1257
1265 template<class T>
1266 void fill(const T& val){
1267 this->_impl->fill(val);
1268 }
1269
1270
1271 /*
1272 @brief renew/create a Storage using c++ vector.
1273 @param vin the C++ vector with supported types.
1274
1275 [Note]
1276 This function is C++ only
1277 */
1278 template<class T>
1279 static Storage from_vector(const std::vector<T> &vin,const int device=-1){
1280 Storage out;
1281 out._from_vector(vin,device);
1282 return out;
1283 }
1284
1285 /*
1286 @brief convert a Storage to C++ vector.
1287
1288 [Note]
1289 This function is C++ only
1290 */
1291 template<class T>
1292 std::vector<T> vector(){
1293 T tmp;
1294 cytnx_error_msg(Type.cy_typeid(tmp)!=this->dtype(),"[ERROR] the dtype of current Storage does not match assigned vector type.%s","\n");
1295
1296 std::vector<T> out(this->size());
1297 Storage S;
1298 if(this->device()!=Device.cpu){
1299 S = this->to(Device.cpu);
1300 memcpy(&out[0],S.data(), sizeof(T)*this->size());
1301 }else{
1302 memcpy(&out[0],this->data(), sizeof(T)*this->size());
1303 }
1304
1305 return out;
1306
1307 }
1308
1309
1311
1312 template<class T>
1313 void _from_vector(const std::vector<T> &vin,const int device=-1){
1314 //auto dispatch:
1315 //check:
1316 cytnx_error_msg(1,"[FATAL] ERROR unsupport type%s","\n");
1317 //this->_impl->Init(vin.size(),device);
1318 //memcpy(this->_impl->Mem,&vin[0],sizeof(T)*vin.size());
1319 }
1320
1321 void _from_vector(const std::vector<cytnx_complex128> &vin, const int device=-1){
1322 this->_impl = __SII.USIInit[Type.ComplexDouble]();
1323 this->_impl->Init(vin.size(),device);
1324 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_complex128)*vin.size());
1325 }
1326 void _from_vector(const std::vector<cytnx_complex64> &vin, const int device=-1){
1327 this->_impl = __SII.USIInit[Type.ComplexFloat]();
1328 this->_impl->Init(vin.size(),device);
1329 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_complex64)*vin.size());
1330 }
1331 void _from_vector(const std::vector<cytnx_double> &vin, const int device=-1){
1332 this->_impl = __SII.USIInit[Type.Double]();
1333 this->_impl->Init(vin.size(),device);
1334 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_double)*vin.size());
1335 }
1336 void _from_vector(const std::vector<cytnx_float> &vin, const int device=-1){
1337 this->_impl = __SII.USIInit[Type.Float]();
1338 this->_impl->Init(vin.size(),device);
1339 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_float)*vin.size());
1340 }
1341 void _from_vector(const std::vector<cytnx_uint64> &vin, const int device=-1){
1342 this->_impl = __SII.USIInit[Type.Uint64]();
1343 this->_impl->Init(vin.size(),device);
1344 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_uint64)*vin.size());
1345 }
1346 void _from_vector(const std::vector<cytnx_int64> &vin, const int device=-1){
1347 this->_impl = __SII.USIInit[Type.Int64]();
1348 this->_impl->Init(vin.size(),device);
1349 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_int64)*vin.size());
1350 }
1351 void _from_vector(const std::vector<cytnx_uint32> &vin, const int device=-1){
1352 this->_impl = __SII.USIInit[Type.Uint32]();
1353 this->_impl->Init(vin.size(),device);
1354 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_uint32)*vin.size());
1355 }
1356 void _from_vector(const std::vector<cytnx_int32> &vin, const int device=-1){
1357 this->_impl = __SII.USIInit[Type.Int32]();
1358 this->_impl->Init(vin.size(),device);
1359 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_int32)*vin.size());
1360 }
1361 void _from_vector(const std::vector<cytnx_uint16> &vin, const int device=-1){
1362 this->_impl = __SII.USIInit[Type.Uint16]();
1363 this->_impl->Init(vin.size(),device);
1364 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_uint16)*vin.size());
1365 }
1366 void _from_vector(const std::vector<cytnx_int16> &vin, const int device=-1){
1367 this->_impl = __SII.USIInit[Type.Int16]();
1368 this->_impl->Init(vin.size(),device);
1369 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_int16)*vin.size());
1370 }
1371 void _from_vector(const std::vector<cytnx_bool> &vin, const int device=-1){
1372 this->_impl = __SII.USIInit[Type.Bool]();
1373 this->_impl->Init(vin.size(),device);
1374 this->_impl->_cpy_bool(this->_impl->Mem,vin);
1375 //memcpy(this->_impl->Mem,vin.data(),sizeof(cytnx_bool)*vin.size());
1376 }
1378
1379
1395 Storage real() const{
1396 return Storage(this->_impl->real());
1397 };
1413 Storage imag() const{
1414 return Storage(this->_impl->imag());
1415 };
1416
1418 return this->_impl->get_item(idx);
1419 };
1420
1421 template<class T>
1422 void set_item(const cytnx_uint64 &idx, const T &elem){
1423 this->_impl->set_item(idx,elem);
1424 };
1425
1426
1427 Scalar::Sproxy operator()(const cytnx_uint64 &idx);
1428
1429 };
1430
1432 std::ostream& operator<<(std::ostream& os, const Storage &in);
1434
1435}
1436
1437#endif
Definition Scalar.hpp:1751
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:934
void to_(const int &device)
move the current Storage to different deivce.
Definition Storage.hpp:1161
void append(const T &val)
append a value
Definition Storage.hpp:1101
const int & device() const
the device-id of current Storage
Definition Storage.hpp:1079
Storage real() const
Get the real part form a Complex type Storage.
Definition Storage.hpp:1395
std::vector< T > vector()
Definition Storage.hpp:1292
Storage(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
Definition Storage.hpp:967
Storage to(const int &device)
move a new Storage with same content as current Storage on different deivce.
Definition Storage.hpp:1174
void resize(const cytnx_uint64 &newsize)
resize the current Storage.
Definition Storage.hpp:1150
void Save(const std::string &fname) const
Save current Storage to file.
Definition Storage.cpp:92
void fill(const T &val)
set all the elements to the assigned value val
Definition Storage.hpp:1266
Storage()
Definition Storage.hpp:970
void Tofile(const std::string &fname) const
Definition Storage.cpp:111
const unsigned int & dtype() const
the dtype-id of current Storage
Definition Storage.hpp:1061
void set_item(const cytnx_uint64 &idx, const T &elem)
Definition Storage.hpp:1422
void Init(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
initialize a Storage
Definition Storage.hpp:962
const unsigned long long & capacity() const
the capacity ( no. of real elements in memory) in the Storage
Definition Storage.hpp:1210
void set_zeros()
set all the elements to zero.
Definition Storage.hpp:1235
static Storage Load(const std::string &fname)
Load current Storage from file.
Definition Storage.cpp:230
Storage astype(const unsigned int &new_type) const
cast the type of current Storage
Definition Storage.hpp:1052
const unsigned long long & size() const
the size ( no. of elements ) in the Storage
Definition Storage.hpp:1201
void print_info() const
print the info of the Storage, including the device, dtype and size.
Definition Storage.hpp:1218
static Storage from_vector(const std::vector< T > &vin, const int device=-1)
Definition Storage.hpp:1279
bool operator!=(const Storage &rhs)
Definition Storage.cpp:87
static Storage Fromfile(const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
Definition Storage.cpp:193
const std::string dtype_str() const
the dtype (std::string) of current Storage
Definition Storage.hpp:1070
Scalar::Sproxy operator()(const cytnx_uint64 &idx)
Definition Storage.cpp:315
void Tofile(std::fstream &f) const
bool operator==(const Storage &rhs)
compare two Storage
Definition Storage.cpp:20
Scalar get_item(const cytnx_uint64 &idx) const
Definition Storage.hpp:1417
Storage imag() const
Get the imaginary part form a Complex type Storage.
Definition Storage.hpp:1413
const std::string device_str() const
the device (std::string) of current Storage
Definition Storage.hpp:1088
Storage clone() const
return a copy of current storage.
Definition Storage.hpp:1192
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
Device_class Device
Definition Device.cpp:105
double cytnx_double
Definition Type.hpp:20
uint32_t cytnx_uint32
Definition Type.hpp:23
std::complex< double > cytnx_complex128
Definition Type.hpp:30
float cytnx_float
Definition Type.hpp:21
std::ostream & operator<<(std::ostream &os, const Scalar &in)
Definition Scalar.cpp:14
int16_t cytnx_int16
Definition Type.hpp:27
std::complex< float > cytnx_complex64
Definition Type.hpp:29
int32_t cytnx_int32
Definition Type.hpp:26
uint16_t cytnx_uint16
Definition Type.hpp:24
uint64_t cytnx_uint64
Definition Type.hpp:22
int64_t cytnx_int64
Definition Type.hpp:25
Storage_init_interface __SII
Definition Storage.cpp:13
Type_class Type
Definition Type.cpp:143