📄 vectortemplates.hpp
字号:
return 0;
PyCATCH_1
}
static int _setslice(TPyOrange *self, int start, int stop, PyObject *args)
{ PyTRY
CAST_TO_err(_ListType, aList, -1)
if (!checkIndices(start, stop, aList->size()))
return -1;
if (args==NULL) {
aList->erase(aList->begin()+start, aList->begin()+stop);
return 0;
}
PyObject *emdict = NULL, *newList = NULL;
try {
PyObject *emdict = PyDict_New();
PyObject *newList = _new(self->ob_type, args, emdict);
Py_DECREF(emdict); emdict=NULL;
if (!newList)
return -1;
NAME_CAST_TO_err(_ListType, newList, nList, -1)
aList->erase(aList->begin()+start, aList->begin()+stop);
aList->insert(aList->begin()+start, nList->begin(), nList->end());
Py_DECREF(newList);
newList = NULL;
return 0;
}
catch (exception err)
{ Py_XDECREF(emdict);
Py_XDECREF(newList);
throw;
}
PyCATCH_1
}
static PyObject *_richcmp(TPyOrange *self, PyObject *other, int op)
{ PyTRY
PyObject *myItem = NULL, *hisItem = NULL;
try {
if (!PySequence_Check(other)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
CAST_TO(_ListType, aList)
int myLen = aList->size();
int hisLen = PySequence_Size(other);
if (myLen != hisLen) {
if (op == Py_EQ) {
Py_INCREF(Py_False);
return Py_False;
}
if (op == Py_NE) {
Py_INCREF(Py_True);
return Py_True;
}
}
int len = myLen < hisLen ? myLen : hisLen;
int k = 0;
iterator ii(aList->begin());
for (int pos=0; !k && (pos<len); pos++) {
myItem = convertToPython(*(ii++));
hisItem = PySequence_GetItem(other, pos);
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)
{ CAST_TO(_ListType, aList);
string res("<");
for(const_iterator bi(aList->begin()), ei(bi), ee(aList->end()); ei!=ee; ei++) {
if (ei!=bi)
res += ", ";
PyObject *obj = convertToPython(*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
_Element obj;
if (!convertFromPython(item, obj))
return PYNULL;
CAST_TO(_ListType, aList);
aList->push_back(obj);
RETURN_NONE;
PyCATCH
}
static PyObject *_count(TPyOrange *self, PyObject *item)
{ PyTRY
_Element obj;
if (!convertFromPython(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
_Element obj;
if (!convertFromPython(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=convertToPython(*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
_Element obj;
if (!convertFromPython(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;
_Element item;
if ( !PyArg_ParseTuple(args, "iO", &index, &obj)
|| !checkIndex(index, aList->size())
|| !(convertFromPython(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(const_iterator li = aList->begin(), le = aList->end(); li!=le; li++)
PyList_SetItem(newList, i++, convertToPython(*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
_Element obj;
if (!convertFromPython(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 _Element &x, const _Element &y) const
{ PyObject *pyx=convertToPython(x), *pyy=convertToPython(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;
}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -