add more functions

This commit is contained in:
Christian Zimmermann 2024-02-25 01:38:01 +01:00
parent b04a5ddc9d
commit bd76070940
4 changed files with 74 additions and 11 deletions

View file

@ -24,10 +24,16 @@ namespace CNORXZ
virtual void readFile(cer::Format f, const String& fname) const = 0; virtual void readFile(cer::Format f, const String& fname) const = 0;
#endif #endif
virtual int typenum() const = 0; virtual int typenum() const = 0;
virtual void* data() const = 0; virtual const void* data() const = 0;
virtual SizeT datasize() const = 0; virtual SizeT datasize() const = 0;
}; };
class ArrayWrapperBase : public CArrayWrapperBase
{
public:
virtual void* data() = 0;
};
template <typename T> template <typename T>
struct Typenum struct Typenum
{ {
@ -59,7 +65,7 @@ namespace CNORXZ
}; };
template <typename T> template <typename T>
class ArrayWrapper : public CArrayWrapperBase class ArrayWrapper : public ArrayWrapperBase
{ {
private: private:
Sptr<ArrayBase<T>> mArr; Sptr<ArrayBase<T>> mArr;
@ -67,6 +73,7 @@ namespace CNORXZ
public: public:
ArrayWrapper() : mArr( std::make_shared<MArray<T>>() ) {} ArrayWrapper() : mArr( std::make_shared<MArray<T>>() ) {}
ArrayWrapper(const RangePtr& r) : mArr( std::make_shared<MArray<T>>(r) ) {} ArrayWrapper(const RangePtr& r) : mArr( std::make_shared<MArray<T>>(r) ) {}
ArrayWrapper(const Sptr<ArrayBase<T>>& arr) : mArr(arr) {}
virtual RangePtr range() const override final { return mArr->range(); } virtual RangePtr range() const override final { return mArr->range(); }
virtual SizeT size() const override final { return mArr->size(); } virtual SizeT size() const override final { return mArr->size(); }
@ -92,7 +99,9 @@ namespace CNORXZ
} }
#endif #endif
virtual int typenum() const override final { return Typenum<T>::value; } virtual int typenum() const override final { return Typenum<T>::value; }
virtual void* data() const override final virtual const void* data() const override final
{ return reinterpret_cast<const void*>( mArr->data() ); }
virtual void* data() override final
{ return reinterpret_cast<void*>( mArr->data() ); } { return reinterpret_cast<void*>( mArr->data() ); }
virtual SizeT datasize() const override final { return size() * sizeof(T); } virtual SizeT datasize() const override final { return size() * sizeof(T); }
}; };

View file

@ -16,6 +16,8 @@ namespace CNORXZ
inline SizeT size() const { return mR->size(); } inline SizeT size() const { return mR->size(); }
inline SizeT dim() const { return mR->dim(); } inline SizeT dim() const { return mR->dim(); }
inline String stringMeta(SizeT pos) const { return mR->stringMeta(pos); } inline String stringMeta(SizeT pos) const { return mR->stringMeta(pos); }
inline MArray<RangePtr> sub() const { return mR->sub(); }
inline RangePtr sub(SizeT pos) const { return mR->sub(pos); }
private: private:
RangePtr mR; RangePtr mR;
@ -33,6 +35,7 @@ void PyCxRange_dealloc(PyCxRange* self);
PyObject* PyCxRange_size(PyCxRange* self); PyObject* PyCxRange_size(PyCxRange* self);
PyObject* PyCxRange_dim(PyCxRange* self); PyObject* PyCxRange_dim(PyCxRange* self);
PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds);
PyObject* PyCxRange_sub(PyCxRange* self, PyObject* args, PyObject* kwds);
PyTypeObject* PyCxRangeType_init(); PyTypeObject* PyCxRangeType_init();
extern PyMethodDef PyCxRange_methods[]; extern PyMethodDef PyCxRange_methods[];

View file

@ -1,4 +1,5 @@
#include "array_wrapper.h"
#include "range_wrapper.h" #include "range_wrapper.h"
using namespace CNORXZ; using namespace CNORXZ;
@ -6,6 +7,7 @@ using namespace CNORXZ;
int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds) int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds)
{ {
//!!! //!!!
return 0;
} }
void PyCxRange_dealloc(PyCxRange* self) void PyCxRange_dealloc(PyCxRange* self)
@ -14,6 +16,9 @@ void PyCxRange_dealloc(PyCxRange* self)
Py_TYPE(self)->tp_free(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) PyObject* PyCxRange_size(PyCxRange* self)
{ {
if(not self->ptrObj->_init()){ if(not self->ptrObj->_init()){
@ -24,6 +29,9 @@ PyObject* PyCxRange_size(PyCxRange* self)
return Py_BuildValue("k", retval); return Py_BuildValue("k", retval);
} }
PyDoc_STRVAR(cxz_PyCxRange_dim_doc,
"dim(self)\n--\n\n"
"return range dimension");
PyObject* PyCxRange_dim(PyCxRange* self) PyObject* PyCxRange_dim(PyCxRange* self)
{ {
if(not self->ptrObj->_init()){ if(not self->ptrObj->_init()){
@ -34,16 +42,58 @@ PyObject* PyCxRange_dim(PyCxRange* self)
return Py_BuildValue("k", retval); 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) 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<RangePtr>( std::make_shared<MArray<RangePtr>>(self->ptrObj->sub()) );
return Py_BuildValue("O", o);
}
}
PyMethodDef PyCxRange_methods[] = { PyMethodDef PyCxRange_methods[] = {
{ "size", (PyCFunction) PyCxRange_size, METH_VARARGS, "return range size" }, { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, cxz_PyCxRange_size_doc },
{ "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, "return range dimension" }, { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, cxz_PyCxRange_dim_doc },
{ "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS, { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS| METH_KEYWORDS,
"return string meta data for given range position" }, cxz_PyCxRange_stringMeta_doc },
{ "sub", (PyCFunction) PyCxRange_sub, METH_VARARGS| METH_KEYWORDS, cxz_PyCxRange_sub_doc },
{ NULL } { NULL }
}; };

View file

@ -21,10 +21,11 @@ have_numpy = True
# flags: # flags:
config_bin = path_to_cnorxz+"/bin/cnorxz-config"
extra_compile_args = sysconfig.get_config_var('CFLAGS').split() 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_version = subprocess.run([config_bin,'--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_flags = subprocess.run([config_bin,'--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_definitions = subprocess.run([config_bin,'--definitions'],stdout=subprocess.PIPE).stdout.decode('ascii').split()
cnorxz_flags.remove("-Werror") cnorxz_flags.remove("-Werror")
cnorxz_flags.append("-Wno-write-strings") cnorxz_flags.append("-Wno-write-strings")
extra_compile_args += cnorxz_flags extra_compile_args += cnorxz_flags