From 6a6e1c823c373386eba421d7a47285f2fa9a662d Mon Sep 17 00:00:00 2001 From: Christian Zimmermann Date: Tue, 2 Apr 2024 20:23:08 +0200 Subject: [PATCH] wrap more functions in Range wrapper --- cnorxz/core/core.cpp | 7 ++++++- cnorxz/core/include/array_wrapper.h | 2 ++ cnorxz/core/include/range_wrapper.h | 2 ++ cnorxz/core/lib/array_wrapper.cpp | 13 ++++++++++--- cnorxz/core/lib/range_wrapper.cpp | 30 ++++++++++++++++++++++++++++- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/cnorxz/core/core.cpp b/cnorxz/core/core.cpp index 7977a15..708741c 100644 --- a/cnorxz/core/core.cpp +++ b/cnorxz/core/core.cpp @@ -5,12 +5,17 @@ using namespace CNORXZ; +static PyMethodDef cnorxz_core_methods[] = { + { "readFile", (PyCFunction) PyCxReadFile, METH_VARARGS| METH_KEYWORDS, "Read cnorxz-formatted file." }, + { NULL, NULL, 0, NULL } +}; + static PyModuleDef cnorxz_core_module = { PyModuleDef_HEAD_INIT, "cnorxz", "cnorxz core module", -1, - NULL, NULL, NULL, NULL, NULL + cnorxz_core_methods }; PyMODINIT_FUNC PyInit_cnorxz() diff --git a/cnorxz/core/include/array_wrapper.h b/cnorxz/core/include/array_wrapper.h index 58052df..d139c51 100644 --- a/cnorxz/core/include/array_wrapper.h +++ b/cnorxz/core/include/array_wrapper.h @@ -113,6 +113,8 @@ PyObject* PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds) PyObject* PyCxArrayB_readFile(PyCxArrayB* self, PyObject* args, PyObject* kwds); PyTypeObject* PyCxArrayBType_init(); +PyObject* PyCxReadFile(PyObject* self, PyObject* args, PyObject* kwds); + extern PyMethodDef PyCxArrayB_methods[]; extern PyTypeObject PyCxArrayBType; diff --git a/cnorxz/core/include/range_wrapper.h b/cnorxz/core/include/range_wrapper.h index 2714da3..f3a1439 100644 --- a/cnorxz/core/include/range_wrapper.h +++ b/cnorxz/core/include/range_wrapper.h @@ -15,6 +15,7 @@ namespace CNORXZ inline bool _init() const { return mR != nullptr; } inline SizeT size() const { return mR->size(); } inline SizeT dim() const { return mR->dim(); } + inline RangePtr sub(SizeT pos) { return mR->sub(pos); } inline String stringMeta(SizeT pos) const { return mR->stringMeta(pos); } private: @@ -32,6 +33,7 @@ int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds); void PyCxRange_dealloc(PyCxRange* self); PyObject* PyCxRange_size(PyCxRange* self); PyObject* PyCxRange_dim(PyCxRange* self); +PyObject* PyCxRange_sub(PyCxRange* self, PyObject* args, PyObject* kwds); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds); PyTypeObject* PyCxRangeType_init(); diff --git a/cnorxz/core/lib/array_wrapper.cpp b/cnorxz/core/lib/array_wrapper.cpp index 90c0868..2b35084 100644 --- a/cnorxz/core/lib/array_wrapper.cpp +++ b/cnorxz/core/lib/array_wrapper.cpp @@ -53,7 +53,6 @@ PyObject* PyCxArrayB_range(PyCxArrayB* self) PyCxRange* o; o = PyObject_New(PyCxRange, &PyCxRangeType); o->ptrObj = new RangeWrapper(r); - //return Py_BuildValue("k", retval); return Py_BuildValue("O", o); } @@ -88,7 +87,7 @@ PyObject* PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds) char* fname = NULL; char* format = NULL; - if(not PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist, &fname, &format)){ + if(not PyArg_ParseTupleAndKeywords(args, kwds, "ss|", kwlist, &fname, &format)){ return NULL; } const String fn(fname); @@ -103,7 +102,7 @@ PyObject* PyCxArrayB_readFile(PyCxArrayB* self, PyObject* args, PyObject* kwds) char* fname = NULL; char* format = NULL; - if(not PyArg_ParseTupleAndKeywords(args, kwds, "s|s", kwlist, &fname, &format)){ + if(not PyArg_ParseTupleAndKeywords(args, kwds, "ss|", kwlist, &fname, &format)){ return NULL; } const String fn(fname); @@ -176,3 +175,11 @@ PyTypeObject* PyCxArrayBType_init() return &PyCxArrayBType; } } + +PyObject* PyCxReadFile(PyObject* self, PyObject* args, PyObject* kwds) +{ + PyCxArrayB* out = (PyCxArrayB*) PyObject_NEW(PyCxArrayB, &PyCxArrayBType); + out->ptrObj = new ArrayWrapper(); + PyCxArrayB_readFile(out,args,kwds); + return Py_BuildValue("O", out);; +} diff --git a/cnorxz/core/lib/range_wrapper.cpp b/cnorxz/core/lib/range_wrapper.cpp index f8b290e..9a81cb8 100644 --- a/cnorxz/core/lib/range_wrapper.cpp +++ b/cnorxz/core/lib/range_wrapper.cpp @@ -5,6 +5,7 @@ using namespace CNORXZ; int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds) { + //!!! } @@ -34,14 +35,41 @@ PyObject* PyCxRange_dim(PyCxRange* self) return Py_BuildValue("k", retval); } +PyObject* PyCxRange_sub(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; + } + + SizeT dim = self->ptrObj->dim(); + if(pos >= dim){ + return PyErr_Format(PyExc_RuntimeError, "requested sub-range position (%d) exceeds range dimension (%d)", pos, dim); + } + + const RangePtr r = self->ptrObj->sub(pos); + PyCxRange* o = PyObject_New(PyCxRange, &PyCxRangeType); + o->ptrObj = new RangeWrapper(r); + return Py_BuildValue("O", o); +} + PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds) { - //!!! + const SizeT size = self->ptrObj->size(); + PyObject* out = PyList_New(size); + for(SizeT i = 0; i != size; ++i){ + PyObject* s = Py_BuildValue("s", self->ptrObj->stringMeta(i).c_str() ); + PyList_SetItem(out, i, s); + } + return out; } PyMethodDef PyCxRange_methods[] = { { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, "return range size" }, { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, "return range dimension" }, + { "sub", (PyCFunction) PyCxRange_sub, METH_VARARGS|METH_KEYWORDS, "return sub-range for given dimension" }, { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS, "return string meta data for given range position" }, { NULL }