4.3. Increase size¶
Just like C++ vectors, we can increase the size of the Storage.
4.3.1. append¶
It is possible to append a new element to the end of the Storage. For example * In Python:
1A = cytnx.Storage(4)
2A.set_zeros();
3print(A)
4
5A.append(500)
6print(A)
In C++:
1auto A = cytnx::Storage(4);
2A.set_zeros();
3cout << A << endl;
4
5A.append(500);
6cout << A << endl;
Output >>
dtype : Double (Float64)
device: cytnx device: CPU
size : 4
[ 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 ]
dtype : Double (Float64)
device: cytnx device: CPU
size : 5
[ 0.00000e+00 0.00000e+00 0.00000e+00 0.00000e+00 5.00000e+02 ]
4.3.2. resize¶
Equivalently to vector.resize in C++, we can resize the Storage in Cytnx.
In Python:
1A = cytnx.Storage(4)
2print(A.size())
3
4A.resize(5)
5print(A.size())
In C++:
1auto A = cytnx::Storage(4);
2cout << A.size() << endl;
3
4A.resize(5);
5cout << A.size() << endl;
Output >>
4
5
Note
[Deprecated] If the size is increased in the resize operation, the additional elements will NOT be set to zero. Please use with care.
[New][v0.6.6+] Additional elements are initialized by zeros when the memory is increased by resize. This behavior is similar to that of a vector.
Tip
You can use Storage.size() to get the current number of elements in the Storage.
Internally, Cytnx allocates memory in multiples of 2. This optimizes the bandwidth use of CPU/GPU transfers and possibly increases the performance of some kernels. You can use Storage.capacity() to check the currently allocated number of elements in real memory, which might be larger than the number of elements in the Storage.