File arrays are file-backed, cached arrays. They implement persistent arrays stored in the local file system or in the cloud, in a variety of file formats.
#include <xtensor-io/xfile_array.hpp>
#include <xtensor-io/xio_binary.hpp>
#include <xtensor-io/xio_disk_handler.hpp>
An on-disk file array stored in binary format:
using file_array = xt::xfile_array<double, xt::xio_disk_handler<xt::xio_binary_config>>;
Since the file doesn't alreay exist, we use the init
file mode:
file_array a1("a1.bin", xt::xfile_mode::init);
std::vector<size_t> shape = {2, 2};
a1.resize(shape);
Let's assign a value to an element of the file array.
a1(0, 1) = 1.;
The in-memory element value has changed, but not the on-disk file yet. The on-disk file will change when the array is explicitly flushed, or when it is destroyed (e.g. when going out of scope).
a1.flush();
Now the on-disk file has changed.
Let's point a2
to a1
's file. For that, we use the load
file mode:
file_array a2("a1.bin", xt::xfile_mode::load);
The binary format doesn't store the shape, so we reshape the array.
a2.resize(shape);
Let's ensure that a1
and a2
are equal.
assert(xt::all(xt::equal(a1, a2)));