📄 statc.cpp
字号:
/*
This file is part of Orange.
Orange is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Orange is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Orange; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Authors: Janez Demsar, Blaz Zupan, 1996--2002
Contact: janez.demsar@fri.uni-lj.si
*/
#ifdef _MSC_VER
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#endif
#include "statc.hpp"
#include "pywrapper.hpp"
#include "stat.hpp"
#include "stladdon.hpp"
//#include <algorithm>
#include <string>
using namespace std;
/* *********** EXCEPTION CATCHING ETC. ************/
#undef min
#undef max
#define PyTRY try {
#define PYNULL ((PyObject *)NULL)
#define PyCATCH PyCATCH_r(PYNULL)
#define PyCATCH_1 PyCATCH_r(-1)
#define PyCATCH_r(r) \
} \
catch (pyexception err) { err.restore(); return r; } \
catch (exception err) { PYERROR(PyExc_StatcKernel, err.what(), r); }
PyObject *PyExc_StatcKernel;
PyObject *PyExc_StatcWarning;
/* *********** MODULE INITIALIZATION ************/
STATC_API void initstatc()
{ if ( ((PyExc_StatcKernel = makeExceptionClass("statc.KernelException", "an error occurred in statc's C++ code")) == NULL)
|| ((PyExc_StatcWarning = makeExceptionClass("statc.Warning", "statc warning", PyExc_Warning)) == NULL))
return;
PyObject *me = Py_InitModule("statc", statc_functions);
PyObject *pdm = PyModule_New("pointDistribution");
PyModule_AddObject(pdm, "Minimal", PyInt_FromLong(DISTRIBUTE_MINIMAL));
PyModule_AddObject(pdm, "Factor", PyInt_FromLong(DISTRIBUTE_FACTOR));
PyModule_AddObject(pdm, "Fixed", PyInt_FromLong(DISTRIBUTE_FIXED));
PyModule_AddObject(pdm, "Uniform", PyInt_FromLong(DISTRIBUTE_UNIFORM));
PyModule_AddObject(me, "pointDistribution", pdm);
}
#ifdef _MSC_VER
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{ switch (ul_reason_for_call)
{ case DLL_PROCESS_ATTACH:case DLL_THREAD_ATTACH:case DLL_THREAD_DETACH:case DLL_PROCESS_DETACH:break; }
return TRUE;
}
#endif
/* *********** CONVERSION TO AND FROM PYTHON ************/
bool py2double(PyObject *pyo, double &dd)
{
PyObject *pyn=PyNumber_Float(pyo);
if (!pyn)
PYERROR(PyExc_TypeError, "invalid number", false);
dd=PyFloat_AsDouble(pyn);
Py_DECREF(pyn);
return true;
}
bool py2int(PyObject *pyo, int &dd)
{ PyObject *pyn=PyNumber_Int(pyo);
if (!pyn)
PYERROR(PyExc_TypeError, "invalid number", false);
dd=int(PyInt_AsLong(pyn));
Py_DECREF(pyn);
return true;
}
bool PyList2flist(PyObject *pylist, vector<double> &flist)
{ int len=PyList_Size(pylist);
flist=vector<double>(len);
for(int i=0; i<len; i++) {
PyObject *item=PyList_GetItem(pylist, i);
PyObject *number=PyNumber_Float(item);
if (!number)
PYERROR(PyExc_AttributeError, "invalid number in list", false);
flist[i]=PyFloat_AsDouble(number);
Py_DECREF(number);
}
return true;
}
bool PyList2flist2d(PyObject *pylist, vector<vector<double> > &flist)
{ int len=PyList_Size(pylist);
flist=vector<vector<double> >(len);
for(int i=0; i<len; i++) {
PyObject *slist=PyList_GetItem(pylist, i);
if (!PyList_Check(slist))
PYERROR(PyExc_TypeError, "list expected", false);
if (!PyList2flist(slist, flist[i]))
return false;
}
return true;
}
bool args2flist(PyObject *args, vector<double> &flist)
{ PyObject *pylist;
if ( !PyArg_ParseTuple(args, "O", &pylist)
|| !PyList_Check(pylist))
PYERROR(PyExc_AttributeError, "list expected", false)
return PyList2flist(pylist, flist);
}
bool args2flist2d(PyObject *args, vector<vector<double> > &flist)
{ PyObject *pylist;
if ( !PyArg_ParseTuple(args, "O", &pylist)
|| !PyList_Check(pylist))
PYERROR(PyExc_AttributeError, "list expected", false)
return PyList2flist2d(pylist, flist);
}
bool args22lists(PyObject *args, vector<double> &flist1, vector<double> &flist2)
{ PyObject *pylist1, *pylist2;
if ( !PyArg_ParseTuple(args, "OO", &pylist1, &pylist2)
|| !PyList_Check(pylist1)
|| !PyList_Check(pylist2)
|| (PyList_Size(pylist1)!=PyList_Size(pylist2)))
PYERROR(PyExc_AttributeError, "two lists of equal sizes expected", false)
return PyList2flist(pylist1, flist1) && PyList2flist(pylist2, flist2);
}
bool args22listsne(PyObject *args, vector<double> &flist1, vector<double> &flist2)
{ PyObject *pylist1, *pylist2;
if ( !PyArg_ParseTuple(args, "OO", &pylist1, &pylist2)
|| !PyList_Check(pylist1)
|| !PyList_Check(pylist2))
PYERROR(PyExc_AttributeError, "two lists expected", false)
return PyList2flist(pylist1, flist1) && PyList2flist(pylist2, flist2);
}
PyObject *flist2PyList(const vector<double> &flist)
{ PyObject *pylist=PyList_New(flist.size());
int i=0;
const_ITERATE(vector<double>, fi, flist)
PyList_SetItem(pylist, i++, PyFloat_FromDouble(*fi));
return pylist;
}
PyObject *ilist2PyList(const vector<int> &ilist)
{ PyObject *pylist=PyList_New(ilist.size());
int i=0;
const_ITERATE(vector<int>, fi, ilist)
PyList_SetItem(pylist, i++, PyInt_FromLong(*fi));
return pylist;
}
bool PyList2wlist(PyObject *pylist, vector<PyWrapper> &wlist)
{ int len=PyList_Size(pylist);
wlist=vector<PyWrapper>();
wlist.reserve(len);
for(int i=0; i<len; i++)
wlist.push_back(PyWrapper(PyList_GetItem(pylist, i)));
return true;
}
bool PyList2wlist2d(PyObject *pylist, vector<vector<PyWrapper> > &flist)
{ int len=PyList_Size(pylist);
flist=vector<vector<PyWrapper> >(len);
for(int i=0; i<len; i++) {
PyObject *slist=PyList_GetItem(pylist, i);
if (!PyList_Check(slist))
PYERROR(PyExc_TypeError, "list expected", false);
if (!PyList2wlist(slist, flist[i]))
return false;
}
return true;
}
bool args2wlist2d(PyObject *args, vector<vector<PyWrapper> > &wlist)
{ PyObject *pylist;
if ( !PyArg_ParseTuple(args, "O", &pylist)
|| !PyList_Check(pylist))
PYERROR(PyExc_AttributeError, "list expected", false)
return PyList2wlist2d(pylist, wlist);
}
bool args2wlist(PyObject *args, vector<PyWrapper> &wlist)
{ PyObject *pylist;
if ( !PyArg_ParseTuple(args, "O", &pylist)
|| !PyList_Check(pylist))
PYERROR(PyExc_AttributeError, "list expected", false)
return PyList2wlist(pylist, wlist);
}
bool args22wlists(PyObject *args, vector<PyWrapper> &flist1, vector<PyWrapper> &flist2)
{ PyObject *pylist1, *pylist2;
if ( !PyArg_ParseTuple(args, "OO", &pylist1, &pylist2)
|| !PyList_Check(pylist1)
|| !PyList_Check(pylist2)
|| (PyList_Size(pylist1)!=PyList_Size(pylist2)))
PYERROR(PyExc_AttributeError, "two lists of equal sizes expected", false)
return PyList2wlist(pylist1, flist1) && PyList2wlist(pylist2, flist2);
}
bool args22wlistsne(PyObject *args, vector<PyWrapper> &flist1, vector<PyWrapper> &flist2)
{ PyObject *pylist1, *pylist2;
if ( !PyArg_ParseTuple(args, "OO", &pylist1, &pylist2)
|| !PyList_Check(pylist1)
|| !PyList_Check(pylist2))
PYERROR(PyExc_AttributeError, "two lists expected", false)
return PyList2wlist(pylist1, flist1) && PyList2wlist(pylist2, flist2);
}
PyObject *wlist2PyList(const vector<PyWrapper> &flist)
{ PyObject *pylist=PyList_New(flist.size());
int i=0;
const_ITERATE(vector<PyWrapper>, fi, flist)
PyList_SetItem(pylist, i++, *fi);
return pylist;
}
/* *********** MACROS FOR GENERAL FUNCTION DEFINITIONS ************/
#define T_FROM_LIST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
vector<double> flist; \
if (args2flist(args, flist)) \
return PyFloat_FromDouble(name(flist)); \
\
PyErr_Clear(); \
\
vector<PyWrapper> wlist; \
if (args2wlist(args, wlist)) \
return name(wlist); \
\
return PYNULL; \
PyCATCH \
}
#define T_FROM_LIST_optT(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pylist; \
vector<double> flist; \
double init=0.0; \
if ( PyArg_ParseTuple(args, "O|d", &pylist, &init) \
&& PyList2flist(pylist, flist)) \
return PyFloat_FromDouble(name(flist, init)); \
\
PyErr_Clear(); \
\
vector<PyWrapper> wlist; \
PyObject *pyinit=NULL; \
if ( PyArg_ParseTuple(args, "O|O", &pylist, &pyinit) \
&& PyList2wlist(pylist, wlist)) \
return (PyObject *)(name(wlist, PyWrapper(pyinit))); \
\
return PYNULL; \
PyCATCH \
}
#define T_FROM_LIST_LIST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
vector<double> x, y; \
if (args22lists(args, x, y)) \
return PyFloat_FromDouble(name(x, y)); \
\
PyErr_Clear(); \
\
vector<PyWrapper> wx, wy; \
if (args22wlists(args, wx, wy)) \
return name(wx, wy); \
\
PYERROR(PyExc_AttributeError, #name": two lists expected", PYNULL); \
PyCATCH \
}
#define T_FROM_LIST_LIST_optT(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pyx, *pyy; \
vector<double> x, y; \
double init=0.0; \
if ( PyArg_ParseTuple(args, "OO|d", &pyx, &pyy, &init) \
&& PyList2flist(pyx, x) \
&& PyList2flist(pyy, y)) \
return PyFloat_FromDouble(name(x, y, init)); \
\
PyErr_Clear(); \
\
vector<PyWrapper> wx, wy; \
PyObject *pyinit=NULL; \
if ( PyArg_ParseTuple(args, "OO|d", &pyx, &pyy, &pyinit) \
&& PyList2wlist(pyx, wx) \
&& PyList2wlist(pyy, wy)) \
return (PyObject *)(name(wx, wy, PyWrapper(pyinit))); \
\
return PYNULL; \
PyCATCH \
}
#define LIST_FROM_LIST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
vector<double> x, res; \
if (args2flist(args, x)) \
name(x, res); \
return flist2PyList(res); \
\
PyErr_Clear(); \
\
vector<PyWrapper> w, wres; \
if (args2wlist(args, w)) \
name(w, wres); \
return wlist2PyList(wres); \
\
PYERROR(PyExc_AttributeError, #name": list expected", PYNULL); \
PyCATCH \
}
#define LIST_FROM_LIST_optT(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pylist; \
vector<double> flist, res; \
double init=0.0; \
if ( PyArg_ParseTuple(args, "O|d", &pylist, &init) \
&& PyList2flist(pylist, flist)) {\
name(flist, res); \
return flist2PyList(res); \
} \
\
PyErr_Clear(); \
\
vector<PyWrapper> wlist, wres; \
PyObject *pyinit=NULL; \
if ( PyArg_ParseTuple(args, "O|O", &pylist, &pyinit) \
&& PyList2wlist(pylist, wlist)) { \
name(wlist, wres); \
return wlist2PyList(wlist); \
} \
\
return PYNULL; \
PyCATCH \
}
#define T_FROM_LIST_T(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pylist; \
double mom; \
vector<double> flist; \
if ( PyArg_ParseTuple(args, "Od", &pylist, &mom) \
&& PyList2flist(pylist, flist)) \
return PyFloat_FromDouble(name(flist, mom)); \
\
PyErr_Clear(); \
\
vector<PyWrapper> wlist; \
PyObject *wmom; \
if ( PyArg_ParseTuple(args, "OO", &pylist, &wmom) \
&& PyList2wlist(pylist, wlist)) \
return name(wlist, PyWrapper(wmom)); \
\
PYERROR(PyExc_AttributeError, #name": invalid arguments", PYNULL); \
PyCATCH \
}
#define T_FROM_LIST_plus(name, type, pys) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pylist; \
type mom; \
if ( PyArg_ParseTuple(args, pys, &pylist, &mom)) {\
vector<double> flist; \
if (PyList2flist(pylist, flist)) \
return PyFloat_FromDouble(name(flist, mom)); \
\
PyErr_Clear(); \
\
vector<PyWrapper> wlist; \
if (PyList2wlist(pylist, wlist)) \
return name(wlist, mom); \
} \
PYERROR(PyExc_AttributeError, #name": invalid arguments", PYNULL); \
PyCATCH \
}
#define T_FROM_LIST_INT(name) T_FROM_LIST_plus(name, int, "Oi")
#define T_FROM_LIST_DOUBLE(name) T_FROM_LIST_plus(name, double, "Od")
#define LIST_FROM_LIST_plus(name, type, pys) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pylist; \
type mom; \
if ( PyArg_ParseTuple(args, pys, &pylist, &mom)) {\
vector<double> flist; \
if (PyList2flist(pylist, flist)) { \
vector<double> fres; \
name(flist, mom, fres); \
return flist2PyList(fres); \
} \
\
PyErr_Clear(); \
\
vector<PyWrapper> wlist; \
if (PyList2wlist(pylist, wlist)) { \
vector<PyWrapper> wres; \
name(wlist, mom, wres); \
return wlist2PyList(wres); \
} \
} \
PYERROR(PyExc_AttributeError, #name": invalid arguments", PYNULL); \
PyCATCH \
}
#define LIST_FROM_LIST_INT(name) LIST_FROM_LIST_plus(name, int, "Oi")
#define LIST_FROM_LIST_DOUBLE(name) LIST_FROM_LIST_plus(name, double, "Od")
#define DOUBLE_DOUBLE_FROM_LIST_LIST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
vector<double> x, y; \
double res1, res2; \
if (args22lists(args, x, y)) {\
res1=name(x, y, res2); \
return Py_BuildValue("dd", res1, res2); \
} \
\
PyErr_Clear(); \
\
vector<PyWrapper> wx, wy; \
if (args22wlists(args, wx, wy)) {\
res1=name(wx, wy, res2); \
return Py_BuildValue("dd", res1, res2); \
} \
\
PYERROR(PyExc_AttributeError, #name": two lists of equal size expected", PYNULL); \
PyCATCH \
}
#define T_T_FROM_LIST_LIST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
vector<double> x, y; \
if (args22lists(args, x, y)) { \
double res1, res2; \
res1=name(x, y, res2); \
return Py_BuildValue("dd", res1, res2); \
} \
\
PyErr_Clear(); \
\
vector<PyWrapper> wx, wy; \
if (args22wlists(args, wx, wy)) { \
PyWrapper res1, res2; \
res1=name(wx, wy, res2); \
return Py_BuildValue("NN", (PyObject *)res1, (PyObject *)res2); \
} \
\
PYERROR(PyExc_AttributeError, #name": two lists of equal size expected", PYNULL); \
PyCATCH \
}
#define T_T_FROM_LIST(name) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
vector<double> flist; \
if (!args2flist(args, flist)) \
return NULL; \
\
double res1, res2; \
res1 = name(flist, res2); \
return Py_BuildValue("dd", res1, res2); \
PyCATCH \
}
#define T_T_FROM_LIST_plus(name, type, pys) \
PyObject *py_##name(PyObject *, PyObject *args) \
{ PyTRY \
PyObject *pylist; \
type mom; \
if ( PyArg_ParseTuple(args, pys, &pylist, &mom)) {\
vector<double> flist; \
if (PyList2flist(pylist, flist)) \
double res1, res2; \
res1=name(flist, mom, res2); \
return PyBuildValue("dd", res1, res2); \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -