further index/array wrappers...

This commit is contained in:
Christian Zimmermann 2023-05-09 19:31:05 +02:00
parent 50b09fa9c2
commit 75d3cab2b6
3 changed files with 121 additions and 7 deletions

View file

@ -1,20 +1,60 @@
from libcpp.memory cimport shared_ptr from libcpp.memory cimport shared_ptr
from range cimport cpp_RangeBase 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 extern from "array/array.h" namespace "CNORXZ":
cdef cppclass cpp_CArrayBase "CNORXZ::CArrayBase" [T]: cdef cppclass cpp_CArrayBase "CNORXZ::CArrayBase" [T]:
cpp_CArrayBase() except+ cpp_CArrayBase() except+
cpp_CArrayBase(const cpp_CArrayBase[T]&) except+ cpp_CArrayBase(const cpp_CArrayBase[T]&) except+
size_t size() except+ size_t size() except+
shared_ptr[cpp_RangeBase] range() 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 extern from "array/array.h" namespace "CNORXZ":
cdef cppclass cpp_MArray "CNORXZ::MArray" [T] (cpp_CArrayBase[T]): 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_ArrayBase[T]):
cpp_MArray() except+ cpp_MArray() except+
cpp_MArray(const cpp_MArray[T]&) except+ cpp_MArray(const cpp_MArray[T]&) except+
cpp_MArray(const shared_ptr[cpp_RangeBase]&) except+ cpp_MArray(const shared_ptr[cpp_RangeBase]&) except+

View file

@ -11,7 +11,7 @@ from libcpp.string cimport string
from libcpp cimport bool from libcpp cimport bool
from range cimport cpp_RangeBase from range cimport cpp_RangeBase
from range_factory cimport cpp_RangeFactoryBase, cpp_CRangeFactory 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 from index cimport cpp_DIndex
@ -59,12 +59,22 @@ cdef class RangeFactory:
cdef class Index: cdef class Index:
def __iter__(self):
return self
def __next__(self):
return self
def lex(self): def lex(self):
return 0 return 0
def dim(self): def dim(self):
return 0 return 0
def stringMeta(self):
return ''
cdef class DIndex (Index): cdef class DIndex (Index):
cdef shared_ptr[cpp_DIndex] cpp_index cdef shared_ptr[cpp_DIndex] cpp_index
cdef bool itercall cdef bool itercall
@ -103,6 +113,7 @@ cdef class DIndex (Index):
def getRangeIndex(_range): def getRangeIndex(_range):
return DIndex(_range) return DIndex(_range)
## =========== ## ===========
## Array ## Array
## =========== ## ===========
@ -132,6 +143,9 @@ cdef class Array_Double (Array):
r.cpp_range = self.cpp_array.get().range() r.cpp_range = self.cpp_array.get().range()
return r return r
def index(self):
return AIndex_Double(self)
cdef class Array_Range (Array): cdef class Array_Range (Array):
cdef shared_ptr[cpp_CArrayBase[shared_ptr[cpp_RangeBase]]] cpp_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() 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()) ) 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 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()

View file

@ -3,11 +3,12 @@ from libcpp.memory cimport shared_ptr
from libcpp.string cimport string from libcpp.string cimport string
from range cimport cpp_RangeBase from range cimport cpp_RangeBase
cdef extern from "ranges/ranges.h" namespace "CNORXZ": cdef extern from "ranges/ranges.h" namespace "CNORXZ":
cdef cppclass cpp_DIndex "CNORXZ::DIndex": cdef cppclass cpp_DIndex "CNORXZ::DIndex":
cpp_DIndex() except+ cpp_DIndex() except+
cpp_DIndex(const cpp_DIndex) except+ cpp_DIndex(const cpp_DIndex&) except+
cpp_DIndex(shared_ptr[cpp_RangeBase], size_t lexpos) except+ cpp_DIndex(const shared_ptr[cpp_RangeBase]&, size_t lexpos) except+
cpp_DIndex setlpos "operator=" (size_t n) except+ cpp_DIndex setlpos "operator=" (size_t n) except+
cpp_DIndex setincr "operator++"() except+ cpp_DIndex setincr "operator++"() except+
cpp_DIndex setdecr "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+ shared_ptr[cpp_RangeBase] range() except+
string stringMeta() 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+