-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0ffb8a
commit 438f293
Showing
5 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef DLUP_GEOMETRY_LAZY_ARRAY_H | ||
#define DLUP_GEOMETRY_LAZY_ARRAY_H | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <pybind11/numpy.h> | ||
#include <pybind11/pybind11.h> | ||
#include <vector> | ||
|
||
namespace py = pybind11; | ||
|
||
template <typename T> | ||
class LazyArray { | ||
public: | ||
using ComputeFunction = std::function<py::array_t<T>()>; | ||
|
||
LazyArray(ComputeFunction compute_func) : compute_func_(std::move(compute_func)), computed_(false) {} | ||
|
||
py::array_t<T> numpy() { | ||
if (!computed_) { | ||
data_ = compute_func_(); | ||
computed_ = true; | ||
} | ||
return data_; | ||
} | ||
|
||
py::array_t<T> operator*() { return numpy(); } | ||
|
||
// Changed this method to return py::array_t<T> directly | ||
py::array_t<T> py_numpy() { return numpy(); } | ||
|
||
private: | ||
ComputeFunction compute_func_; | ||
py::array_t<T> data_; | ||
bool computed_; | ||
}; | ||
|
||
template <typename T> | ||
void declare_lazy_array(py::module &m, const std::string &type_name) { | ||
py::class_<LazyArray<T>>(m, type_name.c_str()) | ||
.def(py::init<typename LazyArray<T>::ComputeFunction>()) | ||
.def("numpy", &LazyArray<T>::py_numpy) | ||
.def("__array__", &LazyArray<T>::py_numpy) | ||
.def("__repr__", [](const LazyArray<T> &) { return "<LazyArray: use numpy() or __array__() to compute>"; }); | ||
} | ||
|
||
#endif // DLUP_GEOMETRY_LAZY_ARRAY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters