📄 vectortemplates.hpp
字号:
k = PyObject_RichCompareBool(myItem, hisItem, Py_NE);
if (k<=0) {
Py_DECREF(myItem);
Py_DECREF(hisItem);
myItem = NULL;
hisItem = NULL;
}
}
if (k == -1)
return PYNULL;
if (!k) {
bool cmp;
switch (op) {
case Py_LT: cmp = myLen < hisLen; break;
case Py_LE: cmp = myLen <= hisLen; break;
case Py_EQ: cmp = myLen == hisLen; break;
case Py_NE: cmp = myLen != hisLen; break;
case Py_GT: cmp = myLen > hisLen; break;
case Py_GE: cmp = myLen >= hisLen; break;
default: return PYNULL; /* cannot happen */
}
PyObject *res = cmp ? Py_True : Py_False;
Py_INCREF(res);
return res;
}
// Here, myItem and hisItem are not decrefed yet!
PyObject *res = PYNULL;
if (op == Py_EQ)
res = Py_False;
else if (op == Py_NE)
res = Py_True;
else
res = PyObject_RichCompare(myItem, hisItem, op);
Py_DECREF(myItem);
Py_DECREF(hisItem);
return res;
}
catch (exception err) {
Py_XDECREF(myItem);
Py_XDECREF(hisItem);
throw;
}
PyCATCH
}
static PyObject *_str(TPyOrange *self)
{
PyObject *result = callbackOutput((PyObject *)self, NULL, NULL, "str", "repr");
if (result)
return result;
CAST_TO(_ListType, aList);
string res("<");
for(iterator bi(aList->begin()), ei(bi), ee(aList->end()); ei!=ee; ei++) {
if (ei!=bi)
res += ", ";
PyObject *obj = WrapOrange(*ei);
PyObject *repred = PyObject_Str(obj);
res += PyString_AsString(repred);
Py_DECREF(obj);
Py_DECREF(repred);
}
res += ">";
return PyString_FromString(res.c_str());
}
/* -------------------------------------------------------------------------------------------------- */
static PyObject *_append(TPyOrange *self, PyObject *item)
{ PyTRY
_WrappedElement obj;
if (!_fromPython(item, obj))
return PYNULL;
CAST_TO(_ListType, aList);
aList->push_back(obj);
RETURN_NONE;
PyCATCH
}
static PyObject *_count(TPyOrange *self, PyObject *item)
{ PyTRY
_WrappedElement obj;
if (!_fromPython(item, obj))
return PYNULL;
CAST_TO(_ListType, aList);
int cnt=0;
for(iterator bi=aList->begin(), be=aList->end(); bi!=be; bi++)
if (obj==*bi)
cnt++;
return PyInt_FromLong(cnt);
PyCATCH
}
static int _contains(TPyOrange *self, PyObject *item)
{ PyTRY
_WrappedElement obj;
if (!_fromPython(item, obj))
return -1;
CAST_TO_err(_ListType, aList, -1);
for(iterator bi=aList->begin(), be=aList->end(); bi!=be; bi++)
if (obj==*bi)
return 1;
return 0;
PyCATCH_1
}
static PyObject *_filter(TPyOrange *self, PyObject *args)
{
PyTRY
PyObject *filtfunc=NULL;
if (!PyArg_ParseTuple(args, "|O:filter", &filtfunc))
return PYNULL;
PyObject *emtuple = PyTuple_New(0);
PyObject *emdict = PyDict_New();
PyObject *newList = self->ob_type->tp_new(self->ob_type, emtuple, emdict);
Py_DECREF(emtuple);
Py_DECREF(emdict);
emtuple = NULL;
emdict = NULL;
if (!newList)
return NULL;
CAST_TO(_ListType, aList)
NAME_CAST_TO(_ListType, newList, cList)
for(iterator bi=aList->begin(), be=aList->end(); bi!=be; bi++) {
PyObject *lel=WrapOrange(*bi);
if (filtfunc) {
PyObject *filtres=PyObject_CallFunction(filtfunc, "O", lel);
Py_DECREF(lel);
if (!filtres)
throw pyexception();
lel=filtres;
}
if (PyObject_IsTrue(lel))
cList->push_back(*bi);
Py_DECREF(lel);
}
return newList;
PyCATCH;
}
static PyObject *_index(TPyOrange *self, PyObject *item)
{ PyTRY
_WrappedElement obj;
if (!_fromPython(item, obj))
return PYNULL;
CAST_TO(_ListType, aList);
for(iterator bi=aList->begin(), be=aList->end(); bi!=be; bi++)
if (obj==*bi)
return PyInt_FromLong(bi-aList->begin());
PYERROR(PyExc_ValueError, "list.index(x): x not in list", PYNULL)
PyCATCH
}
static PyObject *_insert(TPyOrange *self, PyObject *args)
{ PyTRY
CAST_TO(_ListType, aList);
PyObject *obj;
int index;
_WrappedElement item;
if ( !PyArg_ParseTuple(args, "iO", &index, &obj)
|| !checkIndex(index, aList->size())
|| !_fromPython(obj, item))
return PYNULL;
aList->insert(aList->begin()+index, item);
RETURN_NONE;
PyCATCH
}
static PyObject *_native(TPyOrange *self)
{ PyTRY
CAST_TO(_ListType, aList);
PyObject *newList = PyList_New(aList->size());
int i=0;
for(iterator li = aList->begin(), le = aList->end(); li!=le; li++)
PyList_SetItem(newList, i++, WrapOrange(*li));
return newList;
PyCATCH
}
static PyObject *_pop(TPyOrange *self, PyObject *args)
{ PyTRY
CAST_TO(_ListType, aList);
int idx=aList->size()-1;
if (!PyArg_ParseTuple(args, "|i:pop", &idx))
return PYNULL;
PyObject *ret=_getitem(self, idx);
if (!ret)
return PYNULL;
aList->erase(aList->begin()+idx);
return ret;
PyCATCH
}
static PyObject *_remove(TPyOrange *self, PyObject *item)
{ PyTRY
_WrappedElement obj;
if (!_fromPython(item, obj))
return PYNULL;
CAST_TO(_ListType, aList);
for(iterator bi=aList->begin(), be=aList->end(); bi!=be; bi++)
if (obj==*bi) {
aList->erase(bi);
RETURN_NONE;
}
PYERROR(PyExc_ValueError, "remove(x): x not in list", PYNULL)
PyCATCH
}
class TCmpByCallback
{ public:
PyObject *cmpfunc;
TCmpByCallback(PyObject *func)
{ if (!PyCallable_Check(func))
raiseErrorWho("CmpByCallback", "compare object not callable");
cmpfunc=func;
Py_INCREF(cmpfunc);
}
TCmpByCallback(const TCmpByCallback &other)
: cmpfunc(other.cmpfunc)
{ Py_INCREF(cmpfunc); }
~TCmpByCallback()
{ Py_DECREF(cmpfunc);
}
bool operator()(const _WrappedElement &x, const _WrappedElement &y) const
{ PyObject *pyx=WrapOrange(const_cast<_WrappedElement &>(x)), *pyy=WrapOrange(const_cast<_WrappedElement &>(y));
PyObject *cmpres=PyObject_CallFunction(cmpfunc, "OO", pyx, pyy);
Py_DECREF(pyx);
Py_DECREF(pyy);
if (!cmpres)
throw pyexception();
int res=PyInt_AsLong(cmpres);
Py_DECREF(cmpres);
return res<0;
}
};
static PyObject *_concat(TPyOrange *self, PyObject *obj)
{ PyTRY
CAST_TO(_ListType, aList);
PyObject *newList = _new(self->ob_type, (PyObject *)self, NULL);
if (!newList || (_setslice((TPyOrange *)newList, aList->size(), aList->size(), obj) == -1)) {
Py_XDECREF(newList);
return PYNULL;
}
else
return newList;
PyCATCH
}
static PyObject *_extend(TPyOrange *self, PyObject *args)
{ PyTRY
CAST_TO(_ListType, aList)
if (_setslice(self, aList->size(), aList->size(), args) == -1)
return NULL;
RETURN_NONE;
PyCATCH
}
static PyObject *_sort(TPyOrange *self, PyObject *args)
{
PyTRY
PyObject *cmpfunc=NULL;
if (!PyArg_ParseTuple(args, "|O:sort", &cmpfunc))
return PYNULL;
CAST_TO(_ListType, aList)
if (cmpfunc)
std::sort(aList->begin(), aList->end(), TCmpByCallback(cmpfunc));
else
std::sort(aList->begin(), aList->end());
RETURN_NONE;
PyCATCH;
}
};
template<class _WrappedListType, class _ListType, class _Element>
class ListOfUnwrappedMethods : public CommonListMethods<_WrappedListType, _ListType> {
public:
typedef typename _ListType::iterator iterator;
typedef typename _ListType::const_iterator const_iterator;
static _WrappedListType P_FromArguments(PyObject *arg)
{ if (!PySequence_Check(arg)) {
PyErr_Format(PyExc_TypeError, "invalid arguments for '%s' constructor (sequence expected)", TYPENAME(typeid(_ListType)));
return _WrappedListType();
}
_WrappedListType aList = mlnew _ListType();
for(int i=0, e=PySequence_Size(arg); i!=e; i++) {
PyObject *pyobj=PySequence_GetItem(arg, i);
_Element item;
bool ok = convertFromPython(pyobj, item);
if (!ok) {
PyErr_Format(PyExc_TypeError, "element at index %i is of wrong type ('%s')", i, pyobj ? pyobj->ob_type->tp_name : "None");
Py_DECREF(pyobj);
return _WrappedListType();
}
Py_DECREF(pyobj);
aList->push_back(item);
}
return aList;
}
static PyObject *_FromArguments(PyTypeObject *type, PyObject *arg)
{ _WrappedListType newList = P_FromArguments(arg);
return newList ? WrapOrange(newList) : PYNULL;
};
static PyObject *_new(PyTypeObject *type, PyObject *args, PyObject *)
{ if (!args || (PySequence_Check(args) && !PySequence_Size(args)))
return _CreateEmptyList(type);
if (PyTuple_Check(args) && PyTuple_Size(args)==1) {
PyObject *arg=PyTuple_GetItem(args, 0);
if (PySequence_Check(arg))
return _FromArguments(type, arg);
}
return _FromArguments(type, args);
}
static PyObject *_getitem(TPyOrange *self, int index)
{ PyTRY
CAST_TO(_ListType, aList)
return checkIndex(index, aList->size()) ? convertToPython(aList->operator[](index)) : PYNULL;
PyCATCH
}
static int _setitem(TPyOrange *self, int index, PyObject *item)
{ PyTRY
CAST_TO_err(_ListType, aList, -1)
if (!checkIndex(index, aList->size()))
return -1;
if (item==NULL) {
aList->erase(aList->begin()+index);
}
else {
_Element citem;
if (!convertFromPython(item, citem))
return -1;
aList->operator[](index)=citem;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -