Skip to content

Commit

Permalink
Provide ndarray indexing function
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Jun 4, 2018
1 parent 7f85952 commit b4a3fb9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cmake_policy(SET CMP0048 NEW)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
set(CMAKE_DISABLE_SOURCE_CHANGES ON)

project(asdf-cxx VERSION 2.4.1 LANGUAGES CXX)
project(asdf-cxx VERSION 2.5.0 LANGUAGES CXX)
set(PROJECT_DESCRIPTION
"asdf-cxx (Advanced Scientific Data Format), C++ implementation")
set(PROJECT_URL "https://github.com/eschnett/asdf-cxx")
Expand Down
24 changes: 24 additions & 0 deletions asdf_ndarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <yaml-cpp/yaml.h>

#include <array>
#include <cassert>
#include <cstddef>
#include <memory>
Expand Down Expand Up @@ -168,6 +169,29 @@ class ndarray {
shared_ptr<generic_blob_t> get_data() const { return data; }
shared_ptr<datatype_t> get_datatype() const { return datatype; }
vector<int64_t> get_shape() const { return shape; }
int64_t get_offset() const { return offset; }
vector<int64_t> get_strides() const { return strides; }

int64_t linear_index(const vector<int64_t> &idx) const {
int dim = shape.size();
assert(idx.size() == dim);
int64_t lin = offset;
for (int d = 0; d < dim; ++d) {
assert(idx[d] >= 0 && idx[d] < shape[d]);
lin += strides[d] * idx[d];
}
return lin;
}
template <int D> int64_t linear_index(const array<int64_t, D> &idx) const {
int dim = shape.size();
assert(D == dim);
int64_t lin = offset;
for (int d = 0; d < dim; ++d) {
assert(idx[d] >= 0 && idx[d] < shape[d]);
lin += strides[d] * idx[d];
}
return lin;
}
};

} // namespace ASDF
Expand Down

0 comments on commit b4a3fb9

Please sign in to comment.