Cytnx v0.7.3
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 virtual void append(const cytnx_complex128 &val);
172 virtual void append(const cytnx_complex64 &val);
173 virtual void append(const cytnx_double &val);
174 virtual void append(const cytnx_float &val);
175 virtual void append(const cytnx_int64 &val);
176 virtual void append(const cytnx_uint64 &val);
177 virtual void append(const cytnx_int32 &val);
178 virtual void append(const cytnx_uint32 &val);
179 virtual void append(const cytnx_int16 &val);
180 virtual void append(const cytnx_uint16 &val);
181 virtual void append(const cytnx_bool &val);
182
183 virtual Scalar get_item(const cytnx_uint64 &in) const;
184
185 virtual void set_item(const cytnx_uint64 &idx,const Scalar &val);
186 virtual void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
187 virtual void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
188 virtual void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
189 virtual void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
190 virtual void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
191 virtual void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
192 virtual void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
193 virtual void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
194 virtual void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
195 virtual void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
196 virtual void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
197
198
199 };
201
203 class FloatStorage : public Storage_base{
204 public:
205 FloatStorage(){this->dtype=Type.Float;};
206 void Init(const unsigned long long &len_in, const int &device=-1);
207 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);
208 boost::intrusive_ptr<Storage_base> _create_new_sametype();
209 boost::intrusive_ptr<Storage_base> clone();
210 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);
211 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
212 void to_(const int &device);
213 boost::intrusive_ptr<Storage_base> to(const int &device);
214 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
215 void print_elems();
216
217 boost::intrusive_ptr<Storage_base> real();
218 boost::intrusive_ptr<Storage_base> imag();
219
220
221 //generators:
222 void fill(const cytnx_complex128 &val);
223 void fill(const cytnx_complex64 &val);
224 void fill(const cytnx_double &val);
225 void fill(const cytnx_float &val);
226 void fill(const cytnx_int64 &val);
227 void fill(const cytnx_uint64 &val);
228 void fill(const cytnx_int32 &val);
229 void fill(const cytnx_uint32 &val);
230 void fill(const cytnx_int16 &val);
231 void fill(const cytnx_uint16 &val);
232 void fill(const cytnx_bool &val);
233 void set_zeros();
234 void resize(const cytnx_uint64 &newsize);
235
236 void append(const cytnx_complex128 &val);
237 void append(const cytnx_complex64 &val);
238 void append(const cytnx_double &val);
239 void append(const cytnx_float &val);
240 void append(const cytnx_int64 &val);
241 void append(const cytnx_uint64 &val);
242 void append(const cytnx_int32 &val);
243 void append(const cytnx_uint32 &val);
244 void append(const cytnx_int16 &val);
245 void append(const cytnx_uint16 &val);
246 void append(const cytnx_bool &val);
247 Scalar get_item(const cytnx_uint64 &in) const;
248
249
250 void set_item(const cytnx_uint64 &idx,const Scalar &val);
251 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
252 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
253 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
254 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
255 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
256 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
257 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
258 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
259 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
260 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
261 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
262
263
264 };
266
268 class DoubleStorage: public Storage_base{
269 public:
270 DoubleStorage(){this->dtype=Type.Double;};
271 void Init(const unsigned long long &len_in,const int &device=-1);
272 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);
273 boost::intrusive_ptr<Storage_base> _create_new_sametype();
274 boost::intrusive_ptr<Storage_base> clone();
275 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);
276 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
277 void to_(const int &device);
278 boost::intrusive_ptr<Storage_base> to(const int &device);
279 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
280 void print_elems();
281
282 boost::intrusive_ptr<Storage_base> real();
283 boost::intrusive_ptr<Storage_base> imag();
284
285 //generators:
286 void fill(const cytnx_complex128 &val);
287 void fill(const cytnx_complex64 &val);
288 void fill(const cytnx_double &val);
289 void fill(const cytnx_float &val);
290 void fill(const cytnx_int64 &val);
291 void fill(const cytnx_uint64 &val);
292 void fill(const cytnx_int32 &val);
293 void fill(const cytnx_uint32 &val);
294 void fill(const cytnx_int16 &val);
295 void fill(const cytnx_uint16 &val);
296 void fill(const cytnx_bool &val);
297 void set_zeros();
298 void resize(const cytnx_uint64 &newsize);
299
300 void append(const cytnx_complex128 &val);
301 void append(const cytnx_complex64 &val);
302 void append(const cytnx_double &val);
303 void append(const cytnx_float &val);
304 void append(const cytnx_int64 &val);
305 void append(const cytnx_uint64 &val);
306 void append(const cytnx_int32 &val);
307 void append(const cytnx_uint32 &val);
308 void append(const cytnx_int16 &val);
309 void append(const cytnx_uint16 &val);
310 void append(const cytnx_bool &val);
311 Scalar get_item(const cytnx_uint64 &in) const;
312
313 void set_item(const cytnx_uint64 &idx,const Scalar &val);
314 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
315 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
316 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
317 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
318 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
319 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
320 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
321 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
322 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
323 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
324 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
325
326 };
328
330 class ComplexDoubleStorage: public Storage_base{
331 public:
332 ComplexDoubleStorage(){this->dtype=Type.ComplexDouble;};
333 void Init(const unsigned long long &len_in, const int &device=-1);
334 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);
335 boost::intrusive_ptr<Storage_base> _create_new_sametype();
336 boost::intrusive_ptr<Storage_base> clone();
337 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);
338 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
339 void to_(const int &device);
340 boost::intrusive_ptr<Storage_base> to(const int &device);
341 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
342 void print_elems();
343
344 boost::intrusive_ptr<Storage_base> real();
345 boost::intrusive_ptr<Storage_base> imag();
346
347 //generators:
348 void fill(const cytnx_complex128 &val);
349 void fill(const cytnx_complex64 &val);
350 void fill(const cytnx_double &val);
351 void fill(const cytnx_float &val);
352 void fill(const cytnx_int64 &val);
353 void fill(const cytnx_uint64 &val);
354 void fill(const cytnx_int32 &val);
355 void fill(const cytnx_uint32 &val);
356 void fill(const cytnx_int16 &val);
357 void fill(const cytnx_uint16 &val);
358 void fill(const cytnx_bool &val);
359 void set_zeros();
360 void resize(const cytnx_uint64 &newsize);
361
362
363 void append(const cytnx_complex128 &val);
364 void append(const cytnx_complex64 &val);
365 void append(const cytnx_double &val);
366 void append(const cytnx_float &val);
367 void append(const cytnx_int64 &val);
368 void append(const cytnx_uint64 &val);
369 void append(const cytnx_int32 &val);
370 void append(const cytnx_uint32 &val);
371 void append(const cytnx_int16 &val);
372 void append(const cytnx_uint16 &val);
373 void append(const cytnx_bool &val);
374 Scalar get_item(const cytnx_uint64 &in) const;
375
376
377 void set_item(const cytnx_uint64 &idx,const Scalar &val);
378 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
379 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
380 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
381 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
382 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
383 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
384 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
385 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
386 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
387 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
388 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
389
390 };
392
394 class ComplexFloatStorage: public Storage_base{
395 public:
396 ComplexFloatStorage(){this->dtype=Type.ComplexFloat;};
397 void Init(const unsigned long long &len_in,const int &device=-1);
398 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);
399 boost::intrusive_ptr<Storage_base> _create_new_sametype();
400 boost::intrusive_ptr<Storage_base> clone();
401 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);
402 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
403 void to_(const int &device);
404 boost::intrusive_ptr<Storage_base> to(const int &device);
405 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
406 void print_elems();
407
408 boost::intrusive_ptr<Storage_base> real();
409 boost::intrusive_ptr<Storage_base> imag();
410
411 //generators:
412 void fill(const cytnx_complex128 &val);
413 void fill(const cytnx_complex64 &val);
414 void fill(const cytnx_double &val);
415 void fill(const cytnx_float &val);
416 void fill(const cytnx_int64 &val);
417 void fill(const cytnx_uint64 &val);
418 void fill(const cytnx_int32 &val);
419 void fill(const cytnx_uint32 &val);
420 void fill(const cytnx_int16 &val);
421 void fill(const cytnx_uint16 &val);
422 void fill(const cytnx_bool &val);
423 void set_zeros();
424 void resize(const cytnx_uint64 &newsize);
425
426 void append(const cytnx_complex128 &val);
427 void append(const cytnx_complex64 &val);
428 void append(const cytnx_double &val);
429 void append(const cytnx_float &val);
430 void append(const cytnx_int64 &val);
431 void append(const cytnx_uint64 &val);
432 void append(const cytnx_int32 &val);
433 void append(const cytnx_uint32 &val);
434 void append(const cytnx_int16 &val);
435 void append(const cytnx_uint16 &val);
436 void append(const cytnx_bool &val);
437 Scalar get_item(const cytnx_uint64 &in) const;
438
439 void set_item(const cytnx_uint64 &idx,const Scalar &val);
440 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
441 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
442 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
443 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
444 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
445 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
446 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
447 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
448 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
449 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
450 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
451
452
453 };
455
457 class Int64Storage : public Storage_base{
458 public:
459 Int64Storage(){this->dtype=Type.Int64;};
460 void Init(const unsigned long long &len_in, const int &device=-1);
461 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);
462 boost::intrusive_ptr<Storage_base> _create_new_sametype();
463 boost::intrusive_ptr<Storage_base> clone();
464 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);
465 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
466 void to_(const int &device);
467 boost::intrusive_ptr<Storage_base> to(const int &device);
468 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
469 void print_elems();
470
471 boost::intrusive_ptr<Storage_base> real();
472 boost::intrusive_ptr<Storage_base> imag();
473
474
475 //generators:
476 void fill(const cytnx_complex128 &val);
477 void fill(const cytnx_complex64 &val);
478 void fill(const cytnx_double &val);
479 void fill(const cytnx_float &val);
480 void fill(const cytnx_int64 &val);
481 void fill(const cytnx_uint64 &val);
482 void fill(const cytnx_int32 &val);
483 void fill(const cytnx_uint32 &val);
484 void fill(const cytnx_int16 &val);
485 void fill(const cytnx_uint16 &val);
486 void fill(const cytnx_bool &val);
487 void set_zeros();
488 void resize(const cytnx_uint64 &newsize);
489
490 void append(const cytnx_complex128 &val);
491 void append(const cytnx_complex64 &val);
492 void append(const cytnx_double &val);
493 void append(const cytnx_float &val);
494 void append(const cytnx_int64 &val);
495 void append(const cytnx_uint64 &val);
496 void append(const cytnx_int32 &val);
497 void append(const cytnx_uint32 &val);
498 void append(const cytnx_int16 &val);
499 void append(const cytnx_uint16 &val);
500 void append(const cytnx_bool &val);
501 Scalar get_item(const cytnx_uint64 &in) const;
502
503
504 void set_item(const cytnx_uint64 &idx,const Scalar &val);
505 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
506 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
507 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
508 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
509 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
510 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
511 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
512 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
513 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
514 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
515 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
516
517
518 };
520
522 class Uint64Storage : public Storage_base{
523 public:
524 Uint64Storage(){this->dtype=Type.Uint64;};
525 void Init(const unsigned long long &len_in, const int &device=-1);
526 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);
527 boost::intrusive_ptr<Storage_base> _create_new_sametype();
528 boost::intrusive_ptr<Storage_base> clone();
529 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);
530 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
531 void to_(const int &device);
532 boost::intrusive_ptr<Storage_base> to(const int &device);
533 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
534 void print_elems();
535
536
537 boost::intrusive_ptr<Storage_base> real();
538 boost::intrusive_ptr<Storage_base> imag();
539
540 //generators:
541 void fill(const cytnx_complex128 &val);
542 void fill(const cytnx_complex64 &val);
543 void fill(const cytnx_double &val);
544 void fill(const cytnx_float &val);
545 void fill(const cytnx_int64 &val);
546 void fill(const cytnx_uint64 &val);
547 void fill(const cytnx_int32 &val);
548 void fill(const cytnx_uint32 &val);
549 void fill(const cytnx_int16 &val);
550 void fill(const cytnx_uint16 &val);
551 void fill(const cytnx_bool &val);
552 void set_zeros();
553 void resize(const cytnx_uint64 &newsize);
554
555 void append(const cytnx_complex128 &val);
556 void append(const cytnx_complex64 &val);
557 void append(const cytnx_double &val);
558 void append(const cytnx_float &val);
559 void append(const cytnx_int64 &val);
560 void append(const cytnx_uint64 &val);
561 void append(const cytnx_int32 &val);
562 void append(const cytnx_uint32 &val);
563 void append(const cytnx_int16 &val);
564 void append(const cytnx_uint16 &val);
565 void append(const cytnx_bool &val);
566 Scalar get_item(const cytnx_uint64 &in) const;
567
568
569 void set_item(const cytnx_uint64 &idx,const Scalar &val);
570 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
571 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
572 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
573 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
574 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
575 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
576 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
577 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
578 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
579 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
580 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
581
582
583 };
586 class Int32Storage : public Storage_base{
587 public:
588 Int32Storage(){this->dtype=Type.Int32;};
589 void Init(const unsigned long long &len_in, const int &device=-1);
590 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);
591 boost::intrusive_ptr<Storage_base> _create_new_sametype();
592 boost::intrusive_ptr<Storage_base> clone();
593 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);
594 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
595 void to_(const int &device);
596 boost::intrusive_ptr<Storage_base> to(const int &device);
597 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
598 void print_elems();
599
600 boost::intrusive_ptr<Storage_base> real();
601 boost::intrusive_ptr<Storage_base> imag();
602
603 //generators:
604 void fill(const cytnx_complex128 &val);
605 void fill(const cytnx_complex64 &val);
606 void fill(const cytnx_double &val);
607 void fill(const cytnx_float &val);
608 void fill(const cytnx_int64 &val);
609 void fill(const cytnx_uint64 &val);
610 void fill(const cytnx_int32 &val);
611 void fill(const cytnx_uint32 &val);
612 void fill(const cytnx_int16 &val);
613 void fill(const cytnx_uint16 &val);
614 void fill(const cytnx_bool &val);
615 void set_zeros();
616 void resize(const cytnx_uint64 &newsize);
617 void append(const cytnx_complex128 &val);
618 void append(const cytnx_complex64 &val);
619 void append(const cytnx_double &val);
620 void append(const cytnx_float &val);
621 void append(const cytnx_int64 &val);
622 void append(const cytnx_uint64 &val);
623 void append(const cytnx_int32 &val);
624 void append(const cytnx_uint32 &val);
625 void append(const cytnx_int16 &val);
626 void append(const cytnx_uint16 &val);
627 void append(const cytnx_bool &val);
628 Scalar get_item(const cytnx_uint64 &in) const;
629
630
631 void set_item(const cytnx_uint64 &idx,const Scalar &val);
632 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
633 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
634 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
635 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
636 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
637 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
638 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
639 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
640 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
641 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
642 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
643
644
645 };
647
649 class Uint32Storage : public Storage_base{
650 public:
651 Uint32Storage(){this->dtype=Type.Uint32;};
652 void Init(const unsigned long long &len_in, const int &device=-1);
653 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);
654 boost::intrusive_ptr<Storage_base> _create_new_sametype();
655 boost::intrusive_ptr<Storage_base> clone();
656 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);
657 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
658 void to_(const int &device);
659 boost::intrusive_ptr<Storage_base> to(const int &device);
660 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
661 void print_elems();
662
663 boost::intrusive_ptr<Storage_base> real();
664 boost::intrusive_ptr<Storage_base> imag();
665
666
667 //generators:
668 void fill(const cytnx_complex128 &val);
669 void fill(const cytnx_complex64 &val);
670 void fill(const cytnx_double &val);
671 void fill(const cytnx_float &val);
672 void fill(const cytnx_int64 &val);
673 void fill(const cytnx_uint64 &val);
674 void fill(const cytnx_int32 &val);
675 void fill(const cytnx_uint32 &val);
676 void fill(const cytnx_int16 &val);
677 void fill(const cytnx_uint16 &val);
678 void fill(const cytnx_bool &val);
679 void set_zeros();
680 void resize(const cytnx_uint64 &newsize);
681 void append(const cytnx_complex128 &val);
682 void append(const cytnx_complex64 &val);
683 void append(const cytnx_double &val);
684 void append(const cytnx_float &val);
685 void append(const cytnx_int64 &val);
686 void append(const cytnx_uint64 &val);
687 void append(const cytnx_int32 &val);
688 void append(const cytnx_uint32 &val);
689 void append(const cytnx_int16 &val);
690 void append(const cytnx_uint16 &val);
691 void append(const cytnx_bool &val);
692 Scalar get_item(const cytnx_uint64 &in) const;
693
694 void set_item(const cytnx_uint64 &idx,const Scalar &val);
695 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
696 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
697 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
698 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
699 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
700 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
701 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
702 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
703 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
704 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
705 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
706
707 };
709
711 class Uint16Storage : public Storage_base{
712 public:
713 Uint16Storage(){this->dtype=Type.Uint16;};
714 void Init(const unsigned long long &len_in, const int &device=-1);
715 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);
716 boost::intrusive_ptr<Storage_base> _create_new_sametype();
717 boost::intrusive_ptr<Storage_base> clone();
718 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);
719 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
720 void to_(const int &device);
721 boost::intrusive_ptr<Storage_base> to(const int &device);
722 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
723 void print_elems();
724
725 boost::intrusive_ptr<Storage_base> real();
726 boost::intrusive_ptr<Storage_base> imag();
727
728 //generators:
729 void fill(const cytnx_complex128 &val);
730 void fill(const cytnx_complex64 &val);
731 void fill(const cytnx_double &val);
732 void fill(const cytnx_float &val);
733 void fill(const cytnx_int64 &val);
734 void fill(const cytnx_uint64 &val);
735 void fill(const cytnx_int32 &val);
736 void fill(const cytnx_uint32 &val);
737 void fill(const cytnx_int16 &val);
738 void fill(const cytnx_uint16 &val);
739 void fill(const cytnx_bool &val);
740 void set_zeros();
741 void resize(const cytnx_uint64 &newsize);
742 void append(const cytnx_complex128 &val);
743 void append(const cytnx_complex64 &val);
744 void append(const cytnx_double &val);
745 void append(const cytnx_float &val);
746 void append(const cytnx_int64 &val);
747 void append(const cytnx_uint64 &val);
748 void append(const cytnx_int32 &val);
749 void append(const cytnx_uint32 &val);
750 void append(const cytnx_int16 &val);
751 void append(const cytnx_uint16 &val);
752 void append(const cytnx_bool &val);
753 Scalar get_item(const cytnx_uint64 &in) const;
754
755
756 void set_item(const cytnx_uint64 &idx,const Scalar &val);
757 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
758 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
759 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
760 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
761 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
762 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
763 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
764 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
765 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
766 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
767 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
768
769
770
771 };
773
775 class Int16Storage : public Storage_base{
776 public:
777 Int16Storage(){this->dtype=Type.Int16;};
778 void Init(const unsigned long long &len_in, const int &device=-1);
779 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);
780 boost::intrusive_ptr<Storage_base> _create_new_sametype();
781 boost::intrusive_ptr<Storage_base> clone();
782 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);
783 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
784 void to_(const int &device);
785 boost::intrusive_ptr<Storage_base> to(const int &device);
786 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
787 void print_elems();
788
789 boost::intrusive_ptr<Storage_base> real();
790 boost::intrusive_ptr<Storage_base> imag();
791
792 //generators:
793 void fill(const cytnx_complex128 &val);
794 void fill(const cytnx_complex64 &val);
795 void fill(const cytnx_double &val);
796 void fill(const cytnx_float &val);
797 void fill(const cytnx_int64 &val);
798 void fill(const cytnx_uint64 &val);
799 void fill(const cytnx_int32 &val);
800 void fill(const cytnx_uint32 &val);
801 void fill(const cytnx_int16 &val);
802 void fill(const cytnx_uint16 &val);
803 void fill(const cytnx_bool &val);
804 void set_zeros();
805 void resize(const cytnx_uint64 &newsize);
806 void append(const cytnx_complex128 &val);
807 void append(const cytnx_complex64 &val);
808 void append(const cytnx_double &val);
809 void append(const cytnx_float &val);
810 void append(const cytnx_int64 &val);
811 void append(const cytnx_uint64 &val);
812 void append(const cytnx_int32 &val);
813 void append(const cytnx_uint32 &val);
814 void append(const cytnx_int16 &val);
815 void append(const cytnx_uint16 &val);
816 void append(const cytnx_bool &val);
817 Scalar get_item(const cytnx_uint64 &in) const;
818
819
820 void set_item(const cytnx_uint64 &idx,const Scalar &val);
821 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
822 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
823 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
824 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
825 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
826 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
827 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
828 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
829 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
830 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
831 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
832
833
834
835
836 };
838
840 class BoolStorage : public Storage_base{
841 public:
842 BoolStorage(){this->dtype=Type.Bool;};
843 void Init(const unsigned long long &len_in, const int &device=-1);
844 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);
845 boost::intrusive_ptr<Storage_base> _create_new_sametype();
846 boost::intrusive_ptr<Storage_base> clone();
847 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);
848 void Move_memory_(const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper, const std::vector<cytnx_uint64> &invmapper);
849 void to_(const int &device);
850 boost::intrusive_ptr<Storage_base> to(const int &device);
851 void PrintElem_byShape(std::ostream& os, const std::vector<cytnx_uint64> &shape, const std::vector<cytnx_uint64> &mapper={});
852 void print_elems();
853
854 boost::intrusive_ptr<Storage_base> real();
855 boost::intrusive_ptr<Storage_base> imag();
856
857 //generators:
858 void fill(const cytnx_complex128 &val);
859 void fill(const cytnx_complex64 &val);
860 void fill(const cytnx_double &val);
861 void fill(const cytnx_float &val);
862 void fill(const cytnx_int64 &val);
863 void fill(const cytnx_uint64 &val);
864 void fill(const cytnx_int32 &val);
865 void fill(const cytnx_uint32 &val);
866 void fill(const cytnx_int16 &val);
867 void fill(const cytnx_uint16 &val);
868 void fill(const cytnx_bool &val);
869 void set_zeros();
870 void resize(const cytnx_uint64 &newsize);
871 void append(const cytnx_complex128 &val);
872 void append(const cytnx_complex64 &val);
873 void append(const cytnx_double &val);
874 void append(const cytnx_float &val);
875 void append(const cytnx_int64 &val);
876 void append(const cytnx_uint64 &val);
877 void append(const cytnx_int32 &val);
878 void append(const cytnx_uint32 &val);
879 void append(const cytnx_int16 &val);
880 void append(const cytnx_uint16 &val);
881 void append(const cytnx_bool &val);
882 Scalar get_item(const cytnx_uint64 &in) const;
883
884
885 void set_item(const cytnx_uint64 &idx,const Scalar &val);
886 void set_item(const cytnx_uint64 &idx,const cytnx_complex128 &val);
887 void set_item(const cytnx_uint64 &idx,const cytnx_complex64 &val);
888 void set_item(const cytnx_uint64 &idx,const cytnx_double &val);
889 void set_item(const cytnx_uint64 &idx,const cytnx_float &val);
890 void set_item(const cytnx_uint64 &idx,const cytnx_int64 &val);
891 void set_item(const cytnx_uint64 &idx,const cytnx_uint64 &val);
892 void set_item(const cytnx_uint64 &idx,const cytnx_int32 &val);
893 void set_item(const cytnx_uint64 &idx,const cytnx_uint32 &val);
894 void set_item(const cytnx_uint64 &idx,const cytnx_int16 &val);
895 void set_item(const cytnx_uint64 &idx,const cytnx_uint16 &val);
896 void set_item(const cytnx_uint64 &idx,const cytnx_bool &val);
897
898
899
900 };
902
903
905 typedef boost::intrusive_ptr<Storage_base> (*pStorage_init)();
907
909 class Storage_init_interface: public Type_class{
910 public:
911 std::vector<pStorage_init> USIInit;
912 Storage_init_interface();
913 };
914 extern Storage_init_interface __SII;
916
918 class Storage{
919 private:
920 //Interface:
921 //Storage_init_interface __SII;
922
923 public:
925 boost::intrusive_ptr<Storage_base> _impl;
927
928
929
946 void Init(const unsigned long long &size,const unsigned int &dtype=Type.Double, int device=-1){
947 cytnx_error_msg(dtype>=N_Type,"%s","[ERROR] invalid argument: dtype");
948 this->_impl = __SII.USIInit[dtype]();
949 this->_impl->Init(size,device);
950 }
951 Storage(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1): _impl(new Storage_base()){
953 }
954 Storage(): _impl(new Storage_base()){};
956 Storage(boost::intrusive_ptr<Storage_base> in_impl){
957 this->_impl = in_impl;
958 }
959 Storage(const Storage &rhs){
960 this->_impl = rhs._impl;
961 }
962
963 template<class Tp>
964 Storage(const std::vector<Tp> &rhs){
965 this->_from_vector(rhs,-1);
966 }
967 template<class Tp>
968 Storage(const std::initializer_list<Tp> &rhs){
969 this->_from_vector(std::vector<Tp>(rhs),-1);
970 }
971
972
973 Storage& operator=(const Storage &rhs){
974 this->_impl = rhs._impl;
975 return *this;
976 }
977
978
979
981
983 void _Save(std::fstream &f) const;
984 void _Load(std::fstream &f);
985 void _Loadbinary(std::fstream &f, const unsigned int &dtype, const cytnx_uint64 &Nelem);
986 void _Savebinary(std::fstream &f) const;
987
989
998 void Save(const std::string &fname) const;
999 void Save(const char* fname) const;
1000 void Tofile(const std::string &fname) const;
1001 void Tofile(const char* fname) const;
1002 void Tofile(std::fstream &f) const;
1003
1012 static Storage Load(const std::string &fname);
1013 static Storage Load(const char* fname);
1014 static Storage Fromfile(const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1);
1015 static Storage Fromfile(const char* fname, const unsigned int &dtype, const cytnx_int64 &count=-1);
1016
1036 Storage astype(const unsigned int &new_type) const{
1037 return this->_impl->astype(new_type);
1038 }
1039
1045 const unsigned int &dtype() const{
1046 return this->_impl->dtype;
1047 }
1048
1054 const std::string dtype_str() const{
1055 std::string out = this->_impl->dtype_str();
1056 return out;
1057 }
1063 const int &device() const{
1064 return this->_impl->device;
1065 }
1066
1072 const std::string device_str() const{
1073 std::string out = this->_impl->device_str();
1074 return out;
1075 }
1076
1084 template<class T>
1085 void append(const T &val){
1086 return this->_impl->append(val);
1087 }
1088
1090 template<class T> // this is c++ only
1091 T& at(const cytnx_uint64 &idx) const{
1092 return this->_impl->at<T>(idx);
1093 }
1094
1095 const Scalar::Sproxy at(const cytnx_uint64 &idx) const{
1096 Scalar::Sproxy out(this->_impl,idx);
1097 return out;
1098 }
1099 Scalar::Sproxy at(const cytnx_uint64 &idx){
1100 Scalar::Sproxy out(this->_impl,idx);
1101 return out;
1102 }
1103
1104 template<class T> // this is c++ only
1105 T& back() const{
1106 return this->_impl->back<T>();
1107 }
1108
1109 const Scalar::Sproxy back() const{
1110 Scalar::Sproxy out(this->_impl,this->size()-1);
1111 return out;
1112 }
1113 Scalar::Sproxy back(){
1114 Scalar::Sproxy out(this->_impl,this->size()-1);
1115 return out;
1116 }
1117
1118
1119 template<class T> // this is c++ only
1120 T* data() const{
1121 return this->_impl->data<T>();
1122 }
1123
1124 void* data() const{
1125 return this->_impl->data();
1126 }
1128
1135 this->_impl->resize(newsize);
1136 }
1137
1138
1145 void to_(const int &device){
1146 this->_impl->to_(device);
1147 }
1148
1158 Storage to(const int &device){
1159 return Storage(this->_impl->to(device));
1160 }
1177 return Storage(this->_impl->clone());
1178 }
1179
1185 const unsigned long long &size() const{
1186 return this->_impl->len;
1187 }
1188
1194 const unsigned long long &capacity() const{
1195 return this->_impl->cap;
1196 }
1197
1202 void print_info() const{
1203 this->_impl->print_info();
1204 }
1206 // this is a redundant function
1207 void print() const{
1208 this->_impl->print();
1209 }
1211
1220 this->_impl->set_zeros();
1221 }
1222
1239 bool operator==(const Storage &rhs);
1240 bool operator!=(const Storage &rhs);
1241
1249 template<class T>
1250 void fill(const T& val){
1251 this->_impl->fill(val);
1252 }
1253
1254
1255 /*
1256 @brief renew/create a Storage using c++ vector.
1257 @param vin the C++ vector with supported types.
1258
1259 [Note]
1260 This function is C++ only
1261 */
1262 template<class T>
1263 static Storage from_vector(const std::vector<T> &vin,const int device=-1){
1264 Storage out;
1265 out._from_vector(vin,device);
1266 return out;
1267 }
1268
1269
1270
1272
1273 template<class T>
1274 void _from_vector(const std::vector<T> &vin,const int device=-1){
1275 //auto dispatch:
1276 //check:
1277 cytnx_error_msg(1,"[FATAL] ERROR unsupport type%s","\n");
1278 //this->_impl->Init(vin.size(),device);
1279 //memcpy(this->_impl->Mem,&vin[0],sizeof(T)*vin.size());
1280 }
1281
1282 void _from_vector(const std::vector<cytnx_complex128> &vin, const int device=-1){
1283 this->_impl = __SII.USIInit[Type.ComplexDouble]();
1284 this->_impl->Init(vin.size(),device);
1285 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_complex128)*vin.size());
1286 }
1287 void _from_vector(const std::vector<cytnx_complex64> &vin, const int device=-1){
1288 this->_impl = __SII.USIInit[Type.ComplexFloat]();
1289 this->_impl->Init(vin.size(),device);
1290 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_complex64)*vin.size());
1291 }
1292 void _from_vector(const std::vector<cytnx_double> &vin, const int device=-1){
1293 this->_impl = __SII.USIInit[Type.Double]();
1294 this->_impl->Init(vin.size(),device);
1295 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_double)*vin.size());
1296 }
1297 void _from_vector(const std::vector<cytnx_float> &vin, const int device=-1){
1298 this->_impl = __SII.USIInit[Type.Float]();
1299 this->_impl->Init(vin.size(),device);
1300 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_float)*vin.size());
1301 }
1302 void _from_vector(const std::vector<cytnx_uint64> &vin, const int device=-1){
1303 this->_impl = __SII.USIInit[Type.Uint64]();
1304 this->_impl->Init(vin.size(),device);
1305 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_uint64)*vin.size());
1306 }
1307 void _from_vector(const std::vector<cytnx_int64> &vin, const int device=-1){
1308 this->_impl = __SII.USIInit[Type.Int64]();
1309 this->_impl->Init(vin.size(),device);
1310 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_int64)*vin.size());
1311 }
1312 void _from_vector(const std::vector<cytnx_uint32> &vin, const int device=-1){
1313 this->_impl = __SII.USIInit[Type.Uint32]();
1314 this->_impl->Init(vin.size(),device);
1315 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_uint32)*vin.size());
1316 }
1317 void _from_vector(const std::vector<cytnx_int32> &vin, const int device=-1){
1318 this->_impl = __SII.USIInit[Type.Int32]();
1319 this->_impl->Init(vin.size(),device);
1320 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_int32)*vin.size());
1321 }
1322 void _from_vector(const std::vector<cytnx_uint16> &vin, const int device=-1){
1323 this->_impl = __SII.USIInit[Type.Uint16]();
1324 this->_impl->Init(vin.size(),device);
1325 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_uint16)*vin.size());
1326 }
1327 void _from_vector(const std::vector<cytnx_int16> &vin, const int device=-1){
1328 this->_impl = __SII.USIInit[Type.Int16]();
1329 this->_impl->Init(vin.size(),device);
1330 memcpy(this->_impl->Mem,&vin[0],sizeof(cytnx_int16)*vin.size());
1331 }
1332 void _from_vector(const std::vector<cytnx_bool> &vin, const int device=-1){
1333 this->_impl = __SII.USIInit[Type.Bool]();
1334 this->_impl->Init(vin.size(),device);
1335 this->_impl->_cpy_bool(this->_impl->Mem,vin);
1336 //memcpy(this->_impl->Mem,vin.data(),sizeof(cytnx_bool)*vin.size());
1337 }
1339
1340
1356 Storage real() const{
1357 return Storage(this->_impl->real());
1358 };
1374 Storage imag() const{
1375 return Storage(this->_impl->imag());
1376 };
1377
1379 return this->_impl->get_item(idx);
1380 };
1381
1382 template<class T>
1383 void set_item(const cytnx_uint64 &idx, const T &elem){
1384 this->_impl->set_item(idx,elem);
1385 };
1386
1387
1388 //Sproxy operator()(const cytnx_uint64 &idx)
1389
1390
1391 };
1392
1394 std::ostream& operator<<(std::ostream& os, const Storage &in);
1396
1397}
1398
1399#endif
Definition Scalar.hpp:1630
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:918
void to_(const int &device)
move the current Storage to different deivce.
Definition Storage.hpp:1145
void append(const T &val)
append a value
Definition Storage.hpp:1085
const int & device() const
the device-id of current Storage
Definition Storage.hpp:1063
Storage real() const
Get the real part form a Complex type Storage.
Definition Storage.hpp:1356
Storage(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
Definition Storage.hpp:951
Storage to(const int &device)
move a new Storage with same content as current Storage on different deivce.
Definition Storage.hpp:1158
void resize(const cytnx_uint64 &newsize)
resize the current Storage.
Definition Storage.hpp:1134
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:1250
Storage()
Definition Storage.hpp:954
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:1045
void set_item(const cytnx_uint64 &idx, const T &elem)
Definition Storage.hpp:1383
void Init(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1)
initialize a Storage
Definition Storage.hpp:946
const unsigned long long & capacity() const
the capacity ( no. of real elements in memory) in the Storage
Definition Storage.hpp:1194
void set_zeros()
set all the elements to zero.
Definition Storage.hpp:1219
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:1036
const unsigned long long & size() const
the size ( no. of elements ) in the Storage
Definition Storage.hpp:1185
void print_info() const
print the info of the Storage, including the device, dtype and size.
Definition Storage.hpp:1202
static Storage from_vector(const std::vector< T > &vin, const int device=-1)
Definition Storage.hpp:1263
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:1054
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:1378
Storage imag() const
Get the imaginary part form a Complex type Storage.
Definition Storage.hpp:1374
const std::string device_str() const
the device (std::string) of current Storage
Definition Storage.hpp:1072
Storage clone() const
return a copy of current storage.
Definition Storage.hpp:1176
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
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:137