X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2FOCamlDriver.cpp;h=4a31dad386e8b507824bd290ff2fdaf31ac3d88a;hb=feae5b72d47e0ae245145d401ba73804ce18fc8c;hp=be4670997a96ace81e4a25c3512a31f17d9092e7;hpb=6df6ad6cf27e57872bd5891b49354acb0a5ce6a4;p=SXSI%2Fxpathcomp.git diff --git a/src/OCamlDriver.cpp b/src/OCamlDriver.cpp index be46709..4a31dad 100644 --- a/src/OCamlDriver.cpp +++ b/src/OCamlDriver.cpp @@ -20,7 +20,6 @@ #include "XMLTree.h" #include "XMLTreeBuilder.h" -#include "Grammar.h" #include "Utils.h" #include "common_stub.hpp" @@ -32,8 +31,6 @@ #define XMLTREEBUILDER(x) (Obj_val(x)) -#define GRAMMAR(x) (Obj_val(x)) - #define TREENODEVAL(i) ((treeNode) (Int_val(i))) #define TAGVAL(i) ((TagType) (Int_val(i))) @@ -44,6 +41,7 @@ extern "C" { #include #include #include +#include } @@ -160,7 +158,7 @@ extern "C" value caml_xml_tree_load(value fd, value name, value load_tc,value s XMLTree * tree; try { - tree = XMLTree::Load(Int_val(fd),Bool_val(load_tc),Int_val(sf), String_val(name)); + tree = XMLTree::Load(Int_val(fd), Bool_val(load_tc), Int_val(sf), String_val(name)); result = sxsi_alloc_custom(); Obj_val(result) = tree; CAMLreturn(result); @@ -896,86 +894,111 @@ BV_QUERY(contains, Contains) BV_QUERY(lessthan, LessThan) +////////////////////// BP -//////////////////////////////////////////// Grammar stuff - -extern "C" value caml_grammar_load(value file, value load_bp) +extern "C" value caml_bitmap_create(value size) { - CAMLparam2(file, load_bp); - CAMLlocal1(result); - Grammar *grammar; - int f1 = Int_val(file); - int f2 = dup(f1); - FILE * fd = fdopen(f2, "r"); - if (fd == NULL) - CAMLRAISEMSG("Error opening grammar file"); - grammar = Grammar::load(fd, Bool_val(load_bp)); - fclose(fd); - result = sxsi_alloc_custom(); - Obj_val(result) = grammar; - CAMLreturn(result); + CAMLparam1(size); + size_t bits = Long_val(size); + size_t words = bits / (8*sizeof(unsigned int)); + unsigned int *buffer = (unsigned int*) calloc(words+1, sizeof(unsigned int)); + if (buffer == NULL) + CAMLRAISEMSG("BP: cannot allocate memory"); + CAMLreturn( (value) buffer); } -extern "C" value caml_grammar_get_symbol_at(value grammar, value symbol, value preorder) +extern "C" value caml_bitmap_resize(value bitmap, value nsize) { - CAMLparam3(grammar, symbol, preorder); - CAMLreturn(Val_long(GRAMMAR(grammar)->getSymbolAt(Long_val(symbol), Int_val(preorder)))); + CAMLparam2(bitmap, nsize); + size_t bits = Long_val(nsize); + size_t bytes = (bits / (8 * sizeof(unsigned int)) + 1 ) * sizeof(unsigned int); + unsigned int * buffer = (unsigned int*) realloc((void *) bitmap, bytes); + if (buffer == NULL) + CAMLRAISEMSG("BP: cannot reallocate memory"); + CAMLreturn((value) buffer); } -extern "C" value caml_grammar_first_child(value grammar, value rule, value pos) +extern "C" value caml_bitmap_setbit(value bitmap, value i, value b) { - CAMLparam1(grammar); - CAMLreturn(Val_int(GRAMMAR(grammar)->firstChild(Long_val(rule), Int_val(pos)))); + CAMLparam3(bitmap, i, b); + unsigned int j = Int_val(i); + unsigned int x = Bool_val(b); + bp_setbit ((unsigned int*) bitmap, j, x); + CAMLreturn(Val_unit); } -extern "C" value caml_grammar_next_sibling(value grammar, value rule, value pos) +extern "C" void caml_bp_delete(value b) { - CAMLparam1(grammar); - CAMLreturn(Val_int(GRAMMAR(grammar)->nextSibling(Long_val(rule), Int_val(pos)))); + CAMLparam1(b); + bp * B = Obj_val(b); + bp_delete(B); + CAMLreturn0; } -extern "C" value caml_grammar_is_nil(value grammar, value rule) +extern "C" value caml_bp_construct(value bitmap, value npar) { - CAMLparam1(grammar); - CAMLreturn(Val_bool(GRAMMAR(grammar)->isNil(Long_val(rule)))); + CAMLparam2(bitmap, npar); + CAMLlocal1(res); + bp * b = bp_construct(Int_val(npar), (unsigned int *) bitmap, OPT_DEGREE); + res = sxsi_alloc_custom(caml_bp_delete); + Obj_val(res) = b; + CAMLreturn(res); } -extern "C" value caml_grammar_get_tag(value grammar, value tag) +extern "C" value caml_bp_first_child(value b, value idx) { - CAMLparam1(grammar); - CAMLlocal1(res); - const char * s = (GRAMMAR(grammar)->getTagName(Long_val(tag))).c_str(); - res = caml_copy_string(s); - CAMLreturn(res); + CAMLparam2(b, idx); + CAMLreturn (Val_int( bp_first_child(Obj_val(b), Int_val(idx)))); } -extern "C" value caml_grammar_get_id1(value grammar, value rule) + +extern "C" value caml_bp_next_sibling(value b, value idx) { - CAMLparam1(grammar); - CAMLreturn(Val_long(GRAMMAR(grammar)->getID1(Long_val(rule)))); + CAMLparam2(b, idx); + CAMLreturn (Val_int(bp_next_sibling(Obj_val(b), Int_val(idx)))); } -extern "C" value caml_grammar_get_id2(value grammar, value rule) +extern "C" value caml_bp_preorder_rank(value b, value idx) { - CAMLparam1(grammar); - CAMLreturn(Val_long(GRAMMAR(grammar)->getID2(Long_val(rule)))); + CAMLparam2(b, idx); + CAMLreturn (Val_int(bp_preorder_rank(Obj_val(b), Int_val(idx)) - 1)); } -extern "C" value caml_grammar_get_param_pos(value grammar, value rule) + +extern "C" value caml_bp_load(value file) { - CAMLparam1(grammar); - CAMLreturn(Val_int(GRAMMAR(grammar)->getParamPos(Long_val(rule)))); + CAMLparam1(file); + CAMLlocal1(result); + bp *B; + int f1 = Int_val(file); + int f2 = dup(f1); + FILE * fd = fdopen(f2, "r"); + if (fd == NULL) + CAMLRAISEMSG("Error opening bp file"); + B = loadTree(fd); + fclose(fd); + result = sxsi_alloc_custom(caml_bp_delete); + Obj_val(result) = B; + CAMLreturn(result); } -extern "C" value caml_grammar_translate_tag(value grammar, value tag) +extern "C" value caml_bp_save(value b, value file) { - CAMLparam1(grammar); - CAMLreturn(Val_int(GRAMMAR(grammar)->translateTag(Int_val(tag)))); + CAMLparam2(b, file); + bp *B = Obj_val(b); + int f1 = Int_val(file); + int f2 = dup(f1); + FILE * fd = fdopen(f2, "a"); + fflush(stderr); + if (fd == NULL) + CAMLRAISEMSG("Error saving bp file"); + saveTree(B, fd); + fclose(fd); + CAMLreturn(Val_unit); } -extern "C" value caml_grammar_register_tag(value grammar, value str) +extern "C" value caml_bp_alloc_stats(value unit) { - CAMLparam2(grammar, str); - char * s = String_val(str); - CAMLreturn(Val_int(GRAMMAR(grammar)->getTagID(s))); + CAMLparam1(unit); + CAMLreturn (Val_long(bp_get_alloc_stats())); }