From 50b09fa9c2d7737a4373a2430481813db90bedb0 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Mon, 8 May 2023 22:28:31 +0200 Subject: [PATCH] indices + arrays --- cnorxz/base/array.pxd | 18 +++++-- cnorxz/base/base.pyx | 106 +++++++++++++++++++++++++++++++++++++++++- cnorxz/base/index.pxd | 22 +++++++++ cnorxz/base/range.pxd | 3 ++ 4 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 cnorxz/base/index.pxd diff --git a/cnorxz/base/array.pxd b/cnorxz/base/array.pxd index c11c5b0..18563df 100644 --- a/cnorxz/base/array.pxd +++ b/cnorxz/base/array.pxd @@ -2,7 +2,19 @@ from libcpp.memory cimport shared_ptr from range cimport cpp_RangeBase -cdef extern from "array/array_base.h" namespace "CNORXZ": - cdef cppclass cpp_CArrayBase "CNORXZ::CArrayBase"[T]: - size_t size() except+ +cdef extern from "array/array.h" namespace "CNORXZ": + cdef cppclass cpp_CArrayBase "CNORXZ::CArrayBase" [T]: + cpp_CArrayBase() except+ + cpp_CArrayBase(const cpp_CArrayBase[T]&) except+ + + size_t size() except+ + shared_ptr[cpp_RangeBase] range() except+ + +cdef extern from "array/array.h" namespace "CNORXZ": + cdef cppclass cpp_MArray "CNORXZ::MArray" [T] (cpp_CArrayBase[T]): + + cpp_MArray() except+ + cpp_MArray(const cpp_MArray[T]&) except+ + + cpp_MArray(const shared_ptr[cpp_RangeBase]&) except+ diff --git a/cnorxz/base/base.pyx b/cnorxz/base/base.pyx index 06719ef..ae58ddf 100644 --- a/cnorxz/base/base.pyx +++ b/cnorxz/base/base.pyx @@ -1,8 +1,18 @@ +#cython: language_level=3 +#cython: c_string_type=str +#cython: c_string_encoding=ascii +#cython: boundscheck=False +#cython: wrapparound=False +#cython: cdivision=True +#cython: embedsignature=True from libcpp.memory cimport shared_ptr, make_shared, dynamic_pointer_cast +from libcpp.string cimport string +from libcpp cimport bool from range cimport cpp_RangeBase from range_factory cimport cpp_RangeFactoryBase, cpp_CRangeFactory -from array cimport cpp_CArrayBase +from array cimport cpp_CArrayBase, cpp_MArray +from index cimport cpp_DIndex ## ============ @@ -18,6 +28,12 @@ cdef class Range: def dim(self): return self.cpp_range.get().dim() + def sub(self): + return getSubRange(self) + + def index(self): + return getRangeIndex(self) + cdef class RangeFactory: cdef shared_ptr[cpp_RangeFactoryBase] cpp_rfactory @@ -37,14 +53,102 @@ cdef class RangeFactory: return r +## =========== +## Index +## =========== + +cdef class Index: + + def lex(self): + return 0 + + def dim(self): + return 0 + +cdef class DIndex (Index): + cdef shared_ptr[cpp_DIndex] cpp_index + cdef bool itercall + + def __cinit__(self,_range,_lexpos=0): + cdef Range r = _range + cdef size_t l = _lexpos + self.cpp_index = make_shared[cpp_DIndex] (r.cpp_range,l) + self.itercall = False + + def __iter__(self): + self.cpp_index.get().setlpos(0) + self.itercall = True # otherwise first (i.e. zeroth) will be excluded + return self + + def __next__(self): + cdef DIndex ret = self + if self.itercall: + ret.itercall = False + return ret + if self.lex() < self.cpp_index.get().range().get().size()-1: + ret.cpp_index = make_shared[cpp_DIndex] (self.cpp_index.get().plus(1)) + return ret + else: + raise StopIteration + + def lex(self): + return self.cpp_index.get().lex() + + def dim(self): + return self.cpp_index.get().dim() + + def stringMeta(self): + return self.cpp_index.get().stringMeta() + +def getRangeIndex(_range): + return DIndex(_range) + ## =========== ## Array ## =========== cdef class Array: + + def size(self): + return 0 + + def range(self): + cdef Range r = Range() + return r + + +cdef class Array_Double (Array): cdef shared_ptr[cpp_CArrayBase[double]] cpp_array + def __cinit__(self,_range): + cdef Range r = _range + self.cpp_array = dynamic_pointer_cast[cpp_CArrayBase[double],cpp_MArray[double]] (make_shared[cpp_MArray[double]] (r.cpp_range) ) + def size(self): return self.cpp_array.get().size() + def range(self): + cdef Range r = Range() + r.cpp_range = self.cpp_array.get().range() + return r + +cdef class Array_Range (Array): + cdef shared_ptr[cpp_CArrayBase[shared_ptr[cpp_RangeBase]]] cpp_array + + def __cinit__(self,_range): + cdef Range r = _range + self.cpp_array = dynamic_pointer_cast[cpp_CArrayBase[shared_ptr[cpp_RangeBase]],cpp_MArray[shared_ptr[cpp_RangeBase]]] (make_shared[cpp_MArray[shared_ptr[cpp_RangeBase]]] (r.cpp_range) ) + + def size(self): + return self.cpp_array.get().size() + + def range(self): + cdef Range r = Range() + r.cpp_range = self.cpp_array.get().range() + return r +def getSubRange(_range): + cdef Range r = _range + cdef Array_Range a = Array_Range() + a.cpp_array = dynamic_pointer_cast[cpp_CArrayBase[shared_ptr[cpp_RangeBase]],cpp_MArray[shared_ptr[cpp_RangeBase]]] (make_shared[cpp_MArray[shared_ptr[cpp_RangeBase]]] (r.cpp_range.get().sub()) ) + return a diff --git a/cnorxz/base/index.pxd b/cnorxz/base/index.pxd new file mode 100644 index 0000000..7a93506 --- /dev/null +++ b/cnorxz/base/index.pxd @@ -0,0 +1,22 @@ + +from libcpp.memory cimport shared_ptr +from libcpp.string cimport string +from range cimport cpp_RangeBase + +cdef extern from "ranges/ranges.h" namespace "CNORXZ": + cdef cppclass cpp_DIndex "CNORXZ::DIndex": + cpp_DIndex() except+ + cpp_DIndex(const cpp_DIndex) except+ + cpp_DIndex(shared_ptr[cpp_RangeBase], size_t lexpos) except+ + cpp_DIndex setlpos "operator=" (size_t n) except+ + cpp_DIndex setincr "operator++"() except+ + cpp_DIndex setdecr "operator--"() except+ + cpp_DIndex plus "operator+"(int n) except+ + cpp_DIndex minus "operator-"(int n) except+ + + size_t lex() except+ + size_t dim() except+ + + shared_ptr[cpp_RangeBase] range() except+ + + string stringMeta() except+ diff --git a/cnorxz/base/range.pxd b/cnorxz/base/range.pxd index 8bef9dc..f0e0116 100644 --- a/cnorxz/base/range.pxd +++ b/cnorxz/base/range.pxd @@ -1,8 +1,11 @@ from libcpp.memory cimport shared_ptr +from array cimport cpp_MArray +from range cimport cpp_RangeBase cdef extern from "ranges/ranges.h" namespace "CNORXZ": cdef cppclass cpp_RangeBase "CNORXZ::RangeBase": size_t size() except + size_t dim() except + + cpp_MArray[shared_ptr[cpp_RangeBase]] sub() except+