Cytnx v0.9.1
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#include "Type.hpp"
15#include "Device.hpp"
17#include "cytnx_error.hpp"
18#include "Scalar.hpp"
19
20#define STORAGE_DEFT_SZ 2
21
22namespace cytnx {
23
25 class Storage_base : public intrusive_ptr_base<Storage_base> {
26 public:
27 void *Mem;
28 // std::vector<unsigned int> shape;
29
30 unsigned long long len; // default 0
31 unsigned long long cap; // default 0
32 unsigned int dtype; // default 0, Void
33 int device; // default -1, on cpu
34
35 Storage_base() : cap(0), len(0), Mem(NULL), dtype(0), device(-1){};
36 // Storage_base(const std::initializer_list<unsigned int> &init_shape);
37 // Storage_base(const std::vector<unsigned int> &init_shape);
38 Storage_base(const unsigned long long &len_in, const int &device, const bool &init_zero = true);
39
40 Storage_base(Storage_base &Rhs);
41 Storage_base &operator=(Storage_base &Rhs);
42 boost::intrusive_ptr<Storage_base> astype(const unsigned int &dtype);
43
44 // void Init(const std::initializer_list<unsigned int> &init_shape);
45 std::string dtype_str() const;
46 std::string device_str() const;
47 const unsigned long long &capacity() const { return this->cap; }
48 const unsigned long long &size() const { return this->len; }
49 ~Storage_base();
50
51 template <class T>
52 T &at(const cytnx_uint64 &idx) const;
53
54 template <class T>
55 T &back() const;
56
57 template <class T>
58 T *data() const;
59
60 void *data() const { return this->Mem; }
61
62 void _cpy_bool(void *ptr, const std::vector<cytnx_bool> &vin);
63
64 void print();
65 void print_info();
66 /*
67 This function is design to check the type mismatch.
68 Handy for developer to exclude the assign of double
69 C pointer into a non-DoubleStorage.
70
71 For example:
72 float *cptr = (float*)calloc(4,sizeof(float));
73
74 intrusive_ptr<Storage> array(new DoubleStorage());
75 array->_Init_byptr((void*)cptr,4); // This is fatal, since we alloc cptr as float,
76 // but apon free, DoubleStorage will free 2x
77 // of memory!!!!
78
79 array->_Init_byptr_safe(cptr,4); // This is design to avoid the above problem
80 // by checking the type of input pointer with
81 // the type of Storage before call _Init_byptr.
82 // [Note] this will intorduce overhead!!.
83
84 */
85 template <class T>
86 void _Init_byptr_safe(T *rawptr, const unsigned long long &len_in) {
87 // check:
88 if (this->dtype == Type.Float) {
89 cytnx_error_msg(typeid(T) != typeid(cytnx_float), "%s",
90 "[ERROR _Init_byptr_safe type not match]");
91 } else if (this->dtype == Type.Double) {
92 cytnx_error_msg(typeid(T) != typeid(cytnx_double), "%s",
93 "[ERROR _Init_byptr_safe type not match]");
94 } else if (this->dtype == Type.Uint64) {
95 cytnx_error_msg(typeid(T) != typeid(cytnx_uint64), "%s",
96 "[ERROR _Init_byptr_safe type not match]");
97 } else if (this->dtype == Type.Uint32) {
98 cytnx_error_msg(typeid(T) != typeid(cytnx_uint32), "%s",
99 "[ERROR _Init_byptr_safe type not match]");
100 } else if (this->dtype == Type.Int64) {
101 cytnx_error_msg(typeid(T) != typeid(cytnx_int64), "%s",
102 "[ERROR _Init_byptr_safe type not match]");
103 } else if (this->dtype == Type.Int32) {
104 cytnx_error_msg(typeid(T) != typeid(cytnx_int32), "%s",
105 "[ERROR _Init_byptr_safe type not match]");
106 } else if (this->dtype == Type.ComplexDouble) {
107 cytnx_error_msg(typeid(T) != typeid(cytnx_complex128), "%s",
108 "[ERROR _Init_byptr_safe type not match]");
109 } else if (this->dtype == Type.ComplexFloat) {
110 cytnx_error_msg(typeid(T) != typeid(cytnx_complex64), "%s",
111 "[ERROR _Init_byptr_safe type not match]");
112 } else if (this->dtype == Type.Int16) {
113 cytnx_error_msg(typeid(T) != typeid(cytnx_int16), "%s",
114 "[ERROR _Init_byptr_safe type not match]");
115 } else if (this->dtype == Type.Uint16) {
116 cytnx_error_msg(typeid(T) != typeid(cytnx_uint16), "%s",
117 "[ERROR _Init_byptr_safe type not match]");
118 } else if (this->dtype == Type.Bool) {
119 cytnx_error_msg(typeid(T) != typeid(cytnx_bool), "%s",
120 "[ERROR _Init_byptr_safe type not match]");
121 } else {
122 cytnx_error_msg(1, "[FATAL] ERROR%s", "\n");
123 }
124
125 this->_Init_byptr((void *)rawptr, len_in);
126 }
127
128 void GetElem_byShape_v2(boost::intrusive_ptr<Storage_base> &out,
129 const std::vector<cytnx_uint64> &shape,
130 const std::vector<std::vector<cytnx_uint64>> &locators,
131 const cytnx_uint64 &Nunit);
132 void GetElem_byShape(boost::intrusive_ptr<Storage_base> &out,
133 const std::vector<cytnx_uint64> &shape,
134 const std::vector<cytnx_uint64> &mapper,
135 const std::vector<cytnx_uint64> &len,
136 const std::vector<std::vector<cytnx_uint64>> &locators);
137 void SetElem_byShape(boost::intrusive_ptr<Storage_base> &in,
138 const std::vector<cytnx_uint64> &shape,
139 const std::vector<cytnx_uint64> &mapper,
140 const std::vector<cytnx_uint64> &len,
141 const std::vector<std::vector<cytnx_uint64>> &locators,
142 const bool &is_scalar);
143 void SetElem_byShape_v2(boost::intrusive_ptr<Storage_base> &in,
144 const std::vector<cytnx_uint64> &shape,
145 const std::vector<std::vector<cytnx_uint64>> &locators,
146 const cytnx_uint64 &Nunit, const bool &is_scalar);
147 // these is the one that do the work, and customize with Storage_base
148 // virtual void Init(const std::vector<unsigned int> &init_shape);
149 virtual void Init(const unsigned long long &len_in, const int &device = -1,
150 const bool &init_zero = true);
151 virtual void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
152 const bool &iscap = false, const unsigned long long &cap_in = 0);
153
154 // this function will return a new storage with the same type as the one
155 // that initiate this function.
156 virtual boost::intrusive_ptr<Storage_base> _create_new_sametype();
157
158 // [future] this will move the memory to device / cpu
159 virtual void to_(const int &device);
160 virtual boost::intrusive_ptr<Storage_base> to(const int &device);
161
162 virtual boost::intrusive_ptr<Storage_base> clone();
163
164 // this will perform permute on the underlying memory.
165 virtual boost::intrusive_ptr<Storage_base> Move_memory(
166 const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper,
167 const std::vector<cytnx_uint64> &invmapper);
168 virtual void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
169 const std::vector<cytnx_uint64> &mapper,
170 const std::vector<cytnx_uint64> &invmapper);
171 virtual void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
172 const std::vector<cytnx_uint64> &mapper = {});
173 virtual void print_elems();
174
175 virtual boost::intrusive_ptr<Storage_base> real();
176 virtual boost::intrusive_ptr<Storage_base> imag();
177
178 // generators:
179 virtual void fill(const cytnx_complex128 &val);
180 virtual void fill(const cytnx_complex64 &val);
181 virtual void fill(const cytnx_double &val);
182 virtual void fill(const cytnx_float &val);
183 virtual void fill(const cytnx_int64 &val);
184 virtual void fill(const cytnx_uint64 &val);
185 virtual void fill(const cytnx_int32 &val);
186 virtual void fill(const cytnx_uint32 &val);
187 virtual void fill(const cytnx_int16 &val);
188 virtual void fill(const cytnx_uint16 &val);
189 virtual void fill(const cytnx_bool &val);
190 virtual void set_zeros();
191 virtual void resize(const cytnx_uint64 &newsize);
192
193 virtual void append(const Scalar &val);
194 virtual void append(const cytnx_complex128 &val);
195 virtual void append(const cytnx_complex64 &val);
196 virtual void append(const cytnx_double &val);
197 virtual void append(const cytnx_float &val);
198 virtual void append(const cytnx_int64 &val);
199 virtual void append(const cytnx_uint64 &val);
200 virtual void append(const cytnx_int32 &val);
201 virtual void append(const cytnx_uint32 &val);
202 virtual void append(const cytnx_int16 &val);
203 virtual void append(const cytnx_uint16 &val);
204 virtual void append(const cytnx_bool &val);
205
206 virtual Scalar get_item(const cytnx_uint64 &in) const;
207
208 virtual void set_item(const cytnx_uint64 &idx, const Scalar &val);
209 virtual void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
210 virtual void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
211 virtual void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
212 virtual void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
213 virtual void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
214 virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
215 virtual void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
216 virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
217 virtual void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
218 virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
219 virtual void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
220 };
222
224 class FloatStorage : public Storage_base {
225 public:
226 FloatStorage() { this->dtype = Type.Float; };
227 void Init(const unsigned long long &len_in, const int &device = -1,
228 const bool &init_zero = true);
229 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
230 const bool &iscap = false, const unsigned long long &cap_in = 0);
231 boost::intrusive_ptr<Storage_base> _create_new_sametype();
232 boost::intrusive_ptr<Storage_base> clone();
233 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
234 const std::vector<cytnx_uint64> &mapper,
235 const std::vector<cytnx_uint64> &invmapper);
236 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
237 const std::vector<cytnx_uint64> &mapper,
238 const std::vector<cytnx_uint64> &invmapper);
239 void to_(const int &device);
240 boost::intrusive_ptr<Storage_base> to(const int &device);
241 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
242 const std::vector<cytnx_uint64> &mapper = {});
243 void print_elems();
244
245 boost::intrusive_ptr<Storage_base> real();
246 boost::intrusive_ptr<Storage_base> imag();
247
248 // generators:
249 void fill(const cytnx_complex128 &val);
250 void fill(const cytnx_complex64 &val);
251 void fill(const cytnx_double &val);
252 void fill(const cytnx_float &val);
253 void fill(const cytnx_int64 &val);
254 void fill(const cytnx_uint64 &val);
255 void fill(const cytnx_int32 &val);
256 void fill(const cytnx_uint32 &val);
257 void fill(const cytnx_int16 &val);
258 void fill(const cytnx_uint16 &val);
259 void fill(const cytnx_bool &val);
260 void set_zeros();
261 void resize(const cytnx_uint64 &newsize);
262
263 void append(const Scalar &val);
264 void append(const cytnx_complex128 &val);
265 void append(const cytnx_complex64 &val);
266 void append(const cytnx_double &val);
267 void append(const cytnx_float &val);
268 void append(const cytnx_int64 &val);
269 void append(const cytnx_uint64 &val);
270 void append(const cytnx_int32 &val);
271 void append(const cytnx_uint32 &val);
272 void append(const cytnx_int16 &val);
273 void append(const cytnx_uint16 &val);
274 void append(const cytnx_bool &val);
275 Scalar get_item(const cytnx_uint64 &in) const;
276
277 void set_item(const cytnx_uint64 &idx, const Scalar &val);
278 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
279 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
280 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
281 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
282 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
283 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
284 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
285 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
286 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
287 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
288 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
289 };
291
293 class DoubleStorage : public Storage_base {
294 public:
295 DoubleStorage() { this->dtype = Type.Double; };
296 void Init(const unsigned long long &len_in, const int &device = -1,
297 const bool &init_zero = true);
298 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
299 const bool &iscap = false, const unsigned long long &cap_in = 0);
300 boost::intrusive_ptr<Storage_base> _create_new_sametype();
301 boost::intrusive_ptr<Storage_base> clone();
302 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
303 const std::vector<cytnx_uint64> &mapper,
304 const std::vector<cytnx_uint64> &invmapper);
305 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
306 const std::vector<cytnx_uint64> &mapper,
307 const std::vector<cytnx_uint64> &invmapper);
308 void to_(const int &device);
309 boost::intrusive_ptr<Storage_base> to(const int &device);
310 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
311 const std::vector<cytnx_uint64> &mapper = {});
312 void print_elems();
313
314 boost::intrusive_ptr<Storage_base> real();
315 boost::intrusive_ptr<Storage_base> imag();
316
317 // generators:
318 void fill(const cytnx_complex128 &val);
319 void fill(const cytnx_complex64 &val);
320 void fill(const cytnx_double &val);
321 void fill(const cytnx_float &val);
322 void fill(const cytnx_int64 &val);
323 void fill(const cytnx_uint64 &val);
324 void fill(const cytnx_int32 &val);
325 void fill(const cytnx_uint32 &val);
326 void fill(const cytnx_int16 &val);
327 void fill(const cytnx_uint16 &val);
328 void fill(const cytnx_bool &val);
329 void set_zeros();
330 void resize(const cytnx_uint64 &newsize);
331
332 void append(const Scalar &val);
333 void append(const cytnx_complex128 &val);
334 void append(const cytnx_complex64 &val);
335 void append(const cytnx_double &val);
336 void append(const cytnx_float &val);
337 void append(const cytnx_int64 &val);
338 void append(const cytnx_uint64 &val);
339 void append(const cytnx_int32 &val);
340 void append(const cytnx_uint32 &val);
341 void append(const cytnx_int16 &val);
342 void append(const cytnx_uint16 &val);
343 void append(const cytnx_bool &val);
344 Scalar get_item(const cytnx_uint64 &in) const;
345
346 void set_item(const cytnx_uint64 &idx, const Scalar &val);
347 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
348 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
349 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
350 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
351 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
352 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
353 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
354 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
355 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
356 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
357 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
358 };
360
362 class ComplexDoubleStorage : public Storage_base {
363 public:
364 ComplexDoubleStorage() { this->dtype = Type.ComplexDouble; };
365 void Init(const unsigned long long &len_in, const int &device = -1,
366 const bool &init_zero = true);
367 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
368 const bool &iscap = false, const unsigned long long &cap_in = 0);
369 boost::intrusive_ptr<Storage_base> _create_new_sametype();
370 boost::intrusive_ptr<Storage_base> clone();
371 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
372 const std::vector<cytnx_uint64> &mapper,
373 const std::vector<cytnx_uint64> &invmapper);
374 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
375 const std::vector<cytnx_uint64> &mapper,
376 const std::vector<cytnx_uint64> &invmapper);
377 void to_(const int &device);
378 boost::intrusive_ptr<Storage_base> to(const int &device);
379 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
380 const std::vector<cytnx_uint64> &mapper = {});
381 void print_elems();
382
383 boost::intrusive_ptr<Storage_base> real();
384 boost::intrusive_ptr<Storage_base> imag();
385
386 // generators:
387 void fill(const cytnx_complex128 &val);
388 void fill(const cytnx_complex64 &val);
389 void fill(const cytnx_double &val);
390 void fill(const cytnx_float &val);
391 void fill(const cytnx_int64 &val);
392 void fill(const cytnx_uint64 &val);
393 void fill(const cytnx_int32 &val);
394 void fill(const cytnx_uint32 &val);
395 void fill(const cytnx_int16 &val);
396 void fill(const cytnx_uint16 &val);
397 void fill(const cytnx_bool &val);
398 void set_zeros();
399 void resize(const cytnx_uint64 &newsize);
400
401 void append(const Scalar &val);
402 void append(const cytnx_complex128 &val);
403 void append(const cytnx_complex64 &val);
404 void append(const cytnx_double &val);
405 void append(const cytnx_float &val);
406 void append(const cytnx_int64 &val);
407 void append(const cytnx_uint64 &val);
408 void append(const cytnx_int32 &val);
409 void append(const cytnx_uint32 &val);
410 void append(const cytnx_int16 &val);
411 void append(const cytnx_uint16 &val);
412 void append(const cytnx_bool &val);
413 Scalar get_item(const cytnx_uint64 &in) const;
414
415 void set_item(const cytnx_uint64 &idx, const Scalar &val);
416 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
417 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
418 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
419 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
420 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
421 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
422 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
423 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
424 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
425 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
426 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
427 };
429
431 class ComplexFloatStorage : public Storage_base {
432 public:
433 ComplexFloatStorage() { this->dtype = Type.ComplexFloat; };
434 void Init(const unsigned long long &len_in, const int &device = -1,
435 const bool &init_zero = true);
436 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
437 const bool &iscap = false, const unsigned long long &cap_in = 0);
438 boost::intrusive_ptr<Storage_base> _create_new_sametype();
439 boost::intrusive_ptr<Storage_base> clone();
440 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
441 const std::vector<cytnx_uint64> &mapper,
442 const std::vector<cytnx_uint64> &invmapper);
443 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
444 const std::vector<cytnx_uint64> &mapper,
445 const std::vector<cytnx_uint64> &invmapper);
446 void to_(const int &device);
447 boost::intrusive_ptr<Storage_base> to(const int &device);
448 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
449 const std::vector<cytnx_uint64> &mapper = {});
450 void print_elems();
451
452 boost::intrusive_ptr<Storage_base> real();
453 boost::intrusive_ptr<Storage_base> imag();
454
455 // generators:
456 void fill(const cytnx_complex128 &val);
457 void fill(const cytnx_complex64 &val);
458 void fill(const cytnx_double &val);
459 void fill(const cytnx_float &val);
460 void fill(const cytnx_int64 &val);
461 void fill(const cytnx_uint64 &val);
462 void fill(const cytnx_int32 &val);
463 void fill(const cytnx_uint32 &val);
464 void fill(const cytnx_int16 &val);
465 void fill(const cytnx_uint16 &val);
466 void fill(const cytnx_bool &val);
467 void set_zeros();
468 void resize(const cytnx_uint64 &newsize);
469
470 void append(const Scalar &val);
471 void append(const cytnx_complex128 &val);
472 void append(const cytnx_complex64 &val);
473 void append(const cytnx_double &val);
474 void append(const cytnx_float &val);
475 void append(const cytnx_int64 &val);
476 void append(const cytnx_uint64 &val);
477 void append(const cytnx_int32 &val);
478 void append(const cytnx_uint32 &val);
479 void append(const cytnx_int16 &val);
480 void append(const cytnx_uint16 &val);
481 void append(const cytnx_bool &val);
482 Scalar get_item(const cytnx_uint64 &in) const;
483
484 void set_item(const cytnx_uint64 &idx, const Scalar &val);
485 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
486 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
487 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
488 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
489 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
490 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
491 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
492 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
493 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
494 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
495 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
496 };
498
500 class Int64Storage : public Storage_base {
501 public:
502 Int64Storage() { this->dtype = Type.Int64; };
503 void Init(const unsigned long long &len_in, const int &device = -1,
504 const bool &init_zero = true);
505 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
506 const bool &iscap = false, const unsigned long long &cap_in = 0);
507 boost::intrusive_ptr<Storage_base> _create_new_sametype();
508 boost::intrusive_ptr<Storage_base> clone();
509 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
510 const std::vector<cytnx_uint64> &mapper,
511 const std::vector<cytnx_uint64> &invmapper);
512 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
513 const std::vector<cytnx_uint64> &mapper,
514 const std::vector<cytnx_uint64> &invmapper);
515 void to_(const int &device);
516 boost::intrusive_ptr<Storage_base> to(const int &device);
517 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
518 const std::vector<cytnx_uint64> &mapper = {});
519 void print_elems();
520
521 boost::intrusive_ptr<Storage_base> real();
522 boost::intrusive_ptr<Storage_base> imag();
523
524 // generators:
525 void fill(const cytnx_complex128 &val);
526 void fill(const cytnx_complex64 &val);
527 void fill(const cytnx_double &val);
528 void fill(const cytnx_float &val);
529 void fill(const cytnx_int64 &val);
530 void fill(const cytnx_uint64 &val);
531 void fill(const cytnx_int32 &val);
532 void fill(const cytnx_uint32 &val);
533 void fill(const cytnx_int16 &val);
534 void fill(const cytnx_uint16 &val);
535 void fill(const cytnx_bool &val);
536 void set_zeros();
537 void resize(const cytnx_uint64 &newsize);
538
539 void append(const Scalar &val);
540 void append(const cytnx_complex128 &val);
541 void append(const cytnx_complex64 &val);
542 void append(const cytnx_double &val);
543 void append(const cytnx_float &val);
544 void append(const cytnx_int64 &val);
545 void append(const cytnx_uint64 &val);
546 void append(const cytnx_int32 &val);
547 void append(const cytnx_uint32 &val);
548 void append(const cytnx_int16 &val);
549 void append(const cytnx_uint16 &val);
550 void append(const cytnx_bool &val);
551 Scalar get_item(const cytnx_uint64 &in) const;
552
553 void set_item(const cytnx_uint64 &idx, const Scalar &val);
554 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
555 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
556 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
557 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
558 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
559 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
560 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
561 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
562 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
563 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
564 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
565 };
567
569 class Uint64Storage : public Storage_base {
570 public:
571 Uint64Storage() { this->dtype = Type.Uint64; };
572 void Init(const unsigned long long &len_in, const int &device = -1,
573 const bool &init_zero = true);
574 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
575 const bool &iscap = false, const unsigned long long &cap_in = 0);
576 boost::intrusive_ptr<Storage_base> _create_new_sametype();
577 boost::intrusive_ptr<Storage_base> clone();
578 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
579 const std::vector<cytnx_uint64> &mapper,
580 const std::vector<cytnx_uint64> &invmapper);
581 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
582 const std::vector<cytnx_uint64> &mapper,
583 const std::vector<cytnx_uint64> &invmapper);
584 void to_(const int &device);
585 boost::intrusive_ptr<Storage_base> to(const int &device);
586 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
587 const std::vector<cytnx_uint64> &mapper = {});
588 void print_elems();
589
590 boost::intrusive_ptr<Storage_base> real();
591 boost::intrusive_ptr<Storage_base> imag();
592
593 // generators:
594 void fill(const cytnx_complex128 &val);
595 void fill(const cytnx_complex64 &val);
596 void fill(const cytnx_double &val);
597 void fill(const cytnx_float &val);
598 void fill(const cytnx_int64 &val);
599 void fill(const cytnx_uint64 &val);
600 void fill(const cytnx_int32 &val);
601 void fill(const cytnx_uint32 &val);
602 void fill(const cytnx_int16 &val);
603 void fill(const cytnx_uint16 &val);
604 void fill(const cytnx_bool &val);
605 void set_zeros();
606 void resize(const cytnx_uint64 &newsize);
607
608 void append(const Scalar &val);
609 void append(const cytnx_complex128 &val);
610 void append(const cytnx_complex64 &val);
611 void append(const cytnx_double &val);
612 void append(const cytnx_float &val);
613 void append(const cytnx_int64 &val);
614 void append(const cytnx_uint64 &val);
615 void append(const cytnx_int32 &val);
616 void append(const cytnx_uint32 &val);
617 void append(const cytnx_int16 &val);
618 void append(const cytnx_uint16 &val);
619 void append(const cytnx_bool &val);
620 Scalar get_item(const cytnx_uint64 &in) const;
621
622 void set_item(const cytnx_uint64 &idx, const Scalar &val);
623 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
624 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
625 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
626 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
627 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
628 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
629 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
630 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
631 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
632 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
633 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
634 };
637 class Int32Storage : public Storage_base {
638 public:
639 Int32Storage() { this->dtype = Type.Int32; };
640 void Init(const unsigned long long &len_in, const int &device = -1,
641 const bool &init_zero = true);
642 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
643 const bool &iscap = false, const unsigned long long &cap_in = 0);
644 boost::intrusive_ptr<Storage_base> _create_new_sametype();
645 boost::intrusive_ptr<Storage_base> clone();
646 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
647 const std::vector<cytnx_uint64> &mapper,
648 const std::vector<cytnx_uint64> &invmapper);
649 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
650 const std::vector<cytnx_uint64> &mapper,
651 const std::vector<cytnx_uint64> &invmapper);
652 void to_(const int &device);
653 boost::intrusive_ptr<Storage_base> to(const int &device);
654 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
655 const std::vector<cytnx_uint64> &mapper = {});
656 void print_elems();
657
658 boost::intrusive_ptr<Storage_base> real();
659 boost::intrusive_ptr<Storage_base> imag();
660
661 // generators:
662 void fill(const cytnx_complex128 &val);
663 void fill(const cytnx_complex64 &val);
664 void fill(const cytnx_double &val);
665 void fill(const cytnx_float &val);
666 void fill(const cytnx_int64 &val);
667 void fill(const cytnx_uint64 &val);
668 void fill(const cytnx_int32 &val);
669 void fill(const cytnx_uint32 &val);
670 void fill(const cytnx_int16 &val);
671 void fill(const cytnx_uint16 &val);
672 void fill(const cytnx_bool &val);
673 void set_zeros();
674 void resize(const cytnx_uint64 &newsize);
675 void append(const Scalar &val);
676 void append(const cytnx_complex128 &val);
677 void append(const cytnx_complex64 &val);
678 void append(const cytnx_double &val);
679 void append(const cytnx_float &val);
680 void append(const cytnx_int64 &val);
681 void append(const cytnx_uint64 &val);
682 void append(const cytnx_int32 &val);
683 void append(const cytnx_uint32 &val);
684 void append(const cytnx_int16 &val);
685 void append(const cytnx_uint16 &val);
686 void append(const cytnx_bool &val);
687 Scalar get_item(const cytnx_uint64 &in) const;
688
689 void set_item(const cytnx_uint64 &idx, const Scalar &val);
690 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
691 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
692 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
693 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
694 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
695 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
696 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
697 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
698 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
699 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
700 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
701 };
703
705 class Uint32Storage : public Storage_base {
706 public:
707 Uint32Storage() { this->dtype = Type.Uint32; };
708 void Init(const unsigned long long &len_in, const int &device = -1,
709 const bool &init_zero = true);
710 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
711 const bool &iscap = false, const unsigned long long &cap_in = 0);
712 boost::intrusive_ptr<Storage_base> _create_new_sametype();
713 boost::intrusive_ptr<Storage_base> clone();
714 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
715 const std::vector<cytnx_uint64> &mapper,
716 const std::vector<cytnx_uint64> &invmapper);
717 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
718 const std::vector<cytnx_uint64> &mapper,
719 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,
723 const std::vector<cytnx_uint64> &mapper = {});
724 void print_elems();
725
726 boost::intrusive_ptr<Storage_base> real();
727 boost::intrusive_ptr<Storage_base> imag();
728
729 // generators:
730 void fill(const cytnx_complex128 &val);
731 void fill(const cytnx_complex64 &val);
732 void fill(const cytnx_double &val);
733 void fill(const cytnx_float &val);
734 void fill(const cytnx_int64 &val);
735 void fill(const cytnx_uint64 &val);
736 void fill(const cytnx_int32 &val);
737 void fill(const cytnx_uint32 &val);
738 void fill(const cytnx_int16 &val);
739 void fill(const cytnx_uint16 &val);
740 void fill(const cytnx_bool &val);
741 void set_zeros();
742 void resize(const cytnx_uint64 &newsize);
743 void append(const Scalar &val);
744 void append(const cytnx_complex128 &val);
745 void append(const cytnx_complex64 &val);
746 void append(const cytnx_double &val);
747 void append(const cytnx_float &val);
748 void append(const cytnx_int64 &val);
749 void append(const cytnx_uint64 &val);
750 void append(const cytnx_int32 &val);
751 void append(const cytnx_uint32 &val);
752 void append(const cytnx_int16 &val);
753 void append(const cytnx_uint16 &val);
754 void append(const cytnx_bool &val);
755 Scalar get_item(const cytnx_uint64 &in) const;
756
757 void set_item(const cytnx_uint64 &idx, const Scalar &val);
758 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
759 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
760 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
761 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
762 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
763 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
764 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
765 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
766 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
767 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
768 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
769 };
771
773 class Uint16Storage : public Storage_base {
774 public:
775 Uint16Storage() { this->dtype = Type.Uint16; };
776 void Init(const unsigned long long &len_in, const int &device = -1,
777 const bool &init_zero = true);
778 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
779 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,
783 const std::vector<cytnx_uint64> &mapper,
784 const std::vector<cytnx_uint64> &invmapper);
785 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
786 const std::vector<cytnx_uint64> &mapper,
787 const std::vector<cytnx_uint64> &invmapper);
788 void to_(const int &device);
789 boost::intrusive_ptr<Storage_base> to(const int &device);
790 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
791 const std::vector<cytnx_uint64> &mapper = {});
792 void print_elems();
793
794 boost::intrusive_ptr<Storage_base> real();
795 boost::intrusive_ptr<Storage_base> imag();
796
797 // generators:
798 void fill(const cytnx_complex128 &val);
799 void fill(const cytnx_complex64 &val);
800 void fill(const cytnx_double &val);
801 void fill(const cytnx_float &val);
802 void fill(const cytnx_int64 &val);
803 void fill(const cytnx_uint64 &val);
804 void fill(const cytnx_int32 &val);
805 void fill(const cytnx_uint32 &val);
806 void fill(const cytnx_int16 &val);
807 void fill(const cytnx_uint16 &val);
808 void fill(const cytnx_bool &val);
809 void set_zeros();
810 void resize(const cytnx_uint64 &newsize);
811
812 void append(const Scalar &val);
813 void append(const cytnx_complex128 &val);
814 void append(const cytnx_complex64 &val);
815 void append(const cytnx_double &val);
816 void append(const cytnx_float &val);
817 void append(const cytnx_int64 &val);
818 void append(const cytnx_uint64 &val);
819 void append(const cytnx_int32 &val);
820 void append(const cytnx_uint32 &val);
821 void append(const cytnx_int16 &val);
822 void append(const cytnx_uint16 &val);
823 void append(const cytnx_bool &val);
824 Scalar get_item(const cytnx_uint64 &in) const;
825
826 void set_item(const cytnx_uint64 &idx, const Scalar &val);
827 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
828 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
829 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
830 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
831 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
832 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
833 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
834 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
835 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
836 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
837 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
838 };
840
842 class Int16Storage : public Storage_base {
843 public:
844 Int16Storage() { this->dtype = Type.Int16; };
845 void Init(const unsigned long long &len_in, const int &device = -1,
846 const bool &init_zero = true);
847 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
848 const bool &iscap = false, const unsigned long long &cap_in = 0);
849 boost::intrusive_ptr<Storage_base> _create_new_sametype();
850 boost::intrusive_ptr<Storage_base> clone();
851 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
852 const std::vector<cytnx_uint64> &mapper,
853 const std::vector<cytnx_uint64> &invmapper);
854 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
855 const std::vector<cytnx_uint64> &mapper,
856 const std::vector<cytnx_uint64> &invmapper);
857 void to_(const int &device);
858 boost::intrusive_ptr<Storage_base> to(const int &device);
859 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
860 const std::vector<cytnx_uint64> &mapper = {});
861 void print_elems();
862
863 boost::intrusive_ptr<Storage_base> real();
864 boost::intrusive_ptr<Storage_base> imag();
865
866 // generators:
867 void fill(const cytnx_complex128 &val);
868 void fill(const cytnx_complex64 &val);
869 void fill(const cytnx_double &val);
870 void fill(const cytnx_float &val);
871 void fill(const cytnx_int64 &val);
872 void fill(const cytnx_uint64 &val);
873 void fill(const cytnx_int32 &val);
874 void fill(const cytnx_uint32 &val);
875 void fill(const cytnx_int16 &val);
876 void fill(const cytnx_uint16 &val);
877 void fill(const cytnx_bool &val);
878 void set_zeros();
879 void resize(const cytnx_uint64 &newsize);
880 void append(const Scalar &val);
881 void append(const cytnx_complex128 &val);
882 void append(const cytnx_complex64 &val);
883 void append(const cytnx_double &val);
884 void append(const cytnx_float &val);
885 void append(const cytnx_int64 &val);
886 void append(const cytnx_uint64 &val);
887 void append(const cytnx_int32 &val);
888 void append(const cytnx_uint32 &val);
889 void append(const cytnx_int16 &val);
890 void append(const cytnx_uint16 &val);
891 void append(const cytnx_bool &val);
892 Scalar get_item(const cytnx_uint64 &in) const;
893
894 void set_item(const cytnx_uint64 &idx, const Scalar &val);
895 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
896 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
897 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
898 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
899 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
900 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
901 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
902 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
903 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
904 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
905 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
906 };
908
910 class BoolStorage : public Storage_base {
911 public:
912 BoolStorage() { this->dtype = Type.Bool; };
913 void Init(const unsigned long long &len_in, const int &device = -1,
914 const bool &init_zero = true);
915 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
916 const bool &iscap = false, const unsigned long long &cap_in = 0);
917 boost::intrusive_ptr<Storage_base> _create_new_sametype();
918 boost::intrusive_ptr<Storage_base> clone();
919 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
920 const std::vector<cytnx_uint64> &mapper,
921 const std::vector<cytnx_uint64> &invmapper);
922 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
923 const std::vector<cytnx_uint64> &mapper,
924 const std::vector<cytnx_uint64> &invmapper);
925 void to_(const int &device);
926 boost::intrusive_ptr<Storage_base> to(const int &device);
927 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
928 const std::vector<cytnx_uint64> &mapper = {});
929 void print_elems();
930
931 boost::intrusive_ptr<Storage_base> real();
932 boost::intrusive_ptr<Storage_base> imag();
933
934 // generators:
935 void fill(const cytnx_complex128 &val);
936 void fill(const cytnx_complex64 &val);
937 void fill(const cytnx_double &val);
938 void fill(const cytnx_float &val);
939 void fill(const cytnx_int64 &val);
940 void fill(const cytnx_uint64 &val);
941 void fill(const cytnx_int32 &val);
942 void fill(const cytnx_uint32 &val);
943 void fill(const cytnx_int16 &val);
944 void fill(const cytnx_uint16 &val);
945 void fill(const cytnx_bool &val);
946 void set_zeros();
947 void resize(const cytnx_uint64 &newsize);
948 void append(const Scalar &val);
949 void append(const cytnx_complex128 &val);
950 void append(const cytnx_complex64 &val);
951 void append(const cytnx_double &val);
952 void append(const cytnx_float &val);
953 void append(const cytnx_int64 &val);
954 void append(const cytnx_uint64 &val);
955 void append(const cytnx_int32 &val);
956 void append(const cytnx_uint32 &val);
957 void append(const cytnx_int16 &val);
958 void append(const cytnx_uint16 &val);
959 void append(const cytnx_bool &val);
960 Scalar get_item(const cytnx_uint64 &in) const;
961
962 void set_item(const cytnx_uint64 &idx, const Scalar &val);
963 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
964 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
965 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
966 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
967 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
968 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
969 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
970 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
971 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
972 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
973 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
974 };
976
978 typedef boost::intrusive_ptr<Storage_base> (*pStorage_init)();
979 inline boost::intrusive_ptr<Storage_base> SIInit_cd() {
980 boost::intrusive_ptr<Storage_base> out(new ComplexDoubleStorage());
981 return out;
982 }
983 inline boost::intrusive_ptr<Storage_base> SIInit_cf() {
984 boost::intrusive_ptr<Storage_base> out(new ComplexFloatStorage());
985 return out;
986 }
987 inline boost::intrusive_ptr<Storage_base> SIInit_d() {
988 boost::intrusive_ptr<Storage_base> out(new DoubleStorage());
989 return out;
990 }
991 inline boost::intrusive_ptr<Storage_base> SIInit_f() {
992 boost::intrusive_ptr<Storage_base> out(new FloatStorage());
993 return out;
994 }
995 inline boost::intrusive_ptr<Storage_base> SIInit_u64() {
996 boost::intrusive_ptr<Storage_base> out(new Uint64Storage());
997 return out;
998 }
999 inline boost::intrusive_ptr<Storage_base> SIInit_i64() {
1000 boost::intrusive_ptr<Storage_base> out(new Int64Storage());
1001 return out;
1002 }
1003 inline boost::intrusive_ptr<Storage_base> SIInit_u32() {
1004 boost::intrusive_ptr<Storage_base> out(new Uint32Storage());
1005 return out;
1006 }
1007 inline boost::intrusive_ptr<Storage_base> SIInit_i32() {
1008 boost::intrusive_ptr<Storage_base> out(new Int32Storage());
1009 return out;
1010 }
1011 inline boost::intrusive_ptr<Storage_base> SIInit_u16() {
1012 boost::intrusive_ptr<Storage_base> out(new Uint16Storage());
1013 return out;
1014 }
1015 inline boost::intrusive_ptr<Storage_base> SIInit_i16() {
1016 boost::intrusive_ptr<Storage_base> out(new Int16Storage());
1017 return out;
1018 }
1019 inline boost::intrusive_ptr<Storage_base> SIInit_b() {
1020 boost::intrusive_ptr<Storage_base> out(new BoolStorage());
1021 return out;
1022 }
1025 class Storage_init_interface : public Type_class {
1026 public:
1027 // std::vector<pStorage_init> USIInit;
1028 inline static pStorage_init USIInit[N_Type];
1029 inline static bool inited = false;
1030 Storage_init_interface() {
1031 if (!inited) {
1032 USIInit[this->Double] = SIInit_d;
1033 USIInit[this->Float] = SIInit_f;
1034 USIInit[this->ComplexDouble] = SIInit_cd;
1035 USIInit[this->ComplexFloat] = SIInit_cf;
1036 USIInit[this->Uint64] = SIInit_u64;
1037 USIInit[this->Int64] = SIInit_i64;
1038 USIInit[this->Uint32] = SIInit_u32;
1039 USIInit[this->Int32] = SIInit_i32;
1040 USIInit[this->Uint16] = SIInit_u16;
1041 USIInit[this->Int16] = SIInit_i16;
1042 USIInit[this->Bool] = SIInit_b;
1043 inited = true;
1044 }
1045 }
1046 };
1047 extern Storage_init_interface __SII;
1049
1051 class Storage {
1052 private:
1053 // Interface:
1054 // Storage_init_interface __SII;
1055
1056 public:
1058 boost::intrusive_ptr<Storage_base> _impl;
1060
1079 void Init(const unsigned long long &size, const unsigned int &dtype = Type.Double,
1080 int device = -1, const bool &init_zero = true) {
1081 cytnx_error_msg(dtype >= N_Type, "%s", "[ERROR] invalid argument: dtype");
1082 this->_impl = __SII.USIInit[dtype]();
1083 this->_impl->Init(size, device, init_zero);
1084 }
1085 // void _Init_byptr(void *rawptr, const unsigned long long &len_in, const unsigned int &dtype =
1086 // Type.Double, const int &device = -1,
1087 // const bool &iscap = false, const unsigned long long &cap_in =
1088 // 0){
1089 // cytnx_error_msg(dtype >= N_Type, "%s", "[ERROR] invalid argument: dtype");
1090 // this->_impl = __SII.USIInit[dtype]();
1091 // this->_impl->_Init_byptr(rawptr, len_in, device, iscap, cap_in);
1092 // }
1093
1102 Storage(const unsigned long long &size, const unsigned int &dtype = Type.Double,
1103 int device = -1, const bool &init_zero = true)
1104 : _impl(new Storage_base()) {
1106 }
1107 // Storage(void *rawptr, const unsigned long long &len_in, const unsigned int &dtype =
1108 // Type.Double, const int &device = -1,
1109 // const bool &iscap = false, const unsigned long long &cap_in = 0)
1110 // : _impl(new Storage_base()){
1111 // _Init_byptr(rawptr,len_in,dtype,device,iscap,cap_in);
1112 // }
1113
1119 Storage(boost::intrusive_ptr<Storage_base> in_impl) { this->_impl = in_impl; }
1120 Storage(const Storage &rhs) { this->_impl = rhs._impl; }
1121
1122 template <class Tp>
1123 Storage(const std::vector<Tp> &rhs) {
1124 this->_from_vector(rhs, -1);
1125 }
1126 template <class Tp>
1127 Storage(const std::initializer_list<Tp> &rhs) {
1128 this->_from_vector(std::vector<Tp>(rhs), -1);
1129 }
1130
1131 Storage &operator=(const Storage &rhs) {
1132 this->_impl = rhs._impl;
1133 return *this;
1134 }
1135
1137
1139 void _Save(std::fstream &f) const;
1140 void _Load(std::fstream &f);
1141 void _Loadbinary(std::fstream &f, const unsigned int &dtype, const cytnx_uint64 &Nelem);
1142 void _Savebinary(std::fstream &f) const;
1143
1145
1154 void Save(const std::string &fname) const;
1155
1159 void Save(const char *fname) const;
1164 void Tofile(const std::string &fname) const;
1166 void Tofile(const char *fname) const;
1168 void Tofile(std::fstream &f) const;
1169
1178 static Storage Load(const std::string &fname);
1179
1183 static Storage Load(const char *fname);
1201 static Storage Fromfile(const std::string &fname, const unsigned int &dtype,
1202 const cytnx_int64 &count = -1);
1203
1208 static Storage Fromfile(const char *fname, const unsigned int &dtype,
1209 const cytnx_int64 &count = -1);
1210
1235 Storage astype(const unsigned int &new_type) const { return this->_impl->astype(new_type); }
1236
1241 const unsigned int &dtype() const { return this->_impl->dtype; }
1242
1247 const std::string dtype_str() const {
1248 std::string out = this->_impl->dtype_str();
1249 return out;
1250 }
1255 const int &device() const { return this->_impl->device; }
1256
1261 const std::string device_str() const {
1262 std::string out = this->_impl->device_str();
1263 return out;
1264 }
1265
1271 template <class T>
1272 void append(const T &val) {
1273 return this->_impl->append(val);
1274 }
1275
1277 template <class T> // this is c++ only
1278 T &at(const cytnx_uint64 &idx) const {
1279 return this->_impl->at<T>(idx);
1280 }
1281
1282 const Scalar::Sproxy at(const cytnx_uint64 &idx) const {
1283 Scalar::Sproxy out(this->_impl, idx);
1284 return out;
1285 }
1286 Scalar::Sproxy at(const cytnx_uint64 &idx) {
1287 Scalar::Sproxy out(this->_impl, idx);
1288 return out;
1289 }
1290
1291 template <class T> // this is c++ only
1292 T &back() const {
1293 return this->_impl->back<T>();
1294 }
1295
1296 const Scalar::Sproxy back() const {
1297 Scalar::Sproxy out(this->_impl, this->size() - 1);
1298 return out;
1299 }
1300 Scalar::Sproxy back() {
1301 Scalar::Sproxy out(this->_impl, this->size() - 1);
1302 return out;
1303 }
1304
1305 template <class T> // this is c++ only
1306 T *data() const {
1307 return this->_impl->data<T>();
1308 }
1309
1310 void *data() const { return this->_impl->data(); }
1312
1317 void resize(const cytnx_uint64 &newsize) { this->_impl->resize(newsize); }
1318
1324 void to_(const int &device) { this->_impl->to_(device); }
1325
1333 Storage to(const int &device) { return Storage(this->_impl->to(device)); }
1334
1350 Storage clone() const { return Storage(this->_impl->clone()); }
1351
1357 const unsigned long long &size() const { return this->_impl->len; }
1358
1366 const unsigned long long &capacity() const { return this->_impl->cap; }
1367
1372 void print_info() const { this->_impl->print_info(); }
1374 // this is a redundant function
1375 void print() const { this->_impl->print(); }
1377
1384 void set_zeros() { this->_impl->set_zeros(); }
1385
1405 bool operator==(const Storage &rhs);
1406
1410 bool operator!=(const Storage &rhs);
1411
1417 template <class T>
1418 void fill(const T &val) {
1419 this->_impl->fill(val);
1420 }
1421
1427 template <class T>
1428 static Storage from_vector(const std::vector<T> &vin, const int device = -1) {
1429 Storage out;
1430 out._from_vector(vin, device);
1431 return out;
1432 }
1433
1434 /*
1435 @brief convert a Storage to C++ vector.
1436
1437 [Note]
1438 This function is C++ only
1439 */
1440 /*
1441 template <class T>
1442 std::vector<T> vector() {
1443 T tmp;
1444 cytnx_error_msg(Type.cy_typeid(tmp) != this->dtype(),
1445 "[ERROR] the dtype of current Storage does not match assigned vector type.%s",
1446 "\n");
1447
1448 std::vector<T> out(this->size());
1449 Storage S;
1450 if (this->device() != Device.cpu) {
1451 S = this->to(Device.cpu);
1452 memcpy(&out[0], S.data(), sizeof(T) * this->size());
1453 } else {
1454 memcpy(&out[0], this->data(), sizeof(T) * this->size());
1455 }
1456
1457 return out;
1458 }
1459 */
1460
1465 template <class T>
1466 std::vector<T> vector();
1467
1469
1470 template <class T>
1471 void _from_vector(const std::vector<T> &vin, const int device = -1) {
1472 // auto dispatch:
1473 // check:
1474 cytnx_error_msg(1, "[FATAL] ERROR unsupport type%s", "\n");
1475 // this->_impl->Init(vin.size(),device);
1476 // memcpy(this->_impl->Mem,&vin[0],sizeof(T)*vin.size());
1477 }
1478
1479 void _from_vector(const std::vector<cytnx_complex128> &vin, const int device = -1) {
1480 this->_impl = __SII.USIInit[Type.ComplexDouble]();
1481 this->_impl->Init(vin.size(), device);
1482 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_complex128) * vin.size());
1483 }
1484 void _from_vector(const std::vector<cytnx_complex64> &vin, const int device = -1) {
1485 this->_impl = __SII.USIInit[Type.ComplexFloat]();
1486 this->_impl->Init(vin.size(), device);
1487 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_complex64) * vin.size());
1488 }
1489 void _from_vector(const std::vector<cytnx_double> &vin, const int device = -1) {
1490 this->_impl = __SII.USIInit[Type.Double]();
1491 this->_impl->Init(vin.size(), device);
1492 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_double) * vin.size());
1493 }
1494 void _from_vector(const std::vector<cytnx_float> &vin, const int device = -1) {
1495 this->_impl = __SII.USIInit[Type.Float]();
1496 this->_impl->Init(vin.size(), device);
1497 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_float) * vin.size());
1498 }
1499 void _from_vector(const std::vector<cytnx_uint64> &vin, const int device = -1) {
1500 this->_impl = __SII.USIInit[Type.Uint64]();
1501 this->_impl->Init(vin.size(), device);
1502 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint64) * vin.size());
1503 }
1504 void _from_vector(const std::vector<cytnx_int64> &vin, const int device = -1) {
1505 this->_impl = __SII.USIInit[Type.Int64]();
1506 this->_impl->Init(vin.size(), device);
1507 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int64) * vin.size());
1508 }
1509 void _from_vector(const std::vector<cytnx_uint32> &vin, const int device = -1) {
1510 this->_impl = __SII.USIInit[Type.Uint32]();
1511 this->_impl->Init(vin.size(), device);
1512 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint32) * vin.size());
1513 }
1514 void _from_vector(const std::vector<cytnx_int32> &vin, const int device = -1) {
1515 this->_impl = __SII.USIInit[Type.Int32]();
1516 this->_impl->Init(vin.size(), device);
1517 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int32) * vin.size());
1518 }
1519 void _from_vector(const std::vector<cytnx_uint16> &vin, const int device = -1) {
1520 this->_impl = __SII.USIInit[Type.Uint16]();
1521 this->_impl->Init(vin.size(), device);
1522 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint16) * vin.size());
1523 }
1524 void _from_vector(const std::vector<cytnx_int16> &vin, const int device = -1) {
1525 this->_impl = __SII.USIInit[Type.Int16]();
1526 this->_impl->Init(vin.size(), device);
1527 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int16) * vin.size());
1528 }
1529 void _from_vector(const std::vector<cytnx_bool> &vin, const int device = -1) {
1530 this->_impl = __SII.USIInit[Type.Bool]();
1531 this->_impl->Init(vin.size(), device);
1532 this->_impl->_cpy_bool(this->_impl->Mem, vin);
1533 // memcpy(this->_impl->Mem,vin.data(),sizeof(cytnx_bool)*vin.size());
1534 }
1536
1551 Storage real() const { return Storage(this->_impl->real()); };
1552
1567 Storage imag() const { return Storage(this->_impl->imag()); };
1568
1574 Scalar get_item(const cytnx_uint64 &idx) const { return this->_impl->get_item(idx); };
1575
1581 template <class T>
1582 void set_item(const cytnx_uint64 &idx, const T &elem) {
1583 this->_impl->set_item(idx, elem);
1584 };
1585
1590 Scalar::Sproxy operator()(const cytnx_uint64 &idx);
1591 };
1592
1594 std::ostream &operator<<(std::ostream &os, const Storage &in);
1596
1597} // namespace cytnx
1598
1599#endif
A class to represent a scalar.
Definition Scalar.hpp:2470
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:1051
void to_(const int &device)
Move the current Storage to different deivce.
Definition Storage.hpp:1324
void append(const T &val)
append a value
Definition Storage.hpp:1272
Storage(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1, const bool &init_zero=true)
The constructor of Storage class. It will call the function Init to initialize the Storage instance.
Definition Storage.hpp:1102
static Storage Load(const std::string &fname)
Load current Storage from file.
const int & device() const
the device-id of current Storage, see cytnx::Device for more details.
Definition Storage.hpp:1255
Storage real() const
Get the real part form a Complex type Storage.
Definition Storage.hpp:1551
std::vector< T > vector()
renew/create a c++ std::vector using current Storage.
Storage to(const int &device)
move a new Storage with same content as current Storage on different deivce.
Definition Storage.hpp:1333
void Init(const unsigned long long &size, const unsigned int &dtype=Type.Double, int device=-1, const bool &init_zero=true)
initialize a Storage
Definition Storage.hpp:1079
void resize(const cytnx_uint64 &newsize)
resize the current Storage.
Definition Storage.hpp:1317
void Save(const std::string &fname) const
Save current Storage to file.
void fill(const T &val)
set all the elements to the assigned value val
Definition Storage.hpp:1418
Storage()
The default constructor of Storage class. It will create an empty Storage instance.
Definition Storage.hpp:1117
void Tofile(const std::string &fname) const
Save current Storage to a binary file, which only contains the raw data.
const unsigned int & dtype() const
the dtype-id of current Storage, see cytnx::Type for more details.
Definition Storage.hpp:1241
void set_item(const cytnx_uint64 &idx, const T &elem)
Set the element at the given index.
Definition Storage.hpp:1582
const unsigned long long & capacity() const
the capacity in the Storage.
Definition Storage.hpp:1366
void set_zeros()
set all the elements to zero.
Definition Storage.hpp:1384
static Storage Load(const char *fname)
Load current Storage from file, same as Load(const std::string &fname)
static Storage Fromfile(const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
Load the binary file, which only contains the raw data, to current Storage.
Storage astype(const unsigned int &new_type) const
cast the type of current Storage
Definition Storage.hpp:1235
const unsigned long long & size() const
the size ( no. of elements ) in the Storage
Definition Storage.hpp:1357
void print_info() const
print the info of the Storage, including the device, dtype and size.
Definition Storage.hpp:1372
void Tofile(const char *fname) const
static Storage from_vector(const std::vector< T > &vin, const int device=-1)
renew/create a Storage using c++ vector.
Definition Storage.hpp:1428
bool operator!=(const Storage &rhs)
The not-equal operator for Storage.
static Storage Fromfile(const char *fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
const std::string dtype_str() const
the dtype (std::string) of current Storage, see cytnx::Type for more details.
Definition Storage.hpp:1247
Scalar::Sproxy operator()(const cytnx_uint64 &idx)
The access operator for the Storage.
void Tofile(std::fstream &f) const
bool operator==(const Storage &rhs)
compare two Storage
Scalar get_item(const cytnx_uint64 &idx) const
Get the element at the given index.
Definition Storage.hpp:1574
Storage imag() const
Get the imaginary part form a Complex type Storage.
Definition Storage.hpp:1567
const std::string device_str() const
the device (std::string) of current Storage, see cytnx::Device for more details.
Definition Storage.hpp:1261
Storage clone() const
return a deep copy of the current storage.
Definition Storage.hpp:1350
void Save(const char *fname) const
Save current Storage to file, same as Save(const std::string &fname)
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
double cytnx_double
Definition Type.hpp:43
uint32_t cytnx_uint32
Definition Type.hpp:46
std::complex< double > cytnx_complex128
Definition Type.hpp:53
float cytnx_float
Definition Type.hpp:44
int16_t cytnx_int16
Definition Type.hpp:50
std::complex< float > cytnx_complex64
Definition Type.hpp:52
int32_t cytnx_int32
Definition Type.hpp:49
uint16_t cytnx_uint16
Definition Type.hpp:47
uint64_t cytnx_uint64
Definition Type.hpp:45
int64_t cytnx_int64
Definition Type.hpp:48
Type_class Type
data type