/
_rbtree.c
410 lines (369 loc) · 11.1 KB
/
_rbtree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "rbtree.h"
#define Py_LIMITED_API 0x03070000
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
// The module object
static PyObject* mod = NULL;
// Exception names
static const char* subexc[] = { "InsertError", "SearchError", };
static const char* err_label = "Incompatible types";
/*
* Documentation for _rbtree.
*/
PyDoc_STRVAR(_rbtree_doc, "CRBTree wrapper module");
static PyModuleDef _rbtree_def = {
PyModuleDef_HEAD_INIT,
"_rbtree",
_rbtree_doc,
-1, /* m_size */
};
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
RBIter *iter;
} RBIterObject;
static void RBIter_dealloc(RBIterObject* self) {
RBiter_release(self->iter);
}
static PyObject* RBIterObjectNext(RBIterObject* self) {
PyObject* data = RBnext(self->iter);
if (NULL == data) {
if (NULL == PyErr_Occurred()) {
PyErr_SetString(PyExc_StopIteration, "");
}
}
else {
Py_INCREF(data);
}
return data;
}
static PyObject* RBIterObject_iter(PyObject* self) {
return self;
}
static PyMethodDef iter_methods[] = {
{"next", (PyCFunction)RBIterObjectNext, METH_NOARGS,
PyDoc_STR("Get next element")},
{NULL},
};
static PyType_Slot RBIterSlot[] = {
{Py_tp_doc, PyDoc_STR("Internal RBIter")},
{Py_tp_methods, iter_methods},
{Py_tp_iter, RBIterObject_iter},
{Py_tp_iternext, RBIterObjectNext},
{Py_tp_dealloc, RBIter_dealloc},
{0, NULL},
};
static PyType_Spec RBIterTypeSpec = {
.name = "_rbtree.RBIter",
.basicsize = sizeof(RBIterObject),
.flags = Py_TPFLAGS_DEFAULT,
.slots = RBIterSlot,
};
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
RBTree tree;
} RBTreeObject;
static int obj_comp(const void* first, const void* second, int *err) {
PyObject* ofirst = (PyObject *)first;
PyObject* osecond = (PyObject*)second;
int cr = PyObject_RichCompareBool(ofirst, osecond, Py_LT);
if (cr == 1) return -1;
if (cr == -1) {
*err = 1;
return 0;
}
cr = PyObject_RichCompareBool(ofirst, osecond, Py_EQ);
if (cr == -1) {
*err = 1;
return 0;
}
return (cr == 0);
}
static int key_comp(const void* first, const void* second, int* err) {
if ((!PyTuple_Check((PyObject*)first)) || (!PyTuple_Check((PyObject*)second))) {
goto except;
}
PyObject* ofirst = PyTuple_GetItem((PyObject*)first, 0);
PyObject* osecond = PyTuple_GetItem((PyObject*)second, 0);
if ((NULL == ofirst) || (NULL == osecond)) {
goto except;
}
return obj_comp(ofirst, osecond, err);
except:
PyErr_SetString(PyExc_TypeError, "Tuple expected");
*err = 1;
return 0;
}
static int
RBTree_init(RBTreeObject* self, PyObject* args, PyObject* kwds) {
int mapping = 0;
if (! PyArg_ParseTuple(args, "|p", &mapping)) return -1;
RBinit2(&self->tree, mapping ? key_comp : obj_comp);
return 0;
}
static void node_deleter(const void* data) {
Py_DECREF((PyObject*)data);
}
static void RBTree_dealloc(RBTreeObject* self) {
RBdestroy(&self->tree, node_deleter);
}
int wrap_exception(const char *exc_name, const char* label) {
PyObject* exc, * val, * val2, * tb;
PyObject* exc_type = NULL;
if (NULL == PyErr_Occurred()) {
exc_type = PyObject_GetAttrString(mod, exc_name);
if (NULL == exc_type) {
exc_type = PyExc_RuntimeError;
Py_INCREF(exc_type);
}
PyErr_SetString(exc_type, label);
}
else {
PyErr_Fetch(&exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb);
if (tb != NULL) {
PyException_SetTraceback(val, tb);
Py_DECREF(tb);
}
Py_DECREF(exc);
exc_type = PyObject_GetAttrString(mod, exc_name);
if (NULL == exc_type) {
exc_type = PyExc_RuntimeError;
Py_INCREF(exc_type);
}
PyErr_SetString(exc_type, label);
PyErr_Fetch(&exc, &val2, &tb);
PyErr_NormalizeException(&exc, &val2, &tb);
Py_INCREF(val);
PyException_SetCause(val2, val);
PyException_SetContext(val2, val);
PyErr_Restore(exc, val2, tb);
}
Py_DECREF(exc_type);
return 1;
}
static PyObject* RBTreeInsert(RBTreeObject* self, PyObject* args) {
PyObject* data = NULL;
int cr = PyArg_UnpackTuple(args, "value", 1, 2, &data);
if (!cr) return NULL;
int err = 0;
PyObject* previous = RBinsert(&self->tree, data, &err);
if (err) {
wrap_exception(subexc[0], err_label);
return NULL;
}
Py_INCREF(data);
if (NULL == previous) {
Py_RETURN_NONE;
}
return previous;
}
static PyObject* RBTreeRemove(RBTreeObject* self, PyObject* args) {
PyObject* data = NULL;
int cr = PyArg_UnpackTuple(args, "key", 1, 1, &data);
if (!cr) return NULL;
PyObject* old = RBremove(&self->tree, data);
if (!old) {
if (NULL != PyErr_Occurred()) {
PyErr_Clear();
}
Py_RETURN_NONE;
}
return old;
}
static PyObject* find(RBTreeObject* self, PyObject* data) {
PyObject* old = RBfind(&self->tree, data);
if (!old) {
if (NULL != PyErr_Occurred()) {
PyErr_Clear();
}
Py_RETURN_NONE;
}
Py_INCREF(old);
return old;
}
static PyObject* RBTreeGetItem(RBTreeObject* self, PyObject* key) {
PyObject* obj = PyTuple_Pack(2, key, Py_None);
if (NULL == obj) return NULL;
PyObject* old = find(self, obj);
Py_DECREF(obj);
if (old == Py_None) {
Py_DECREF(old);
PyErr_SetString(PyExc_KeyError, "Not found");
return NULL;
}
obj = old;
old = PyTuple_GetItem(obj, 1);
Py_INCREF(old);
Py_DECREF(obj);
return old;
}
static PyObject* build_args(PyObject* key, PyObject* val) {
PyObject* obj = PyTuple_Pack(2, key, val);
if (NULL == obj) return NULL;
PyObject* args = PyTuple_Pack(1, obj);
Py_DECREF(obj);
return args;
}
static int RBTreeSetItem(RBTreeObject* self, PyObject* key, PyObject* val) {
PyObject* args, *old = NULL;
if (NULL == val) {
args = build_args(key, Py_None);
if (NULL == args) return -1;
old = RBTreeRemove(self, args);
Py_DECREF(args);
if (Py_None == old) {
Py_DECREF(old);
PyErr_SetString(PyExc_KeyError, "Not found");
return -1;
}
}
else {
args = build_args(key, val);
old = RBTreeInsert(self, args);
Py_DECREF(args);
if (NULL == old) return -1;
}
Py_DECREF(old);
return 0;
}
static PyObject* RBTreeFind(RBTreeObject* self, PyObject* args) {
PyObject* data = NULL;
int cr = PyArg_UnpackTuple(args, "key", 1, 1, &data);
if (!cr) return NULL;
return find(self, data);
}
static PyObject* build_iter(RBIter *iter) {
RBIterObject* iterobj = NULL;
if (NULL == iter) {
wrap_exception(subexc[1], err_label);
return NULL;
}
PyTypeObject* itertyp = (PyTypeObject*)PyObject_GetAttrString(mod, "RBIter");
if (NULL == itertyp) goto except;
iterobj = PyObject_New(RBIterObject, itertyp);
if (NULL == iterobj) goto except;
iterobj->iter = iter;
goto finally;
except:
if (iter) RBiter_release(iter);
finally:
Py_XDECREF(itertyp);
return (PyObject*)iterobj;
}
static PyObject* RBTreeSearch(RBTreeObject* self, PyObject* args) {
PyObject* data = NULL;
int cr = PyArg_UnpackTuple(args, "key", 1, 1, &data);
if (!cr) return NULL;
RBIter* iter = RBsearch(&self->tree, data);
return build_iter(iter);
}
static PyObject* RBTreeFirst(RBTreeObject* self, PyObject *args) {
RBIter* iter = RBfirst(&self->tree);
return build_iter(iter);
}
static PyObject* RBTree_iter(PyObject* self) {
return RBTreeFirst((RBTreeObject*)self, NULL);
}
static PyObject* add_ref(PyObject* obj) {
Py_INCREF(obj);
return obj;
}
static PyObject* RBTreeClone(RBTreeObject* self) {
RBTreeObject* obj = NULL;
PyTypeObject* typ = (PyTypeObject*)PyObject_GetAttrString(mod, "RBTree");
if (NULL == typ) goto except;
obj = PyObject_New(RBTreeObject, typ);
Py_DECREF(typ);
if (NULL == obj) goto except;
RBTree * tree = RBclone(&self->tree, &add_ref);
memcpy(&(obj->tree), tree, sizeof(*tree));
free(tree);
except:
return obj;
}
static PyMethodDef methods[] = {
{"insert", (PyCFunction)RBTreeInsert, METH_VARARGS, PyDoc_STR("Inserts an element")},
{"remove", (PyCFunction)RBTreeRemove, METH_VARARGS, PyDoc_STR("Removes an element")},
{"find", (PyCFunction)RBTreeFind, METH_VARARGS, PyDoc_STR("Finds an element")},
{"search", (PyCFunction)RBTreeSearch, METH_VARARGS, PyDoc_STR("Searches for an element")},
{"first", (PyCFunction)RBTreeFirst, METH_NOARGS, PyDoc_STR("Gets an iterator on first element") },
{"clone", (PyCFunction)RBTreeClone, METH_NOARGS, PyDoc_STR("Duplicates a tree")},
{NULL},
};
static PyMemberDef members[] = {
{"black_depth", T_UINT, offsetof(RBTree, black_depth) + offsetof(RBTreeObject, tree),
READONLY, PyDoc_STR("Black depth of the (red-black) tree")},
{"count", T_UINT, offsetof(RBTree, count) + offsetof(RBTreeObject, tree),
READONLY, PyDoc_STR("Number of elements in the tree")},
{NULL}
};
Py_ssize_t RBTree_length(PyObject* self) {
return ((RBTreeObject*)self)->tree.count;
}
static PyType_Slot RBtreeSlot[] = {
{Py_tp_doc, PyDoc_STR("Internal RBTree")},
{Py_tp_init, (initproc) RBTree_init},
{Py_tp_methods, methods},
{Py_tp_members, members},
{Py_tp_iter, RBTree_iter},
{Py_mp_length, RBTree_length},
{Py_mp_subscript, RBTreeGetItem},
{Py_mp_ass_subscript, RBTreeSetItem},
{Py_tp_dealloc, RBTree_dealloc},
{0, NULL},
};
static PyType_Spec RBTreeTypeSpec = {
.name = "_rbtree.RBTree",
.basicsize = sizeof(RBTreeObject),
.flags = Py_TPFLAGS_DEFAULT,
.slots = RBtreeSlot,
};
PyMODINIT_FUNC PyInit__rbtree() {
PyObject* typ = NULL;
PyObject* base_exc = NULL;
mod = PyModule_Create(&_rbtree_def);
if (!mod) goto except;
typ = PyType_FromSpec(&RBTreeTypeSpec);
if (!typ) goto except;
int cr = PyModule_AddObject(mod, "RBTree", typ);
if (cr) {
Py_DECREF(typ);
goto except;
}
typ = PyType_FromSpec(&RBIterTypeSpec);
if (!typ) goto except;
cr = PyModule_AddObject(mod, "RBIter", typ);
if (cr) {
Py_DECREF(typ);
goto except;
}
// Add Exception classes
base_exc = PyErr_NewException("pyrbtree.RBError", NULL, NULL);
if (!base_exc) goto except;
cr = PyModule_AddObject(mod, "pyrbtree.RBError", base_exc);
if (cr) {
Py_DECREF(base_exc);
goto except;
}
for (int i = 0; i < sizeof(subexc) / sizeof(*subexc); i++) {
char name[32];
strcpy(name, "pyrbtree.");
strcat(name, subexc[i]);
typ = PyErr_NewException(name, base_exc, NULL);
if (!typ) goto except;
cr = PyModule_AddObject(mod, subexc[i], typ);
if (cr) {
Py_DECREF(typ);
goto except;
}
}
if (0 != PyModule_AddStringConstant(mod, "__author__", "SBA")) goto except;
goto finally;
except:
if (mod) Py_DECREF(mod);
finally:
return mod;
}