summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/codec/c++/data.h30
-rw-r--r--modules/codec/tests/codec.cpp17
2 files changed, 47 insertions, 0 deletions
diff --git a/modules/codec/c++/data.h b/modules/codec/c++/data.h
index 365405f..2dffdbe 100644
--- a/modules/codec/c++/data.h
+++ b/modules/codec/c++/data.h
@@ -1,6 +1,7 @@
#pragma once
#include <forstio/common.h>
+#include <forstio/error.h>
#include <forstio/templates.h>
#include <cassert>
@@ -205,6 +206,35 @@ class data<schema::Array<T,Dim>, encode::Native> {
value_.resize(get_full_size());
}
+ template<size_t i = 0>
+ error_or<void> add(saw::data<T,encode::Native> data){
+ /** @todo
+ * Generally the last dimension can always accept a element so to say.
+ * Changing the others would require moving data due to the stride changing.
+ * Since the last dimension doesn't affect the stride, we don't need reordering there.
+ * But I want a quick solution for one dimension so here we are.
+ *
+ * I can always ignore strides and use a stacked std::vector
+ * std::vector<std::vector<...>> and so on.
+ * But for now I'm keeping the strides. Smaller chunks of memory aren't to bad either
+ * though.
+ * I'll probably change it to the smaller chunks
+ */
+ static_assert(Dim == 1, "Currently can't deal with higher dims");
+ static_assert(i < Dim, "Can't add to dimension. Index i larger than dimension size");
+
+ try {
+ value_.emplace_back(std::move(data));
+ }catch(const std::exception& e){
+ (void) e;
+ return make_error<err::out_of_memory>();
+ }
+
+ ++dims_.at(i);
+
+ return void_t{};
+ }
+
template<std::integral... Dims>
data(Dims... size_):
data{{static_cast<std::size_t>(size_)...}}
diff --git a/modules/codec/tests/codec.cpp b/modules/codec/tests/codec.cpp
index 2cf4587..fb599e3 100644
--- a/modules/codec/tests/codec.cpp
+++ b/modules/codec/tests/codec.cpp
@@ -61,6 +61,23 @@ SAW_TEST("One Dimensional Array") {
SAW_EXPECT(sum == 124750, std::to_string(sum) + " is not 124750. Expected that data stays correct");
}
+SAW_TEST("One dimensional Array Add"){
+ using namespace saw;
+
+ data<schema::OneDimArray, encode::Native> arr{5u};
+
+ int bar = 0;
+
+ for(size_t i = 0; i < arr.get_dim_size(0); ++i){
+ arr.at(i).set(bar++);
+ }
+
+ arr.add(7);
+
+ SAW_EXPECT(arr.size() == 6u, "Array size is not 6u. Expected that data stays correct");
+ SAW_EXPECT(arr.at(6u).get() == 7, "Array at 6u is not 7. Expected that data stays correct");
+}
+
SAW_TEST("Two Dimensional Array") {
using namespace saw;