From bd7607094031631481885764fc9309f601e54aa8 Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Sun, 25 Feb 2024 01:38:01 +0100 Subject: [PATCH] add more functions --- cnorxz/core/include/array_wrapper.h | 15 ++++++-- cnorxz/core/include/range_wrapper.h | 3 ++ cnorxz/core/lib/range_wrapper.cpp | 60 ++++++++++++++++++++++++++--- setup.py | 7 ++-- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/cnorxz/core/include/array_wrapper.h b/cnorxz/core/include/array_wrapper.h index 58052df..4eecc79 100644 --- a/cnorxz/core/include/array_wrapper.h +++ b/cnorxz/core/include/array_wrapper.h @@ -24,10 +24,16 @@ namespace CNORXZ virtual void readFile(cer::Format f, const String& fname) const = 0; #endif virtual int typenum() const = 0; - virtual void* data() const = 0; + virtual const void* data() const = 0; virtual SizeT datasize() const = 0; }; + class ArrayWrapperBase : public CArrayWrapperBase + { + public: + virtual void* data() = 0; + }; + template struct Typenum { @@ -59,7 +65,7 @@ namespace CNORXZ }; template - class ArrayWrapper : public CArrayWrapperBase + class ArrayWrapper : public ArrayWrapperBase { private: Sptr> mArr; @@ -67,6 +73,7 @@ namespace CNORXZ public: ArrayWrapper() : mArr( std::make_shared>() ) {} ArrayWrapper(const RangePtr& r) : mArr( std::make_shared>(r) ) {} + ArrayWrapper(const Sptr>& arr) : mArr(arr) {} virtual RangePtr range() const override final { return mArr->range(); } virtual SizeT size() const override final { return mArr->size(); } @@ -92,7 +99,9 @@ namespace CNORXZ } #endif virtual int typenum() const override final { return Typenum::value; } - virtual void* data() const override final + virtual const void* data() const override final + { return reinterpret_cast( mArr->data() ); } + virtual void* data() override final { return reinterpret_cast( mArr->data() ); } virtual SizeT datasize() const override final { return size() * sizeof(T); } }; diff --git a/cnorxz/core/include/range_wrapper.h b/cnorxz/core/include/range_wrapper.h index 2714da3..fabcd74 100644 --- a/cnorxz/core/include/range_wrapper.h +++ b/cnorxz/core/include/range_wrapper.h @@ -16,6 +16,8 @@ namespace CNORXZ inline SizeT size() const { return mR->size(); } inline SizeT dim() const { return mR->dim(); } inline String stringMeta(SizeT pos) const { return mR->stringMeta(pos); } + inline MArray sub() const { return mR->sub(); } + inline RangePtr sub(SizeT pos) const { return mR->sub(pos); } private: RangePtr mR; @@ -33,6 +35,7 @@ void PyCxRange_dealloc(PyCxRange* self); PyObject* PyCxRange_size(PyCxRange* self); PyObject* PyCxRange_dim(PyCxRange* self); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds); +PyObject* PyCxRange_sub(PyCxRange* self, PyObject* args, PyObject* kwds); PyTypeObject* PyCxRangeType_init(); extern PyMethodDef PyCxRange_methods[]; diff --git a/cnorxz/core/lib/range_wrapper.cpp b/cnorxz/core/lib/range_wrapper.cpp index f8b290e..405bd6e 100644 --- a/cnorxz/core/lib/range_wrapper.cpp +++ b/cnorxz/core/lib/range_wrapper.cpp @@ -1,4 +1,5 @@ +#include "array_wrapper.h" #include "range_wrapper.h" using namespace CNORXZ; @@ -6,6 +7,7 @@ using namespace CNORXZ; int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds) { //!!! + return 0; } void PyCxRange_dealloc(PyCxRange* self) @@ -14,6 +16,9 @@ void PyCxRange_dealloc(PyCxRange* self) Py_TYPE(self)->tp_free(self); } +PyDoc_STRVAR(cxz_PyCxRange_size_doc, + "size(self)\n--\n\n" + "return range size"); PyObject* PyCxRange_size(PyCxRange* self) { if(not self->ptrObj->_init()){ @@ -24,6 +29,9 @@ PyObject* PyCxRange_size(PyCxRange* self) return Py_BuildValue("k", retval); } +PyDoc_STRVAR(cxz_PyCxRange_dim_doc, + "dim(self)\n--\n\n" + "return range dimension"); PyObject* PyCxRange_dim(PyCxRange* self) { if(not self->ptrObj->_init()){ @@ -34,16 +42,58 @@ PyObject* PyCxRange_dim(PyCxRange* self) return Py_BuildValue("k", retval); } +PyDoc_STRVAR(cxz_PyCxRange_stringMeta_doc, + "stringMeta(self, pos)\n--\n\n" + "return string meta data for given range position"); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds) { - //!!! + static char* kwlist[] = { "pos", NULL }; + SizeT pos = 0; + if(not PyArg_ParseTupleAndKeywords(args, kwds, "k", kwlist, &pos)){ + return NULL; + } + const String s = self->ptrObj->stringMeta(pos); + return Py_BuildValue("s", s.c_str()); } +PyDoc_STRVAR(cxz_PyCxRange_sub_doc, + "sub(self, pos)\n--\n\n" + "get sub-ranges for given dimension; if pos >= dim, all sub-ranges are returned"); +PyObject* PyCxRange_sub(PyCxRange* self, PyObject* args, PyObject* kwds) +{ + static char* kwlist[] = { "pos", NULL }; + const SizeT max = self->ptrObj->dim(); + SizeT pos = max; + if(not PyArg_ParseTupleAndKeywords(args, kwds, "|k", kwlist, &pos)){ + return NULL; + } + if(pos < max) { + const RangePtr sub = self->ptrObj->sub(pos); + if(sub){ + PyCxRange* o; + o = PyObject_New(PyCxRange, &PyCxRangeType); + o->ptrObj = new RangeWrapper( sub ); + return Py_BuildValue("O", o); + } + else { + Py_RETURN_NONE; + } + } + else { + PyCxArrayB* o; + o = PyObject_New(PyCxArrayB, &PyCxArrayBType); + o->ptrObj = new ArrayWrapper( std::make_shared>(self->ptrObj->sub()) ); + return Py_BuildValue("O", o); + } +} + + PyMethodDef PyCxRange_methods[] = { - { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, "return range size" }, - { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, "return range dimension" }, - { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS, - "return string meta data for given range position" }, + { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, cxz_PyCxRange_size_doc }, + { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, cxz_PyCxRange_dim_doc }, + { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS| METH_KEYWORDS, + cxz_PyCxRange_stringMeta_doc }, + { "sub", (PyCFunction) PyCxRange_sub, METH_VARARGS| METH_KEYWORDS, cxz_PyCxRange_sub_doc }, { NULL } }; diff --git a/setup.py b/setup.py index 7af21d2..fbdeb42 100644 --- a/setup.py +++ b/setup.py @@ -21,10 +21,11 @@ have_numpy = True # flags: +config_bin = path_to_cnorxz+"/bin/cnorxz-config" extra_compile_args = sysconfig.get_config_var('CFLAGS').split() -cnorxz_version = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--version'],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] -cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() -cnorxz_definitions = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--definitions'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +cnorxz_version = subprocess.run([config_bin,'--version'],stdout=subprocess.PIPE).stdout.decode('ascii')[:-1] +cnorxz_flags = subprocess.run([config_bin,'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() +cnorxz_definitions = subprocess.run([config_bin,'--definitions'],stdout=subprocess.PIPE).stdout.decode('ascii').split() cnorxz_flags.remove("-Werror") cnorxz_flags.append("-Wno-write-strings") extra_compile_args += cnorxz_flags