int main(int argc, char **argv) { LST_STree *tree; LST_String *string = NULL; LST_StringSet *set; int i; if (argc < 2) test_usage(argv[0]); /* Create a string set to conveniently hold all our strings: */ set = lst_stringset_new(); /* Add all strings passed on the command line to the set. * Note that we pass the string length excluding the null * terminator, libstree handles that generically. */ for (i = 1; i < argc; i++) { string = lst_string_new(argv[i], 1, strlen(argv[i])); lst_stringset_add(set, string); } /* Create a suffix tree for all strings in the set: */ tree = lst_stree_new(set); fprintf(stderr, "Tree after all insertions:\n"); lst_debug_print_tree(tree); /* Free suffix tree: */ lst_stree_free(tree); return 0; }
static PyObject* st_destroy(PyObject* self, PyObject* args) { tree_handle_t *handle; if (!PyArg_ParseTuple(args, "l:st_destroy", (long*)&handle)) return NULL; lst_alg_bus(handle->tree, (LST_NodeVisitCB)clear_annotations, NULL); lst_stree_free(handle->tree); handle->tree = NULL; lst_stringset_free(handle->string_set); handle->string_set = NULL; free(handle); Py_INCREF(Py_None); return Py_None; }
static PyObject* st_create(PyObject* self, PyObject* args) { PyObject *pylist = NULL, *pystring = NULL; char *cstring = NULL; int length; tree_handle_t *handle; LST_String *string; /* parse arguments */ if (!PyArg_ParseTuple(args, "O:st_create", &pylist)) return NULL; /* create the tree */ handle = malloc(sizeof(tree_handle_t)); if (handle == NULL) { return PyErr_NoMemory(); } handle->tree = lst_stree_new(NULL); assert(handle != NULL); /* fill in the tree and stringset */ PyObject *iterator = PyObject_GetIter(pylist); handle->string_set = lst_stringset_new(); assert(handle->string_set != NULL); handle->numstrings = 0; while((pystring = PyIter_Next(iterator)) != NULL) { handle->numstrings++; if (PyString_AsStringAndSize(pystring,&cstring,&length)) { Py_DECREF(iterator); lst_stree_free(handle->tree); lst_stringset_free(handle->string_set); free(handle); return NULL; } /* LST_String is pointing back at the python string's data. * python wrapper class keeps a reference to each string added, * so don't keep the reference here. */ Py_DECREF(pystring); /* this is freed when stringset is freed */ string = (LST_String*)malloc(sizeof(LST_String)); if (string == NULL) { Py_DECREF(iterator); lst_stree_free(handle->tree); lst_stringset_free(handle->string_set); free(handle); return PyErr_NoMemory(); } lst_string_init(string, cstring, 1, length); lst_stringset_add(handle->string_set, string); lst_stree_add_string(handle->tree, string); } Py_DECREF(iterator); iterator = NULL; /* not annotated */ handle->annotated = False; // lst_debug_print_tree(handle->tree); /* return pointer to the handle */ return Py_BuildValue("l", (long)handle); }