From 75d3cab2b6bc00bbde0d9a33ebc5c89d77333f34 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 9 May 2023 19:31:05 +0200 Subject: [PATCH] further index/array wrappers... --- cnorxz/base/array.pxd | 46 ++++++++++++++++++++++++++++++++--- cnorxz/base/base.pyx | 56 +++++++++++++++++++++++++++++++++++++++++-- cnorxz/base/index.pxd | 26 ++++++++++++++++++-- 3 files changed, 121 insertions(+), 7 deletions(-) diff --git a/cnorxz/base/array.pxd b/cnorxz/base/array.pxd index 18563df..5ed0bd4 100644 --- a/cnorxz/base/array.pxd +++ b/cnorxz/base/array.pxd @@ -1,20 +1,60 @@ from libcpp.memory cimport shared_ptr from range cimport cpp_RangeBase +from index cimport cpp_YIndex + + +cdef extern from "array/array.h" namespace "CNORXZ": + cdef cppclass cpp_AIndex "CNORXZ::AIndex" [T] (cpp_YIndex): + cpp_AIndex() except+ + cpp_AIndex(const cpp_AIndex&) except+ + cpp_AIndex(const T*, const shared_ptr[cpp_RangeBase]&, size_t lexpos) except+ + cpp_AIndex(const T*, const cpp_YIndex&) except+ + + cpp_AIndex A_plus "operator+" (int n) except+ + cpp_AIndex A_minus "operator-" (int n) except+ + + const T& A_get "operator*" () except+ + + +cdef extern from "array/array.h" namespace "CNORXZ": + cdef cppclass cpp_BIndex "CNORXZ::BIndex" [T] (cpp_AIndex[T]): + cpp_BIndex() except+ + cpp_BIndex(const cpp_AIndex[T]&) except+ + cpp_BIndex(const T*, const shared_ptr[cpp_RangeBase]&, size_t lexpos) except+ + cpp_BIndex(const T*, const cpp_AIndex[T]&) except+ + + cpp_BIndex B_plus "operator+" (int n) except+ + cpp_BIndex B_minus "operator-" (int n) except+ + + T& B_get "operator*" () 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+ + cpp_AIndex[T] begin() except+ + cpp_AIndex[T] end() except+ + + +cdef extern from "array/array.h" namespace "CNORXZ": + cdef cppclass cpp_ArrayBase "CNORXZ::ArrayBase" [T] (cpp_CArrayBase[T]): + cpp_ArrayBase() except+ + cpp_ArrayBase(const cpp_ArrayBase[T]&) except+ + + cpp_BIndex[T] begin() except+ + cpp_BIndex[T] end() except+ + cdef extern from "array/array.h" namespace "CNORXZ": - cdef cppclass cpp_MArray "CNORXZ::MArray" [T] (cpp_CArrayBase[T]): - + cdef cppclass cpp_MArray "CNORXZ::MArray" [T] (cpp_ArrayBase[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 ae58ddf..cdff232 100644 --- a/cnorxz/base/base.pyx +++ b/cnorxz/base/base.pyx @@ -11,7 +11,7 @@ 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, cpp_MArray +from array cimport cpp_CArrayBase, cpp_MArray, cpp_AIndex from index cimport cpp_DIndex @@ -59,12 +59,22 @@ cdef class RangeFactory: cdef class Index: + def __iter__(self): + return self + + def __next__(self): + return self + def lex(self): return 0 def dim(self): return 0 + def stringMeta(self): + return '' + + cdef class DIndex (Index): cdef shared_ptr[cpp_DIndex] cpp_index cdef bool itercall @@ -102,7 +112,8 @@ cdef class DIndex (Index): def getRangeIndex(_range): return DIndex(_range) - + + ## =========== ## Array ## =========== @@ -132,6 +143,9 @@ cdef class Array_Double (Array): r.cpp_range = self.cpp_array.get().range() return r + def index(self): + return AIndex_Double(self) + cdef class Array_Range (Array): cdef shared_ptr[cpp_CArrayBase[shared_ptr[cpp_RangeBase]]] cpp_array @@ -152,3 +166,41 @@ def getSubRange(_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 + +cdef class AIndex_Double (Index): + cdef shared_ptr[cpp_AIndex[double]] cpp_index + cdef bool itercall + + def __cinit__(self,_array,_lexpos=0): + cdef size_t l = _lexpos + cdef Array_Double a = _array + self.cpp_index = make_shared[cpp_AIndex[double]] (a.cpp_array.get().begin().A_plus(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 AIndex_Double 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_AIndex[double]] (self.cpp_index.get().A_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 get(self): + return self.cpp_index.get().A_get() diff --git a/cnorxz/base/index.pxd b/cnorxz/base/index.pxd index 7a93506..d4b0a4a 100644 --- a/cnorxz/base/index.pxd +++ b/cnorxz/base/index.pxd @@ -3,11 +3,12 @@ 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(const cpp_DIndex&) except+ + cpp_DIndex(const 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+ @@ -20,3 +21,24 @@ cdef extern from "ranges/ranges.h" namespace "CNORXZ": shared_ptr[cpp_RangeBase] range() except+ string stringMeta() except+ + + +cdef extern from "ranges/ranges.h" namespace "CNORXZ": + cdef cppclass cpp_YIndex "CNORXZ::YIndex": + cpp_YIndex() except+ + cpp_YIndex(const cpp_YIndex&) except+ + cpp_YIndex(const shared_ptr[cpp_RangeBase]&, size_t lexpos) except+ + + cpp_YIndex setlpos "operator=" (size_t n) except+ + cpp_YIndex setincr "operator++"() except+ + cpp_YIndex setdecr "operator--"() except+ + cpp_YIndex plus "operator+"(int n) except+ + cpp_YIndex minus "operator-"(int n) except+ + + size_t lex() except+ + size_t dim() except+ + + shared_ptr[cpp_RangeBase] range() except+ + + string stringMeta() except+ +