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, const bool &init_zero = true);
150 virtual void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
151 const bool &iscap = false, const unsigned long long &cap_in = 0);
152
153 // this function will return a new storage with the same type as the one
154 // that initiate this function.
155 virtual boost::intrusive_ptr<Storage_base> _create_new_sametype();
156
157 // [future] this will move the memory to device / cpu
158 virtual void to_(const int &device);
159 virtual boost::intrusive_ptr<Storage_base> to(const int &device);
160
161 virtual boost::intrusive_ptr<Storage_base> clone();
162
163 // this will perform permute on the underlying memory.
164 virtual boost::intrusive_ptr<Storage_base> Move_memory(
165 const std::vector<cytnx_uint64> &old_shape, const std::vector<cytnx_uint64> &mapper,
166 const std::vector<cytnx_uint64> &invmapper);
167 virtual void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
168 const std::vector<cytnx_uint64> &mapper,
169 const std::vector<cytnx_uint64> &invmapper);
170 virtual void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
171 const std::vector<cytnx_uint64> &mapper = {});
172 virtual void print_elems();
173
174 virtual boost::intrusive_ptr<Storage_base> real();
175 virtual boost::intrusive_ptr<Storage_base> imag();
176
177 // generators:
178 virtual void fill(const cytnx_complex128 &val);
179 virtual void fill(const cytnx_complex64 &val);
180 virtual void fill(const cytnx_double &val);
181 virtual void fill(const cytnx_float &val);
182 virtual void fill(const cytnx_int64 &val);
183 virtual void fill(const cytnx_uint64 &val);
184 virtual void fill(const cytnx_int32 &val);
185 virtual void fill(const cytnx_uint32 &val);
186 virtual void fill(const cytnx_int16 &val);
187 virtual void fill(const cytnx_uint16 &val);
188 virtual void fill(const cytnx_bool &val);
189 virtual void set_zeros();
190 virtual void resize(const cytnx_uint64 &newsize);
191
192 virtual void append(const Scalar &val);
193 virtual void append(const cytnx_complex128 &val);
194 virtual void append(const cytnx_complex64 &val);
195 virtual void append(const cytnx_double &val);
196 virtual void append(const cytnx_float &val);
197 virtual void append(const cytnx_int64 &val);
198 virtual void append(const cytnx_uint64 &val);
199 virtual void append(const cytnx_int32 &val);
200 virtual void append(const cytnx_uint32 &val);
201 virtual void append(const cytnx_int16 &val);
202 virtual void append(const cytnx_uint16 &val);
203 virtual void append(const cytnx_bool &val);
204
205 virtual Scalar get_item(const cytnx_uint64 &in) const;
206
207 virtual void set_item(const cytnx_uint64 &idx, const Scalar &val);
208 virtual void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
209 virtual void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
210 virtual void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
211 virtual void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
212 virtual void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
213 virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
214 virtual void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
215 virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
216 virtual void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
217 virtual void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
218 virtual void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
219 };
221
223 class FloatStorage : public Storage_base {
224 public:
225 FloatStorage() { this->dtype = Type.Float; };
226 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero=true);
227 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
228 const bool &iscap = false, const unsigned long long &cap_in = 0);
229 boost::intrusive_ptr<Storage_base> _create_new_sametype();
230 boost::intrusive_ptr<Storage_base> clone();
231 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
232 const std::vector<cytnx_uint64> &mapper,
233 const std::vector<cytnx_uint64> &invmapper);
234 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
235 const std::vector<cytnx_uint64> &mapper,
236 const std::vector<cytnx_uint64> &invmapper);
237 void to_(const int &device);
238 boost::intrusive_ptr<Storage_base> to(const int &device);
239 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
240 const std::vector<cytnx_uint64> &mapper = {});
241 void print_elems();
242
243 boost::intrusive_ptr<Storage_base> real();
244 boost::intrusive_ptr<Storage_base> imag();
245
246 // generators:
247 void fill(const cytnx_complex128 &val);
248 void fill(const cytnx_complex64 &val);
249 void fill(const cytnx_double &val);
250 void fill(const cytnx_float &val);
251 void fill(const cytnx_int64 &val);
252 void fill(const cytnx_uint64 &val);
253 void fill(const cytnx_int32 &val);
254 void fill(const cytnx_uint32 &val);
255 void fill(const cytnx_int16 &val);
256 void fill(const cytnx_uint16 &val);
257 void fill(const cytnx_bool &val);
258 void set_zeros();
259 void resize(const cytnx_uint64 &newsize);
260
261 void append(const Scalar &val);
262 void append(const cytnx_complex128 &val);
263 void append(const cytnx_complex64 &val);
264 void append(const cytnx_double &val);
265 void append(const cytnx_float &val);
266 void append(const cytnx_int64 &val);
267 void append(const cytnx_uint64 &val);
268 void append(const cytnx_int32 &val);
269 void append(const cytnx_uint32 &val);
270 void append(const cytnx_int16 &val);
271 void append(const cytnx_uint16 &val);
272 void append(const cytnx_bool &val);
273 Scalar get_item(const cytnx_uint64 &in) const;
274
275 void set_item(const cytnx_uint64 &idx, const Scalar &val);
276 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
277 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
278 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
279 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
280 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
281 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
282 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
283 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
284 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
285 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
286 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
287 };
289
291 class DoubleStorage : public Storage_base {
292 public:
293 DoubleStorage() { this->dtype = Type.Double; };
294 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero=true);
295 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
296 const bool &iscap = false, const unsigned long long &cap_in = 0);
297 boost::intrusive_ptr<Storage_base> _create_new_sametype();
298 boost::intrusive_ptr<Storage_base> clone();
299 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
300 const std::vector<cytnx_uint64> &mapper,
301 const std::vector<cytnx_uint64> &invmapper);
302 void 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 to_(const int &device);
306 boost::intrusive_ptr<Storage_base> to(const int &device);
307 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
308 const std::vector<cytnx_uint64> &mapper = {});
309 void print_elems();
310
311 boost::intrusive_ptr<Storage_base> real();
312 boost::intrusive_ptr<Storage_base> imag();
313
314 // generators:
315 void fill(const cytnx_complex128 &val);
316 void fill(const cytnx_complex64 &val);
317 void fill(const cytnx_double &val);
318 void fill(const cytnx_float &val);
319 void fill(const cytnx_int64 &val);
320 void fill(const cytnx_uint64 &val);
321 void fill(const cytnx_int32 &val);
322 void fill(const cytnx_uint32 &val);
323 void fill(const cytnx_int16 &val);
324 void fill(const cytnx_uint16 &val);
325 void fill(const cytnx_bool &val);
326 void set_zeros();
327 void resize(const cytnx_uint64 &newsize);
328
329 void append(const Scalar &val);
330 void append(const cytnx_complex128 &val);
331 void append(const cytnx_complex64 &val);
332 void append(const cytnx_double &val);
333 void append(const cytnx_float &val);
334 void append(const cytnx_int64 &val);
335 void append(const cytnx_uint64 &val);
336 void append(const cytnx_int32 &val);
337 void append(const cytnx_uint32 &val);
338 void append(const cytnx_int16 &val);
339 void append(const cytnx_uint16 &val);
340 void append(const cytnx_bool &val);
341 Scalar get_item(const cytnx_uint64 &in) const;
342
343 void set_item(const cytnx_uint64 &idx, const Scalar &val);
344 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
345 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
346 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
347 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
348 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
349 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
350 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
351 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
352 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
353 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
354 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
355 };
357
359 class ComplexDoubleStorage : public Storage_base {
360 public:
361 ComplexDoubleStorage() { this->dtype = Type.ComplexDouble; };
362 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero = true);
363 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
364 const bool &iscap = false, const unsigned long long &cap_in = 0);
365 boost::intrusive_ptr<Storage_base> _create_new_sametype();
366 boost::intrusive_ptr<Storage_base> clone();
367 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
368 const std::vector<cytnx_uint64> &mapper,
369 const std::vector<cytnx_uint64> &invmapper);
370 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
371 const std::vector<cytnx_uint64> &mapper,
372 const std::vector<cytnx_uint64> &invmapper);
373 void to_(const int &device);
374 boost::intrusive_ptr<Storage_base> to(const int &device);
375 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
376 const std::vector<cytnx_uint64> &mapper = {});
377 void print_elems();
378
379 boost::intrusive_ptr<Storage_base> real();
380 boost::intrusive_ptr<Storage_base> imag();
381
382 // generators:
383 void fill(const cytnx_complex128 &val);
384 void fill(const cytnx_complex64 &val);
385 void fill(const cytnx_double &val);
386 void fill(const cytnx_float &val);
387 void fill(const cytnx_int64 &val);
388 void fill(const cytnx_uint64 &val);
389 void fill(const cytnx_int32 &val);
390 void fill(const cytnx_uint32 &val);
391 void fill(const cytnx_int16 &val);
392 void fill(const cytnx_uint16 &val);
393 void fill(const cytnx_bool &val);
394 void set_zeros();
395 void resize(const cytnx_uint64 &newsize);
396
397 void append(const Scalar &val);
398 void append(const cytnx_complex128 &val);
399 void append(const cytnx_complex64 &val);
400 void append(const cytnx_double &val);
401 void append(const cytnx_float &val);
402 void append(const cytnx_int64 &val);
403 void append(const cytnx_uint64 &val);
404 void append(const cytnx_int32 &val);
405 void append(const cytnx_uint32 &val);
406 void append(const cytnx_int16 &val);
407 void append(const cytnx_uint16 &val);
408 void append(const cytnx_bool &val);
409 Scalar get_item(const cytnx_uint64 &in) const;
410
411 void set_item(const cytnx_uint64 &idx, const Scalar &val);
412 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
413 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
414 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
415 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
416 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
417 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
418 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
419 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
420 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
421 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
422 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
423 };
425
427 class ComplexFloatStorage : public Storage_base {
428 public:
429 ComplexFloatStorage() { this->dtype = Type.ComplexFloat; };
430 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero = true);
431 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
432 const bool &iscap = false, const unsigned long long &cap_in = 0);
433 boost::intrusive_ptr<Storage_base> _create_new_sametype();
434 boost::intrusive_ptr<Storage_base> clone();
435 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
436 const std::vector<cytnx_uint64> &mapper,
437 const std::vector<cytnx_uint64> &invmapper);
438 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
439 const std::vector<cytnx_uint64> &mapper,
440 const std::vector<cytnx_uint64> &invmapper);
441 void to_(const int &device);
442 boost::intrusive_ptr<Storage_base> to(const int &device);
443 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
444 const std::vector<cytnx_uint64> &mapper = {});
445 void print_elems();
446
447 boost::intrusive_ptr<Storage_base> real();
448 boost::intrusive_ptr<Storage_base> imag();
449
450 // generators:
451 void fill(const cytnx_complex128 &val);
452 void fill(const cytnx_complex64 &val);
453 void fill(const cytnx_double &val);
454 void fill(const cytnx_float &val);
455 void fill(const cytnx_int64 &val);
456 void fill(const cytnx_uint64 &val);
457 void fill(const cytnx_int32 &val);
458 void fill(const cytnx_uint32 &val);
459 void fill(const cytnx_int16 &val);
460 void fill(const cytnx_uint16 &val);
461 void fill(const cytnx_bool &val);
462 void set_zeros();
463 void resize(const cytnx_uint64 &newsize);
464
465 void append(const Scalar &val);
466 void append(const cytnx_complex128 &val);
467 void append(const cytnx_complex64 &val);
468 void append(const cytnx_double &val);
469 void append(const cytnx_float &val);
470 void append(const cytnx_int64 &val);
471 void append(const cytnx_uint64 &val);
472 void append(const cytnx_int32 &val);
473 void append(const cytnx_uint32 &val);
474 void append(const cytnx_int16 &val);
475 void append(const cytnx_uint16 &val);
476 void append(const cytnx_bool &val);
477 Scalar get_item(const cytnx_uint64 &in) const;
478
479 void set_item(const cytnx_uint64 &idx, const Scalar &val);
480 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
481 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
482 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
483 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
484 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
485 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
486 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
487 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
488 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
489 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
490 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
491 };
493
495 class Int64Storage : public Storage_base {
496 public:
497 Int64Storage() { this->dtype = Type.Int64; };
498 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero = true);
499 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
500 const bool &iscap = false, const unsigned long long &cap_in = 0);
501 boost::intrusive_ptr<Storage_base> _create_new_sametype();
502 boost::intrusive_ptr<Storage_base> clone();
503 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
504 const std::vector<cytnx_uint64> &mapper,
505 const std::vector<cytnx_uint64> &invmapper);
506 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
507 const std::vector<cytnx_uint64> &mapper,
508 const std::vector<cytnx_uint64> &invmapper);
509 void to_(const int &device);
510 boost::intrusive_ptr<Storage_base> to(const int &device);
511 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
512 const std::vector<cytnx_uint64> &mapper = {});
513 void print_elems();
514
515 boost::intrusive_ptr<Storage_base> real();
516 boost::intrusive_ptr<Storage_base> imag();
517
518 // generators:
519 void fill(const cytnx_complex128 &val);
520 void fill(const cytnx_complex64 &val);
521 void fill(const cytnx_double &val);
522 void fill(const cytnx_float &val);
523 void fill(const cytnx_int64 &val);
524 void fill(const cytnx_uint64 &val);
525 void fill(const cytnx_int32 &val);
526 void fill(const cytnx_uint32 &val);
527 void fill(const cytnx_int16 &val);
528 void fill(const cytnx_uint16 &val);
529 void fill(const cytnx_bool &val);
530 void set_zeros();
531 void resize(const cytnx_uint64 &newsize);
532
533 void append(const Scalar &val);
534 void append(const cytnx_complex128 &val);
535 void append(const cytnx_complex64 &val);
536 void append(const cytnx_double &val);
537 void append(const cytnx_float &val);
538 void append(const cytnx_int64 &val);
539 void append(const cytnx_uint64 &val);
540 void append(const cytnx_int32 &val);
541 void append(const cytnx_uint32 &val);
542 void append(const cytnx_int16 &val);
543 void append(const cytnx_uint16 &val);
544 void append(const cytnx_bool &val);
545 Scalar get_item(const cytnx_uint64 &in) const;
546
547 void set_item(const cytnx_uint64 &idx, const Scalar &val);
548 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
549 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
550 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
551 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
552 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
553 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
554 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
555 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
556 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
557 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
558 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
559 };
561
563 class Uint64Storage : public Storage_base {
564 public:
565 Uint64Storage() { this->dtype = Type.Uint64; };
566 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero = true);
567 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
568 const bool &iscap = false, const unsigned long long &cap_in = 0);
569 boost::intrusive_ptr<Storage_base> _create_new_sametype();
570 boost::intrusive_ptr<Storage_base> clone();
571 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
572 const std::vector<cytnx_uint64> &mapper,
573 const std::vector<cytnx_uint64> &invmapper);
574 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
575 const std::vector<cytnx_uint64> &mapper,
576 const std::vector<cytnx_uint64> &invmapper);
577 void to_(const int &device);
578 boost::intrusive_ptr<Storage_base> to(const int &device);
579 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
580 const std::vector<cytnx_uint64> &mapper = {});
581 void print_elems();
582
583 boost::intrusive_ptr<Storage_base> real();
584 boost::intrusive_ptr<Storage_base> imag();
585
586 // generators:
587 void fill(const cytnx_complex128 &val);
588 void fill(const cytnx_complex64 &val);
589 void fill(const cytnx_double &val);
590 void fill(const cytnx_float &val);
591 void fill(const cytnx_int64 &val);
592 void fill(const cytnx_uint64 &val);
593 void fill(const cytnx_int32 &val);
594 void fill(const cytnx_uint32 &val);
595 void fill(const cytnx_int16 &val);
596 void fill(const cytnx_uint16 &val);
597 void fill(const cytnx_bool &val);
598 void set_zeros();
599 void resize(const cytnx_uint64 &newsize);
600
601 void append(const Scalar &val);
602 void append(const cytnx_complex128 &val);
603 void append(const cytnx_complex64 &val);
604 void append(const cytnx_double &val);
605 void append(const cytnx_float &val);
606 void append(const cytnx_int64 &val);
607 void append(const cytnx_uint64 &val);
608 void append(const cytnx_int32 &val);
609 void append(const cytnx_uint32 &val);
610 void append(const cytnx_int16 &val);
611 void append(const cytnx_uint16 &val);
612 void append(const cytnx_bool &val);
613 Scalar get_item(const cytnx_uint64 &in) const;
614
615 void set_item(const cytnx_uint64 &idx, const Scalar &val);
616 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
617 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
618 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
619 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
620 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
621 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
622 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
623 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
624 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
625 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
626 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
627 };
630 class Int32Storage : public Storage_base {
631 public:
632 Int32Storage() { this->dtype = Type.Int32; };
633 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero = true);
634 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
635 const bool &iscap = false, const unsigned long long &cap_in = 0);
636 boost::intrusive_ptr<Storage_base> _create_new_sametype();
637 boost::intrusive_ptr<Storage_base> clone();
638 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
639 const std::vector<cytnx_uint64> &mapper,
640 const std::vector<cytnx_uint64> &invmapper);
641 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
642 const std::vector<cytnx_uint64> &mapper,
643 const std::vector<cytnx_uint64> &invmapper);
644 void to_(const int &device);
645 boost::intrusive_ptr<Storage_base> to(const int &device);
646 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
647 const std::vector<cytnx_uint64> &mapper = {});
648 void print_elems();
649
650 boost::intrusive_ptr<Storage_base> real();
651 boost::intrusive_ptr<Storage_base> imag();
652
653 // generators:
654 void fill(const cytnx_complex128 &val);
655 void fill(const cytnx_complex64 &val);
656 void fill(const cytnx_double &val);
657 void fill(const cytnx_float &val);
658 void fill(const cytnx_int64 &val);
659 void fill(const cytnx_uint64 &val);
660 void fill(const cytnx_int32 &val);
661 void fill(const cytnx_uint32 &val);
662 void fill(const cytnx_int16 &val);
663 void fill(const cytnx_uint16 &val);
664 void fill(const cytnx_bool &val);
665 void set_zeros();
666 void resize(const cytnx_uint64 &newsize);
667 void append(const Scalar &val);
668 void append(const cytnx_complex128 &val);
669 void append(const cytnx_complex64 &val);
670 void append(const cytnx_double &val);
671 void append(const cytnx_float &val);
672 void append(const cytnx_int64 &val);
673 void append(const cytnx_uint64 &val);
674 void append(const cytnx_int32 &val);
675 void append(const cytnx_uint32 &val);
676 void append(const cytnx_int16 &val);
677 void append(const cytnx_uint16 &val);
678 void append(const cytnx_bool &val);
679 Scalar get_item(const cytnx_uint64 &in) const;
680
681 void set_item(const cytnx_uint64 &idx, const Scalar &val);
682 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
683 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
684 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
685 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
686 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
687 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
688 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
689 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
690 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
691 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
692 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
693 };
695
697 class Uint32Storage : public Storage_base {
698 public:
699 Uint32Storage() { this->dtype = Type.Uint32; };
700 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero= true);
701 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
702 const bool &iscap = false, const unsigned long long &cap_in = 0);
703 boost::intrusive_ptr<Storage_base> _create_new_sametype();
704 boost::intrusive_ptr<Storage_base> clone();
705 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
706 const std::vector<cytnx_uint64> &mapper,
707 const std::vector<cytnx_uint64> &invmapper);
708 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
709 const std::vector<cytnx_uint64> &mapper,
710 const std::vector<cytnx_uint64> &invmapper);
711 void to_(const int &device);
712 boost::intrusive_ptr<Storage_base> to(const int &device);
713 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
714 const std::vector<cytnx_uint64> &mapper = {});
715 void print_elems();
716
717 boost::intrusive_ptr<Storage_base> real();
718 boost::intrusive_ptr<Storage_base> imag();
719
720 // generators:
721 void fill(const cytnx_complex128 &val);
722 void fill(const cytnx_complex64 &val);
723 void fill(const cytnx_double &val);
724 void fill(const cytnx_float &val);
725 void fill(const cytnx_int64 &val);
726 void fill(const cytnx_uint64 &val);
727 void fill(const cytnx_int32 &val);
728 void fill(const cytnx_uint32 &val);
729 void fill(const cytnx_int16 &val);
730 void fill(const cytnx_uint16 &val);
731 void fill(const cytnx_bool &val);
732 void set_zeros();
733 void resize(const cytnx_uint64 &newsize);
734 void append(const Scalar &val);
735 void append(const cytnx_complex128 &val);
736 void append(const cytnx_complex64 &val);
737 void append(const cytnx_double &val);
738 void append(const cytnx_float &val);
739 void append(const cytnx_int64 &val);
740 void append(const cytnx_uint64 &val);
741 void append(const cytnx_int32 &val);
742 void append(const cytnx_uint32 &val);
743 void append(const cytnx_int16 &val);
744 void append(const cytnx_uint16 &val);
745 void append(const cytnx_bool &val);
746 Scalar get_item(const cytnx_uint64 &in) const;
747
748 void set_item(const cytnx_uint64 &idx, const Scalar &val);
749 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
750 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
751 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
752 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
753 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
754 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
755 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
756 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
757 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
758 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
759 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
760 };
762
764 class Uint16Storage : public Storage_base {
765 public:
766 Uint16Storage() { this->dtype = Type.Uint16; };
767 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero=true);
768 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
769 const bool &iscap = false, const unsigned long long &cap_in = 0);
770 boost::intrusive_ptr<Storage_base> _create_new_sametype();
771 boost::intrusive_ptr<Storage_base> clone();
772 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
773 const std::vector<cytnx_uint64> &mapper,
774 const std::vector<cytnx_uint64> &invmapper);
775 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
776 const std::vector<cytnx_uint64> &mapper,
777 const std::vector<cytnx_uint64> &invmapper);
778 void to_(const int &device);
779 boost::intrusive_ptr<Storage_base> to(const int &device);
780 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
781 const std::vector<cytnx_uint64> &mapper = {});
782 void print_elems();
783
784 boost::intrusive_ptr<Storage_base> real();
785 boost::intrusive_ptr<Storage_base> imag();
786
787 // generators:
788 void fill(const cytnx_complex128 &val);
789 void fill(const cytnx_complex64 &val);
790 void fill(const cytnx_double &val);
791 void fill(const cytnx_float &val);
792 void fill(const cytnx_int64 &val);
793 void fill(const cytnx_uint64 &val);
794 void fill(const cytnx_int32 &val);
795 void fill(const cytnx_uint32 &val);
796 void fill(const cytnx_int16 &val);
797 void fill(const cytnx_uint16 &val);
798 void fill(const cytnx_bool &val);
799 void set_zeros();
800 void resize(const cytnx_uint64 &newsize);
801
802 void append(const Scalar &val);
803 void append(const cytnx_complex128 &val);
804 void append(const cytnx_complex64 &val);
805 void append(const cytnx_double &val);
806 void append(const cytnx_float &val);
807 void append(const cytnx_int64 &val);
808 void append(const cytnx_uint64 &val);
809 void append(const cytnx_int32 &val);
810 void append(const cytnx_uint32 &val);
811 void append(const cytnx_int16 &val);
812 void append(const cytnx_uint16 &val);
813 void append(const cytnx_bool &val);
814 Scalar get_item(const cytnx_uint64 &in) const;
815
816 void set_item(const cytnx_uint64 &idx, const Scalar &val);
817 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
818 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
819 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
820 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
821 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
822 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
823 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
824 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
825 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
826 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
827 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
828 };
830
832 class Int16Storage : public Storage_base {
833 public:
834 Int16Storage() { this->dtype = Type.Int16; };
835 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero=true);
836 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
837 const bool &iscap = false, const unsigned long long &cap_in = 0);
838 boost::intrusive_ptr<Storage_base> _create_new_sametype();
839 boost::intrusive_ptr<Storage_base> clone();
840 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
841 const std::vector<cytnx_uint64> &mapper,
842 const std::vector<cytnx_uint64> &invmapper);
843 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
844 const std::vector<cytnx_uint64> &mapper,
845 const std::vector<cytnx_uint64> &invmapper);
846 void to_(const int &device);
847 boost::intrusive_ptr<Storage_base> to(const int &device);
848 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
849 const std::vector<cytnx_uint64> &mapper = {});
850 void print_elems();
851
852 boost::intrusive_ptr<Storage_base> real();
853 boost::intrusive_ptr<Storage_base> imag();
854
855 // generators:
856 void fill(const cytnx_complex128 &val);
857 void fill(const cytnx_complex64 &val);
858 void fill(const cytnx_double &val);
859 void fill(const cytnx_float &val);
860 void fill(const cytnx_int64 &val);
861 void fill(const cytnx_uint64 &val);
862 void fill(const cytnx_int32 &val);
863 void fill(const cytnx_uint32 &val);
864 void fill(const cytnx_int16 &val);
865 void fill(const cytnx_uint16 &val);
866 void fill(const cytnx_bool &val);
867 void set_zeros();
868 void resize(const cytnx_uint64 &newsize);
869 void append(const Scalar &val);
870 void append(const cytnx_complex128 &val);
871 void append(const cytnx_complex64 &val);
872 void append(const cytnx_double &val);
873 void append(const cytnx_float &val);
874 void append(const cytnx_int64 &val);
875 void append(const cytnx_uint64 &val);
876 void append(const cytnx_int32 &val);
877 void append(const cytnx_uint32 &val);
878 void append(const cytnx_int16 &val);
879 void append(const cytnx_uint16 &val);
880 void append(const cytnx_bool &val);
881 Scalar get_item(const cytnx_uint64 &in) const;
882
883 void set_item(const cytnx_uint64 &idx, const Scalar &val);
884 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
885 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
886 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
887 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
888 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
889 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
890 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
891 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
892 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
893 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
894 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
895 };
897
899 class BoolStorage : public Storage_base {
900 public:
901 BoolStorage() { this->dtype = Type.Bool; };
902 void Init(const unsigned long long &len_in, const int &device = -1, const bool &init_zero=true);
903 void _Init_byptr(void *rawptr, const unsigned long long &len_in, const int &device = -1,
904 const bool &iscap = false, const unsigned long long &cap_in = 0);
905 boost::intrusive_ptr<Storage_base> _create_new_sametype();
906 boost::intrusive_ptr<Storage_base> clone();
907 boost::intrusive_ptr<Storage_base> Move_memory(const std::vector<cytnx_uint64> &old_shape,
908 const std::vector<cytnx_uint64> &mapper,
909 const std::vector<cytnx_uint64> &invmapper);
910 void Move_memory_(const std::vector<cytnx_uint64> &old_shape,
911 const std::vector<cytnx_uint64> &mapper,
912 const std::vector<cytnx_uint64> &invmapper);
913 void to_(const int &device);
914 boost::intrusive_ptr<Storage_base> to(const int &device);
915 void PrintElem_byShape(std::ostream &os, const std::vector<cytnx_uint64> &shape,
916 const std::vector<cytnx_uint64> &mapper = {});
917 void print_elems();
918
919 boost::intrusive_ptr<Storage_base> real();
920 boost::intrusive_ptr<Storage_base> imag();
921
922 // generators:
923 void fill(const cytnx_complex128 &val);
924 void fill(const cytnx_complex64 &val);
925 void fill(const cytnx_double &val);
926 void fill(const cytnx_float &val);
927 void fill(const cytnx_int64 &val);
928 void fill(const cytnx_uint64 &val);
929 void fill(const cytnx_int32 &val);
930 void fill(const cytnx_uint32 &val);
931 void fill(const cytnx_int16 &val);
932 void fill(const cytnx_uint16 &val);
933 void fill(const cytnx_bool &val);
934 void set_zeros();
935 void resize(const cytnx_uint64 &newsize);
936 void append(const Scalar &val);
937 void append(const cytnx_complex128 &val);
938 void append(const cytnx_complex64 &val);
939 void append(const cytnx_double &val);
940 void append(const cytnx_float &val);
941 void append(const cytnx_int64 &val);
942 void append(const cytnx_uint64 &val);
943 void append(const cytnx_int32 &val);
944 void append(const cytnx_uint32 &val);
945 void append(const cytnx_int16 &val);
946 void append(const cytnx_uint16 &val);
947 void append(const cytnx_bool &val);
948 Scalar get_item(const cytnx_uint64 &in) const;
949
950 void set_item(const cytnx_uint64 &idx, const Scalar &val);
951 void set_item(const cytnx_uint64 &idx, const cytnx_complex128 &val);
952 void set_item(const cytnx_uint64 &idx, const cytnx_complex64 &val);
953 void set_item(const cytnx_uint64 &idx, const cytnx_double &val);
954 void set_item(const cytnx_uint64 &idx, const cytnx_float &val);
955 void set_item(const cytnx_uint64 &idx, const cytnx_int64 &val);
956 void set_item(const cytnx_uint64 &idx, const cytnx_uint64 &val);
957 void set_item(const cytnx_uint64 &idx, const cytnx_int32 &val);
958 void set_item(const cytnx_uint64 &idx, const cytnx_uint32 &val);
959 void set_item(const cytnx_uint64 &idx, const cytnx_int16 &val);
960 void set_item(const cytnx_uint64 &idx, const cytnx_uint16 &val);
961 void set_item(const cytnx_uint64 &idx, const cytnx_bool &val);
962 };
964
966 typedef boost::intrusive_ptr<Storage_base> (*pStorage_init)();
968 inline boost::intrusive_ptr<Storage_base> SIInit_cd() {
969 boost::intrusive_ptr<Storage_base> out(new ComplexDoubleStorage());
970 return out;
971 }
972 inline boost::intrusive_ptr<Storage_base> SIInit_cf() {
973 boost::intrusive_ptr<Storage_base> out(new ComplexFloatStorage());
974 return out;
975 }
976 inline boost::intrusive_ptr<Storage_base> SIInit_d() {
977 boost::intrusive_ptr<Storage_base> out(new DoubleStorage());
978 return out;
979 }
980 inline boost::intrusive_ptr<Storage_base> SIInit_f() {
981 boost::intrusive_ptr<Storage_base> out(new FloatStorage());
982 return out;
983 }
984 inline boost::intrusive_ptr<Storage_base> SIInit_u64() {
985 boost::intrusive_ptr<Storage_base> out(new Uint64Storage());
986 return out;
987 }
988 inline boost::intrusive_ptr<Storage_base> SIInit_i64() {
989 boost::intrusive_ptr<Storage_base> out(new Int64Storage());
990 return out;
991 }
992 inline boost::intrusive_ptr<Storage_base> SIInit_u32() {
993 boost::intrusive_ptr<Storage_base> out(new Uint32Storage());
994 return out;
995 }
996 inline boost::intrusive_ptr<Storage_base> SIInit_i32() {
997 boost::intrusive_ptr<Storage_base> out(new Int32Storage());
998 return out;
999 }
1000 inline boost::intrusive_ptr<Storage_base> SIInit_u16() {
1001 boost::intrusive_ptr<Storage_base> out(new Uint16Storage());
1002 return out;
1003 }
1004 inline boost::intrusive_ptr<Storage_base> SIInit_i16() {
1005 boost::intrusive_ptr<Storage_base> out(new Int16Storage());
1006 return out;
1007 }
1008 inline boost::intrusive_ptr<Storage_base> SIInit_b() {
1009 boost::intrusive_ptr<Storage_base> out(new BoolStorage());
1010 return out;
1011 }
1013 class Storage_init_interface : public Type_class {
1014 public:
1015 // std::vector<pStorage_init> USIInit;
1016 inline static pStorage_init USIInit[N_Type];
1017 inline static bool inited = false;
1018 Storage_init_interface(){
1019 if(!inited){
1020 USIInit[this->Double] = SIInit_d;
1021 USIInit[this->Float] = SIInit_f;
1022 USIInit[this->ComplexDouble] = SIInit_cd;
1023 USIInit[this->ComplexFloat] = SIInit_cf;
1024 USIInit[this->Uint64] = SIInit_u64;
1025 USIInit[this->Int64] = SIInit_i64;
1026 USIInit[this->Uint32] = SIInit_u32;
1027 USIInit[this->Int32] = SIInit_i32;
1028 USIInit[this->Uint16] = SIInit_u16;
1029 USIInit[this->Int16] = SIInit_i16;
1030 USIInit[this->Bool] = SIInit_b;
1031 inited = true;
1032 }
1033 }
1034 };
1035 extern Storage_init_interface __SII;
1037
1039 class Storage {
1040 private:
1041 // Interface:
1042 // Storage_init_interface __SII;
1043
1044 public:
1046 boost::intrusive_ptr<Storage_base> _impl;
1048
1066 void Init(const unsigned long long &size, const unsigned int &dtype = Type.Double,
1067 int device = -1, const bool &init_zero = true) {
1068 cytnx_error_msg(dtype >= N_Type, "%s", "[ERROR] invalid argument: dtype");
1069 this->_impl = __SII.USIInit[dtype]();
1070 this->_impl->Init(size, device,init_zero);
1071 }
1072 // void _Init_byptr(void *rawptr, const unsigned long long &len_in, const unsigned int &dtype = Type.Double, const int &device = -1,
1073 // const bool &iscap = false, const unsigned long long &cap_in = 0){
1074 // cytnx_error_msg(dtype >= N_Type, "%s", "[ERROR] invalid argument: dtype");
1075 // this->_impl = __SII.USIInit[dtype]();
1076 // this->_impl->_Init_byptr(rawptr, len_in, device, iscap, cap_in);
1077 // }
1078
1086 Storage(const unsigned long long &size, const unsigned int &dtype = Type.Double,
1087 int device = -1, const bool &init_zero=true)
1088 : _impl(new Storage_base()) {
1090 }
1091 // Storage(void *rawptr, const unsigned long long &len_in, const unsigned int &dtype = Type.Double, const int &device = -1,
1092 // const bool &iscap = false, const unsigned long long &cap_in = 0)
1093 // : _impl(new Storage_base()){
1094 // _Init_byptr(rawptr,len_in,dtype,device,iscap,cap_in);
1095 // }
1096
1100 Storage() : _impl(new Storage_base()){};
1102 Storage(boost::intrusive_ptr<Storage_base> in_impl) { this->_impl = in_impl; }
1103 Storage(const Storage &rhs) { this->_impl = rhs._impl; }
1104
1105 template <class Tp>
1106 Storage(const std::vector<Tp> &rhs) {
1107 this->_from_vector(rhs, -1);
1108 }
1109 template <class Tp>
1110 Storage(const std::initializer_list<Tp> &rhs) {
1111 this->_from_vector(std::vector<Tp>(rhs), -1);
1112 }
1113
1114 Storage &operator=(const Storage &rhs) {
1115 this->_impl = rhs._impl;
1116 return *this;
1117 }
1118
1120
1122 void _Save(std::fstream &f) const;
1123 void _Load(std::fstream &f);
1124 void _Loadbinary(std::fstream &f, const unsigned int &dtype, const cytnx_uint64 &Nelem);
1125 void _Savebinary(std::fstream &f) const;
1126
1128
1137 void Save(const std::string &fname) const;
1138
1142 void Save(const char *fname) const;
1143 void Tofile(const std::string &fname) const;
1144 void Tofile(const char *fname) const;
1145 void Tofile(std::fstream &f) const;
1146
1154 static Storage Load(const std::string &fname);
1155
1159 static Storage Load(const char *fname);
1160 static Storage Fromfile(const std::string &fname, const unsigned int &dtype,
1161 const cytnx_int64 &count = -1);
1162 static Storage Fromfile(const char *fname, const unsigned int &dtype,
1163 const cytnx_int64 &count = -1);
1164
1185 Storage astype(const unsigned int &new_type) const { return this->_impl->astype(new_type); }
1186
1191 const unsigned int &dtype() const { return this->_impl->dtype; }
1192
1197 const std::string dtype_str() const {
1198 std::string out = this->_impl->dtype_str();
1199 return out;
1200 }
1205 const int &device() const { return this->_impl->device; }
1206
1211 const std::string device_str() const {
1212 std::string out = this->_impl->device_str();
1213 return out;
1214 }
1215
1221 template <class T>
1222 void append(const T &val) {
1223 return this->_impl->append(val);
1224 }
1225
1227 template <class T> // this is c++ only
1228 T &at(const cytnx_uint64 &idx) const {
1229 return this->_impl->at<T>(idx);
1230 }
1231
1232 const Scalar::Sproxy at(const cytnx_uint64 &idx) const {
1233 Scalar::Sproxy out(this->_impl, idx);
1234 return out;
1235 }
1236 Scalar::Sproxy at(const cytnx_uint64 &idx) {
1237 Scalar::Sproxy out(this->_impl, idx);
1238 return out;
1239 }
1240
1241 template <class T> // this is c++ only
1242 T &back() const {
1243 return this->_impl->back<T>();
1244 }
1245
1246 const Scalar::Sproxy back() const {
1247 Scalar::Sproxy out(this->_impl, this->size() - 1);
1248 return out;
1249 }
1250 Scalar::Sproxy back() {
1251 Scalar::Sproxy out(this->_impl, this->size() - 1);
1252 return out;
1253 }
1254
1255 template <class T> // this is c++ only
1256 T *data() const {
1257 return this->_impl->data<T>();
1258 }
1259
1260 void *data() const { return this->_impl->data(); }
1262
1267 void resize(const cytnx_uint64 &newsize) { this->_impl->resize(newsize); }
1268
1274 void to_(const int &device) { this->_impl->to_(device); }
1275
1283 Storage to(const int &device) { return Storage(this->_impl->to(device)); }
1284
1300 Storage clone() const { return Storage(this->_impl->clone()); }
1301
1307 const unsigned long long &size() const { return this->_impl->len; }
1308
1314 const unsigned long long &capacity() const { return this->_impl->cap; }
1315
1320 void print_info() const { this->_impl->print_info(); }
1322 // this is a redundant function
1323 void print() const { this->_impl->print(); }
1325
1332 void set_zeros() { this->_impl->set_zeros(); }
1333
1351 bool operator==(const Storage &rhs);
1352
1356 bool operator!=(const Storage &rhs);
1357
1363 template <class T>
1364 void fill(const T &val) {
1365 this->_impl->fill(val);
1366 }
1367
1373 template <class T>
1374 static Storage from_vector(const std::vector<T> &vin, const int device = -1) {
1375 Storage out;
1376 out._from_vector(vin, device);
1377 return out;
1378 }
1379
1380 /*
1381 @brief convert a Storage to C++ vector.
1382
1383 [Note]
1384 This function is C++ only
1385 */
1386 /*
1387 template <class T>
1388 std::vector<T> vector() {
1389 T tmp;
1390 cytnx_error_msg(Type.cy_typeid(tmp) != this->dtype(),
1391 "[ERROR] the dtype of current Storage does not match assigned vector type.%s",
1392 "\n");
1393
1394 std::vector<T> out(this->size());
1395 Storage S;
1396 if (this->device() != Device.cpu) {
1397 S = this->to(Device.cpu);
1398 memcpy(&out[0], S.data(), sizeof(T) * this->size());
1399 } else {
1400 memcpy(&out[0], this->data(), sizeof(T) * this->size());
1401 }
1402
1403 return out;
1404 }
1405 */
1406 template <class T>
1407 std::vector<T> vector();
1408
1410
1411 template <class T>
1412 void _from_vector(const std::vector<T> &vin, const int device = -1) {
1413 // auto dispatch:
1414 // check:
1415 cytnx_error_msg(1, "[FATAL] ERROR unsupport type%s", "\n");
1416 // this->_impl->Init(vin.size(),device);
1417 // memcpy(this->_impl->Mem,&vin[0],sizeof(T)*vin.size());
1418 }
1419
1420 void _from_vector(const std::vector<cytnx_complex128> &vin, const int device = -1) {
1421 this->_impl = __SII.USIInit[Type.ComplexDouble]();
1422 this->_impl->Init(vin.size(), device);
1423 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_complex128) * vin.size());
1424 }
1425 void _from_vector(const std::vector<cytnx_complex64> &vin, const int device = -1) {
1426 this->_impl = __SII.USIInit[Type.ComplexFloat]();
1427 this->_impl->Init(vin.size(), device);
1428 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_complex64) * vin.size());
1429 }
1430 void _from_vector(const std::vector<cytnx_double> &vin, const int device = -1) {
1431 this->_impl = __SII.USIInit[Type.Double]();
1432 this->_impl->Init(vin.size(), device);
1433 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_double) * vin.size());
1434 }
1435 void _from_vector(const std::vector<cytnx_float> &vin, const int device = -1) {
1436 this->_impl = __SII.USIInit[Type.Float]();
1437 this->_impl->Init(vin.size(), device);
1438 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_float) * vin.size());
1439 }
1440 void _from_vector(const std::vector<cytnx_uint64> &vin, const int device = -1) {
1441 this->_impl = __SII.USIInit[Type.Uint64]();
1442 this->_impl->Init(vin.size(), device);
1443 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint64) * vin.size());
1444 }
1445 void _from_vector(const std::vector<cytnx_int64> &vin, const int device = -1) {
1446 this->_impl = __SII.USIInit[Type.Int64]();
1447 this->_impl->Init(vin.size(), device);
1448 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int64) * vin.size());
1449 }
1450 void _from_vector(const std::vector<cytnx_uint32> &vin, const int device = -1) {
1451 this->_impl = __SII.USIInit[Type.Uint32]();
1452 this->_impl->Init(vin.size(), device);
1453 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint32) * vin.size());
1454 }
1455 void _from_vector(const std::vector<cytnx_int32> &vin, const int device = -1) {
1456 this->_impl = __SII.USIInit[Type.Int32]();
1457 this->_impl->Init(vin.size(), device);
1458 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int32) * vin.size());
1459 }
1460 void _from_vector(const std::vector<cytnx_uint16> &vin, const int device = -1) {
1461 this->_impl = __SII.USIInit[Type.Uint16]();
1462 this->_impl->Init(vin.size(), device);
1463 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_uint16) * vin.size());
1464 }
1465 void _from_vector(const std::vector<cytnx_int16> &vin, const int device = -1) {
1466 this->_impl = __SII.USIInit[Type.Int16]();
1467 this->_impl->Init(vin.size(), device);
1468 memcpy(this->_impl->Mem, &vin[0], sizeof(cytnx_int16) * vin.size());
1469 }
1470 void _from_vector(const std::vector<cytnx_bool> &vin, const int device = -1) {
1471 this->_impl = __SII.USIInit[Type.Bool]();
1472 this->_impl->Init(vin.size(), device);
1473 this->_impl->_cpy_bool(this->_impl->Mem, vin);
1474 // memcpy(this->_impl->Mem,vin.data(),sizeof(cytnx_bool)*vin.size());
1475 }
1477
1492 Storage real() const { return Storage(this->_impl->real()); };
1493
1508 Storage imag() const { return Storage(this->_impl->imag()); };
1509
1515 Scalar get_item(const cytnx_uint64 &idx) const { return this->_impl->get_item(idx); };
1516
1522 template <class T>
1523 void set_item(const cytnx_uint64 &idx, const T &elem) {
1524 this->_impl->set_item(idx, elem);
1525 };
1526
1531 Scalar::Sproxy operator()(const cytnx_uint64 &idx);
1532 };
1533
1535 std::ostream &operator<<(std::ostream &os, const Storage &in);
1537
1538} // namespace cytnx
1539
1540#endif
A class to represent a scalar.
Definition Scalar.hpp:2470
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:1039
void to_(const int &device)
Move the current Storage to different deivce.
Definition Storage.hpp:1274
void append(const T &val)
append a value
Definition Storage.hpp:1222
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:1086
const int & device() const
the device-id of current Storage, see cytnx::Device for more details.
Definition Storage.hpp:1205
Storage real() const
Get the real part form a Complex type Storage.
Definition Storage.hpp:1492
std::vector< T > vector()
Definition Storage.cpp:323
Storage to(const int &device)
move a new Storage with same content as current Storage on different deivce.
Definition Storage.hpp:1283
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:1066
void resize(const cytnx_uint64 &newsize)
resize the current Storage.
Definition Storage.hpp:1267
void Save(const std::string &fname) const
Save current Storage to file.
Definition Storage.cpp:87
void fill(const T &val)
set all the elements to the assigned value val
Definition Storage.hpp:1364
Storage()
The default constructor of Storage class. It will create an empty Storage instance.
Definition Storage.hpp:1100
void Tofile(const std::string &fname) const
Definition Storage.cpp:106
const unsigned int & dtype() const
the dtype-id of current Storage, see cytnx::Type for more details.
Definition Storage.hpp:1191
void set_item(const cytnx_uint64 &idx, const T &elem)
Set the element at the given index.
Definition Storage.hpp:1523
const unsigned long long & capacity() const
the capacity ( no. of real elements in memory) in the Storage
Definition Storage.hpp:1314
void set_zeros()
set all the elements to zero.
Definition Storage.hpp:1332
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:1185
const unsigned long long & size() const
the size ( no. of elements ) in the Storage
Definition Storage.hpp:1307
void print_info() const
print the info of the Storage, including the device, dtype and size.
Definition Storage.hpp:1320
static Storage from_vector(const std::vector< T > &vin, const int device=-1)
renew/create a Storage using c++ vector.
Definition Storage.hpp:1374
bool operator!=(const Storage &rhs)
The not-equal operator for Storage.
Definition Storage.cpp:85
static Storage Fromfile(const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
Definition Storage.cpp:189
const std::string dtype_str() const
the dtype (std::string) of current Storage, see cytnx::Type for more details.
Definition Storage.hpp:1197
Scalar::Sproxy operator()(const cytnx_uint64 &idx)
The access operator for the Storage.
Definition Storage.cpp:317
void Tofile(std::fstream &f) const
bool operator==(const Storage &rhs)
compare two Storage
Definition Storage.cpp:19
Scalar get_item(const cytnx_uint64 &idx) const
Get the element at the given index.
Definition Storage.hpp:1515
Storage imag() const
Get the imaginary part form a Complex type Storage.
Definition Storage.hpp:1508
const std::string device_str() const
the device (std::string) of current Storage, see cytnx::Device for more details.
Definition Storage.hpp:1211
Storage clone() const
return a deep copy of the current storage.
Definition Storage.hpp:1300
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Definition Accessor.hpp:12
boost::intrusive_ptr< Storage_base > SIInit_u32()
Definition Storage.hpp:992
double cytnx_double
Definition Type.hpp:43
boost::intrusive_ptr< Storage_base > SIInit_d()
Definition Storage.hpp:976
uint32_t cytnx_uint32
Definition Type.hpp:46
boost::intrusive_ptr< Storage_base > SIInit_u64()
Definition Storage.hpp:984
std::complex< double > cytnx_complex128
Definition Type.hpp:53
float cytnx_float
Definition Type.hpp:44
std::ostream & operator<<(std::ostream &os, const Scalar &in)
The stream operator for Scalar objects.
Definition Scalar.cpp:10
boost::intrusive_ptr< Storage_base > SIInit_u16()
Definition Storage.hpp:1000
boost::intrusive_ptr< Storage_base > SIInit_f()
Definition Storage.hpp:980
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
boost::intrusive_ptr< Storage_base > SIInit_i16()
Definition Storage.hpp:1004
boost::intrusive_ptr< Storage_base > SIInit_i32()
Definition Storage.hpp:996
uint16_t cytnx_uint16
Definition Type.hpp:47
boost::intrusive_ptr< Storage_base > SIInit_i64()
Definition Storage.hpp:988
uint64_t cytnx_uint64
Definition Type.hpp:45
boost::intrusive_ptr< Storage_base > SIInit_b()
Definition Storage.hpp:1008
int64_t cytnx_int64
Definition Type.hpp:48
Storage_init_interface __SII
Definition Storage.cpp:12
Type_class Type
data type
Definition Type.cpp:23
boost::intrusive_ptr< Storage_base > SIInit_cf()
Definition Storage.hpp:972
boost::intrusive_ptr< Storage_base > SIInit_cd()
Definition Storage.hpp:968