more cereal wrapper + fixes
This commit is contained in:
parent
b3b6cf1584
commit
54fc5fba2b
2 changed files with 88 additions and 9 deletions
|
@ -14,7 +14,7 @@ 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, cpp_AIndex
|
from array cimport cpp_CArrayBase, cpp_MArray, cpp_AIndex
|
||||||
from index cimport cpp_DIndex
|
from index cimport cpp_DIndex
|
||||||
from cereal cimport cpp_writeJSONFile
|
from cereal cimport cpp_writeJSONFile, cpp_writeBINARYFile, cpp_readJSONFile, cpp_readBINARYFile
|
||||||
|
|
||||||
|
|
||||||
## ============
|
## ============
|
||||||
|
@ -133,8 +133,10 @@ cdef class Array:
|
||||||
cdef class Array_Double (Array):
|
cdef class Array_Double (Array):
|
||||||
cdef shared_ptr[cpp_CArrayBase[double]] cpp_array
|
cdef shared_ptr[cpp_CArrayBase[double]] cpp_array
|
||||||
|
|
||||||
def __cinit__(self,_range):
|
def __cinit__(self,_range=None):
|
||||||
cdef Range r = _range
|
cdef Range r
|
||||||
|
if not _range is None:
|
||||||
|
r = _range
|
||||||
self.cpp_array = dynamic_pointer_cast[cpp_CArrayBase[double],cpp_MArray[double]] (make_shared[cpp_MArray[double]] (r.cpp_range) )
|
self.cpp_array = dynamic_pointer_cast[cpp_CArrayBase[double],cpp_MArray[double]] (make_shared[cpp_MArray[double]] (r.cpp_range) )
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
|
@ -151,8 +153,10 @@ cdef class Array_Double (Array):
|
||||||
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
|
||||||
|
|
||||||
def __cinit__(self,_range):
|
def __cinit__(self,_range=None):
|
||||||
cdef Range r = _range
|
cdef Range r
|
||||||
|
if not _range is None:
|
||||||
|
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) )
|
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):
|
def size(self):
|
||||||
|
@ -163,25 +167,48 @@ cdef class Array_Range (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_Range(self)
|
||||||
|
|
||||||
|
|
||||||
def getSubRange(_range):
|
def getSubRange(_range):
|
||||||
cdef Range r = _range
|
cdef Range r = _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
|
||||||
|
|
||||||
def writeJSONFile(_fname,_array):
|
def writeFile(_fname,_array,_format):
|
||||||
cdef Array_Double a = _array
|
cdef Array_Double a = _array
|
||||||
cdef shared_ptr[cpp_MArray[double]] ap = dynamic_pointer_cast[cpp_MArray[double],cpp_CArrayBase[double]](a.cpp_array)
|
cdef shared_ptr[cpp_MArray[double]] ap = dynamic_pointer_cast[cpp_MArray[double],cpp_CArrayBase[double]](a.cpp_array)
|
||||||
|
if _format.upper() == "JSON":
|
||||||
cpp_writeJSONFile[double](_fname, deref(ap.get()))
|
cpp_writeJSONFile[double](_fname, deref(ap.get()))
|
||||||
|
elif _format.upper() == "BINARY":
|
||||||
|
cpp_writeBINARYFile[double](_fname, deref(ap.get()))
|
||||||
|
else:
|
||||||
|
raise Exception("unknown array file format '{}'".format(_format))
|
||||||
|
|
||||||
|
def readFile(_fname,_format):
|
||||||
|
cdef Array_Double a = Array_Double()
|
||||||
|
cdef shared_ptr[cpp_MArray[double]] ap = make_shared[cpp_MArray[double]]()
|
||||||
|
a.cpp_array = dynamic_pointer_cast[cpp_CArrayBase[double],cpp_MArray[double]](ap)
|
||||||
|
if _format.upper() == "JSON":
|
||||||
|
cpp_readJSONFile[double](_fname, deref(ap.get()))
|
||||||
|
elif _format.upper() == "BINARY":
|
||||||
|
cpp_readBINARYFile[double](_fname, deref(ap.get()))
|
||||||
|
else:
|
||||||
|
raise Exception("unknown array file format '{}'".format(_format))
|
||||||
|
return a
|
||||||
|
|
||||||
|
|
||||||
cdef class AIndex_Double (Index):
|
cdef class AIndex_Double (Index):
|
||||||
|
cdef shared_ptr[cpp_CArrayBase[double]] cpp_array # keep the instance alive
|
||||||
cdef shared_ptr[cpp_AIndex[double]] cpp_index
|
cdef shared_ptr[cpp_AIndex[double]] cpp_index
|
||||||
cdef bool itercall
|
cdef bool itercall
|
||||||
|
|
||||||
def __cinit__(self,_array,_lexpos=0):
|
def __cinit__(self,_array,_lexpos=0):
|
||||||
cdef size_t l = _lexpos
|
cdef size_t l = _lexpos
|
||||||
cdef Array_Double a = _array
|
cdef Array_Double a = _array
|
||||||
|
self.cpp_array = a.cpp_array
|
||||||
self.cpp_index = make_shared[cpp_AIndex[double]] (a.cpp_array.get().begin().A_plus(l))
|
self.cpp_index = make_shared[cpp_AIndex[double]] (a.cpp_array.get().begin().A_plus(l))
|
||||||
self.itercall = False
|
self.itercall = False
|
||||||
|
|
||||||
|
@ -212,3 +239,46 @@ cdef class AIndex_Double (Index):
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
return self.cpp_index.get().A_get()
|
return self.cpp_index.get().A_get()
|
||||||
|
|
||||||
|
|
||||||
|
cdef class AIndex_Range (Index):
|
||||||
|
cdef shared_ptr[cpp_CArrayBase[shared_ptr[cpp_RangeBase]]] cpp_array # keep the instance alive
|
||||||
|
cdef shared_ptr[cpp_AIndex[shared_ptr[cpp_RangeBase]]] cpp_index
|
||||||
|
cdef bool itercall
|
||||||
|
|
||||||
|
def __cinit__(self,_array,_lexpos=0):
|
||||||
|
cdef size_t l = _lexpos
|
||||||
|
cdef Array_Range a = _array
|
||||||
|
self.cpp_array = a.cpp_array
|
||||||
|
self.cpp_index = make_shared[cpp_AIndex[shared_ptr[cpp_RangeBase]]] (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_Range 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[shared_ptr[cpp_RangeBase]]] (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):
|
||||||
|
cdef Range r = Range()
|
||||||
|
r.cpp_range = self.cpp_index.get().A_get()
|
||||||
|
return r
|
||||||
|
|
|
@ -13,3 +13,12 @@ cdef extern from "cereal/cnorxz_cereal.h" namespace "CNORXZ::cer::Format":
|
||||||
|
|
||||||
cdef extern from "cereal/cnorxz_cereal.h" namespace "CNORXZ::cer":
|
cdef extern from "cereal/cnorxz_cereal.h" namespace "CNORXZ::cer":
|
||||||
cdef void cpp_writeJSONFile "CNORXZ::cer::writeJSONFile" [T] (const string&, const cpp_MArray[T]&)
|
cdef void cpp_writeJSONFile "CNORXZ::cer::writeJSONFile" [T] (const string&, const cpp_MArray[T]&)
|
||||||
|
|
||||||
|
cdef extern from "cereal/cnorxz_cereal.h" namespace "CNORXZ::cer":
|
||||||
|
cdef void cpp_writeBINARYFile "CNORXZ::cer::writeBINARYFile" [T] (const string&, const cpp_MArray[T]&)
|
||||||
|
|
||||||
|
cdef extern from "cereal/cnorxz_cereal.h" namespace "CNORXZ::cer":
|
||||||
|
cdef void cpp_readJSONFile "CNORXZ::cer::readJSONFile" [T] (const string&, cpp_MArray[T]&)
|
||||||
|
|
||||||
|
cdef extern from "cereal/cnorxz_cereal.h" namespace "CNORXZ::cer":
|
||||||
|
cdef void cpp_readBINARYFile "CNORXZ::cer::readBINARYFile" [T] (const string&, cpp_MArray[T]&)
|
||||||
|
|
Loading…
Reference in a new issue