-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_bindings.c
More file actions
551 lines (462 loc) · 13.1 KB
/
Copy pathpython_bindings.c
File metadata and controls
551 lines (462 loc) · 13.1 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* Python suffix tree */
/* Originally developed by Thomas Mailund <mailund@birc.dk> and Søren Besenbacher <besen@birc.dk> */
/* Adapted by Dell Zhang <dell.z@ieee.org> to support Unicode text and new verision Python. */
#include <wchar.h>
#include "suffix_tree.h"
#include <Python.h>
#include <structmember.h>
typedef struct SuffixTreeObject {
PyObject_HEAD
suffix_tree_t *tree;
} SuffixTreeObject;
typedef struct NodeObject {
PyObject_HEAD
/* Add dictionary to handle user-annotation */
PyObject *dict;
SuffixTreeObject *tree;
node_t *node;
} NodeObject;
static PyObject *SuffixTree_new (PyTypeObject *type,
PyObject *args,
PyObject *kwds);
static int SuffixTree_init (SuffixTreeObject *self,
PyObject *args,
PyObject *kwds);
static PyObject *SuffixTree_root (SuffixTreeObject *self);
static PyObject *SuffixTree_string (SuffixTreeObject *self);
static PyObject* wrap_node (SuffixTreeObject *tree,
node_t *n);
static PyObject *Node_new (PyTypeObject *type,
PyObject *args,
PyObject *kwds);
static void Node_dealloc (NodeObject *self);
static int Node_compare (NodeObject *n1,
NodeObject *n2);
static long Node_hash (NodeObject *self);
static PyObject *Node_start (NodeObject *self);
static PyObject *Node_end (NodeObject *self);
static PyObject *Node_index (NodeObject *self);
static PyObject *Node_string_depth (NodeObject *self);
static PyObject *Node_edge_label (NodeObject *self);
static PyObject *Node_path_label (NodeObject *self);
static PyObject *Node_suffix (NodeObject *self);
static PyObject *Node_parent (NodeObject *self);
static PyObject *Node_next (NodeObject *self);
static PyObject *Node_prev (NodeObject *self);
static PyObject *Node_first_child (NodeObject *self);
static PyObject *Node_last_child (NodeObject *self);
static PyObject *Node_is_leaf (NodeObject *self);
/**********************************************************************/
/* exporting stuff to python */
/**********************************************************************/
static PyGetSetDef SuffixTree_getseters[] = {
{"root", (getter)SuffixTree_root, NULL,
"The root-node of the tree.",
NULL /* no closure */
},
{"string", (getter)SuffixTree_string, NULL,
"The string the tree is built over + the terminal symbol.",
NULL /* no closure */
},
{0} /* sentinel */
};
/* FIXME: deallocation of this guy! */
static PyTypeObject SuffixTreeType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"_suffix_tree.SuffixTree", /*tp_name*/
sizeof(SuffixTreeObject), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Suffix tree object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
SuffixTree_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)SuffixTree_init, /* tp_init */
0, /* tp_alloc */
SuffixTree_new, /* tp_new */
};
static PyGetSetDef Node_getseters[] = {
{"start", (getter)Node_start, NULL,
"The start index of the label on the edge from parent to this node.",
NULL /* no closure */
},
{"end", (getter)Node_end, NULL,
"The end index of the label on the edge from parent to this node.",
NULL /* no closure */
},
{"index", (getter)Node_index, NULL,
"If the node is a leaf, this is the start of the suffix. If the node "
"is an inner node, it is the index of one of its leaves.",
NULL /* no closure */
},
{"stringDepth", (getter)Node_string_depth, NULL,
"The string depth (length of the path label) of this node. "
"Notice that this is not the *tree* depth, that is, not the number of "
"nodes between the root and this node,",
NULL /* no closure */
},
{"edgeLabel", (getter)Node_edge_label, NULL,
"The label on the edge to this node's parent (string[start:end+1]).",
NULL /* no closure */
},
{"pathLabel", (getter)Node_path_label, NULL,
"The label on the path to this node from the root.",
NULL /* no closure */
},
{"suffix", (getter)Node_suffix, NULL,
"The string string[index:], which for leaves is the correct suffix "
"but for inner nodes is really just some suffix.",
NULL /* no closure */
},
{"parent", (getter)Node_parent, NULL,
"The parent of this node, or None if the node is the root.",
NULL /* no closure */
},
{"next", (getter)Node_next, NULL,
"The next sibling of this node, or None.",
NULL /* no closure */
},
{"prev", (getter)Node_prev, NULL,
"The previous sibling of this node, or None.",
NULL /* no closure */
},
{"firstChild", (getter)Node_first_child, NULL,
"The first child of this node, or None.",
NULL /* no closure */
},
{"lastChild", (getter)Node_last_child, NULL,
"The last child of this node, or None.",
NULL /* no closure */
},
{ "isLeaf", (getter)Node_is_leaf, NULL,
"Predicate that is true if self is a leaf, false otherwise.", NULL
},
{0} /* sentinel */
};
static PyTypeObject NodeType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"_suffix_tree.SuffixTreeNode", /*tp_name*/
sizeof(NodeObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Node_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
(cmpfunc)Node_compare, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)Node_hash, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Suffix tree node object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
Node_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
offsetof(NodeObject,dict), /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
Node_new, /* tp_new */
};
static PyMethodDef _suffix_tree_methods[] = {
{NULL} /* Sentinel */
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
init_suffix_tree(void)
{
PyObject *m;
if (PyType_Ready(&SuffixTreeType) < 0)
return;
if (PyType_Ready(&NodeType) < 0)
return;
m = Py_InitModule3("_suffix_tree", _suffix_tree_methods,
"Wrapper module for suffix trees.");
if (m == NULL)
return;
Py_INCREF(&SuffixTreeType);
Py_INCREF(&NodeType);
PyModule_AddObject(m, "SuffixTree", (PyObject *)&SuffixTreeType);
}
/**********************************************************************/
/* tree bindings */
/**********************************************************************/
static PyObject *
SuffixTree_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
SuffixTreeObject *self = (SuffixTreeObject *)type->tp_alloc(type, 0);
self->tree = NULL;
return (PyObject*)self;
}
static int
SuffixTree_init(SuffixTreeObject *self, PyObject *args, PyObject *kwds)
{
PyUnicodeObject *string = NULL;
PyUnicodeObject *terminal = NULL;
static char *kwlist[] = {"string", "terminal", NULL};
static wchar_t* s;
static wchar_t t[1];
int n;
Py_ssize_t input_string_size;
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|uu", kwlist,
&string, &terminal))
return -1; /* rethrow exception */
if (!(string = (PyUnicodeObject *) PyTuple_GetItem(args,0))) return -1; /* rethrow */
if (!(terminal = (PyUnicodeObject *) PyTuple_GetItem(args,1))) return -1; /* rethrow */
input_string_size = PyUnicode_GetSize(string);
s = malloc((input_string_size + 1) * sizeof(wchar_t));
if ((n = PyUnicode_AsWideChar(string,s,input_string_size)) < 0) return -1; /* rethrow */
/* n is actually ignored here -- the size of string s is wcslen(s) */
if ((n = PyUnicode_AsWideChar(terminal,t,1)) < 0) return -1; /* rethrow */
if (n != 1) // not a single terminal symbol
{
PyErr_SetString(PyExc_RuntimeError,
"Terminal symbol must be a single character!");
return -1;
}
s[input_string_size] = '\0';
t[1] = '\0';
self->tree = st_make(s,*t);
free(s);
if (!self->tree) {
PyErr_SetString(PyErr_NoMemory(),
"Could not allocated suffix tree!");
return -1;
}
return 0;
}
static PyObject *
SuffixTree_string(SuffixTreeObject *self)
{
return PyUnicode_FromWideChar(self->tree->str, self->tree->str_len);
}
static PyObject*
wrap_node(SuffixTreeObject *tree, node_t *n)
{
NodeObject *node;
if (n->python_node)
{
Py_INCREF((PyObject*)n->python_node);
return (PyObject*)n->python_node;
}
node = (NodeObject*)_PyObject_New((PyTypeObject*)&NodeType);
if (!node) return 0;
node->dict = PyDict_New();
node->tree = tree;
node->node = n;
//Py_INCREF((PyObject*)tree); //FIXME: handle cyclic GC and make
//sure tree isn't deleted while we have a node
// make sure the node is never deleted while the tree is in existence
Py_INCREF(node);
n->python_node = node;
return (PyObject*)node;
}
static PyObject*
SuffixTree_root(SuffixTreeObject *self)
{
return wrap_node(self,self->tree->root);
}
/**********************************************************************/
/* node bindings */
/**********************************************************************/
static PyObject *
Node_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
NodeObject *self = (NodeObject *)type->tp_alloc(type, 0);
self->dict = NULL;
self->tree = NULL;
self->node = NULL;
return (PyObject*)self;
}
static int
Node_clear(NodeObject *self)
{
Py_XDECREF(self->dict); self->dict = NULL;
Py_XDECREF(self->tree); self->tree = NULL;
return 0;
}
static void
Node_dealloc(NodeObject *self)
{
Node_clear(self);
self->ob_type->tp_free((PyObject*)self);
}
static int
Node_compare(NodeObject *n1, NodeObject *n2)
{
return (int)(n1->node - n2->node);
}
static long
Node_hash(NodeObject *self)
{
return (long)(self->node);
}
static PyObject *
Node_start(NodeObject *self)
{
return PyInt_FromLong(self->node->start);
}
static PyObject *
Node_end(NodeObject *self)
{
return PyInt_FromLong(self->node->end);
}
static PyObject *
Node_index(NodeObject *self)
{
return PyInt_FromLong(self->node->term_number);
}
static PyObject *
Node_string_depth(NodeObject *self)
{
return PyInt_FromLong(self->node->depth);
}
static PyObject *
Node_edge_label(NodeObject *self)
{
int start = self->node->start;
int end = self->node->end+1;
int length = end-start;
/* the root is a special case... */
if (start < 0) return PyUnicode_FromWideChar(L"", 0);
return PyUnicode_FromWideChar(self->tree->tree->str + start, length);
}
static PyObject *
Node_path_label(NodeObject *self)
{
int start = self->node->term_number;
int length = self->node->depth;
return PyUnicode_FromWideChar(self->tree->tree->str + start, length);
}
static PyObject *
Node_suffix(NodeObject *self)
{
int start = self->node->term_number;
int length = self->tree->tree->str_len - start;
/* the root is a special case... */
if (start < 0) return SuffixTree_string(self->tree);
return PyUnicode_FromWideChar(self->tree->tree->str + start, length);
}
static PyObject *
Node_parent(NodeObject *self)
{
node_t *parent = self->node->parent;
if (!parent)
{
Py_INCREF(Py_None);
return Py_None;
}
else
{
return wrap_node(self->tree,parent);
}
}
static PyObject *
Node_next(NodeObject *self)
{
node_t *next = self->node->next;
if (!next)
{
Py_INCREF(Py_None);
return Py_None;
}
else
{
return wrap_node(self->tree,next);
}
}
static PyObject *
Node_prev(NodeObject *self)
{
node_t *prev = self->node->prev;
if (!prev)
{
Py_INCREF(Py_None);
return Py_None;
}
else
{
return wrap_node(self->tree,prev);
}
}
static PyObject *
Node_first_child(NodeObject *self)
{
node_t *c = self->node->children.head;
if (!c)
{
Py_INCREF(Py_None);
return Py_None;
}
else
{
return wrap_node(self->tree,c);
}
}
static PyObject *
Node_last_child(NodeObject *self)
{
node_t *c = self->node->children.tail;
if (!c)
{
Py_INCREF(Py_None);
return Py_None;
}
else
{
return wrap_node(self->tree,c);
}
}
static PyObject*
Node_is_leaf(NodeObject *self)
{
if (self->node && self->node->children.head)
{ Py_INCREF(Py_False); return Py_False; }
else
{ Py_INCREF(Py_True); return Py_True; }
}