Skip to content

Commit d0dd4fb

Browse files
committed
Support initialized list for chunked_array shapes
1 parent 4940c50 commit d0dd4fb

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

include/xtensor-io/xchunk_store_manager.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ namespace xt
226226
std::size_t pool_size = 1,
227227
layout_type chunk_memory_layout = XTENSOR_DEFAULT_LAYOUT);
228228

229+
template <class T, class IOH, layout_type L = XTENSOR_DEFAULT_LAYOUT, class IP = xindex_path, class EXT = empty_extension, class S>
230+
xchunked_array<xchunk_store_manager<xfile_array<T, IOH, L>, IP>, EXT>
231+
chunked_file_array(std::initializer_list<S> shape,
232+
std::initializer_list<S> chunk_shape,
233+
const std::string& path,
234+
std::size_t pool_size = 1,
235+
layout_type chunk_memory_layout = XTENSOR_DEFAULT_LAYOUT);
236+
229237
/**
230238
* Creates a chunked file array.
231239
* This function returns an uninitialized ``xchunked_array<xchunk_store_manager<xfile_array<T, IOH>>>``.
@@ -254,6 +262,15 @@ namespace xt
254262
std::size_t pool_size = 1,
255263
layout_type chunk_memory_layout = XTENSOR_DEFAULT_LAYOUT);
256264

265+
template <class T, class IOH, layout_type L = XTENSOR_DEFAULT_LAYOUT, class IP = xindex_path, class EXT = empty_extension, class S>
266+
xchunked_array<xchunk_store_manager<xfile_array<T, IOH, L>, IP>, EXT>
267+
chunked_file_array(std::initializer_list<S> shape,
268+
std::initializer_list<S> chunk_shape,
269+
const std::string& path,
270+
const T& init_value,
271+
std::size_t pool_size = 1,
272+
layout_type chunk_memory_layout = XTENSOR_DEFAULT_LAYOUT);
273+
257274
/**
258275
* Creates a chunked file array.
259276
* This function returns a ``xchunked_array<xchunk_store_manager<xfile_array<T, IOH>>>`` initialized from an expression.
@@ -365,6 +382,20 @@ namespace xt
365382
return xchunked_array<chunk_storage, EXT>(std::move(chunks), std::forward<S>(shape), std::forward<S>(chunk_shape));
366383
}
367384

385+
template <class T, class IOH, layout_type L, class IP, class EXT, class S>
386+
inline xchunked_array<xchunk_store_manager<xfile_array<T, IOH, L>, IP>, EXT>
387+
chunked_file_array(std::initializer_list<S> shape, std::initializer_list<S> chunk_shape, const std::string& path, std::size_t pool_size, layout_type chunk_memory_layout)
388+
{
389+
using sh_type = std::vector<std::size_t>;
390+
sh_type sh = xtl::make_sequence<sh_type>(shape.size());
391+
std::copy(shape.begin(), shape.end(), sh.begin());
392+
sh_type ch_sh = xtl::make_sequence<sh_type>(chunk_shape.size());
393+
std::copy(chunk_shape.begin(), chunk_shape.end(), ch_sh.begin());
394+
using chunk_storage = xchunk_store_manager<xfile_array<T, IOH, L>, IP>;
395+
chunk_storage chunks(sh, ch_sh, path, pool_size, chunk_memory_layout);
396+
return xchunked_array<chunk_storage, EXT>(std::move(chunks), std::move(sh), std::move(ch_sh));
397+
}
398+
368399
template <class T, class IOH, layout_type L, class IP, class EXT, class S>
369400
inline xchunked_array<xchunk_store_manager<xfile_array<T, IOH, L>, IP>, EXT>
370401
chunked_file_array(S&& shape, S&& chunk_shape, const std::string& path, const T& init_value,std::size_t pool_size, layout_type chunk_memory_layout)
@@ -374,6 +405,20 @@ namespace xt
374405
return xchunked_array<chunk_storage, EXT>(std::move(chunks), std::forward<S>(shape), std::forward<S>(chunk_shape));
375406
}
376407

408+
template <class T, class IOH, layout_type L, class IP, class EXT, class S>
409+
inline xchunked_array<xchunk_store_manager<xfile_array<T, IOH, L>, IP>, EXT>
410+
chunked_file_array(std::initializer_list<S> shape, std::initializer_list<S> chunk_shape, const std::string& path, const T& init_value,std::size_t pool_size, layout_type chunk_memory_layout)
411+
{
412+
using sh_type = std::vector<std::size_t>;
413+
sh_type sh = xtl::make_sequence<sh_type>(shape.size());
414+
std::copy(shape.begin(), shape.end(), sh.begin());
415+
sh_type ch_sh = xtl::make_sequence<sh_type>(chunk_shape.size());
416+
std::copy(chunk_shape.begin(), chunk_shape.end(), ch_sh.begin());
417+
using chunk_storage = xchunk_store_manager<xfile_array<T, IOH, L>, IP>;
418+
chunk_storage chunks(sh, ch_sh, path, pool_size, init_value, chunk_memory_layout);
419+
return xchunked_array<chunk_storage, EXT>(std::move(chunks), std::move(sh), std::move(ch_sh));
420+
}
421+
377422
template <class IOH, layout_type L, class IP, class EXT, class E, class S>
378423
inline xchunked_array<xchunk_store_manager<xfile_array<typename E::value_type, IOH, L>, IP>, EXT>
379424
chunked_file_array(const xexpression<E>& e, S&& chunk_shape, const std::string& path, std::size_t pool_size, layout_type chunk_memory_layout)

test/test_xchunk_store_manager.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,15 @@ namespace xt
121121
EXPECT_EQ(v, init_value);
122122
}
123123
}
124+
125+
TEST(xchunked_array, shape_initializer_list)
126+
{
127+
std::vector<size_t> shape = {4, 4};
128+
std::vector<size_t> chunk_shape = {2, 2};
129+
auto a = chunked_file_array<double, xio_disk_handler<xio_binary_config>>({4, 4}, {2, 2}, "files3");
130+
for (auto v: a)
131+
{
132+
EXPECT_EQ(v, 5.5);
133+
}
134+
}
124135
}

0 commit comments

Comments
 (0)