X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2FOCamlDriver.cpp;h=b93286cb1175b7603be98e044a09f4af2cb6bde6;hb=718a2eff89f4798ee47e055556f500dc950a82b7;hp=074dd1c97121c0c045674fe995f4a17c91023fd5;hpb=dc9e9ba2f50ce881c3a77743663829d661363d51;p=SXSI%2Fxpathcomp.git diff --git a/src/OCamlDriver.cpp b/src/OCamlDriver.cpp index 074dd1c..b93286c 100644 --- a/src/OCamlDriver.cpp +++ b/src/OCamlDriver.cpp @@ -41,9 +41,19 @@ extern "C" { #include #include #include +#include } +extern "C" value caml_clz(value i) +{ + return Val_long( ((sizeof(unsigned long)*8) - __builtin_clzl(Long_val(i))) - 1); +} + +extern "C" value caml_leading_bit(value i) +{ + return Val_long( ( 1 << (sizeof(unsigned long)*8 - __builtin_clzl(Long_val(i)) - 1))); +} /** XMLTreeBuilder bindings * @@ -157,7 +167,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); @@ -861,28 +871,48 @@ extern "C" value caml_text_collection_lessthan(value tree,value str){ /** Full reporting into a bit vector */ +static std::vector sort_results(std::vector v) +{ + std::vector res; + std::sort(v.begin(), v.end()); + DocID prev = NULLT; + for(auto i = v.begin(); i != v.end(); ++i){ + while (prev == *i){ + ++i; + if (i == v.end()) return res; + }; + prev = *i; + res.push_back(prev); + }; + return res; +} #define BV_QUERY(pref, Pref) \ - extern "C" value caml_text_collection_## pref ##_bv(value tree, value str){ \ - CAMLparam2(tree, str); \ - CAMLlocal3(res, res_bv, res_array); \ - int j; \ - uchar * cstr = (uchar *) strdup(String_val(str)); \ - std::vector results = XMLTREE(tree)->Pref(cstr); \ - res_bv = caml_alloc_string((XMLTREE(tree)->Size() / 4) + 2); \ - unsigned long slen = caml_string_length(res_bv); \ - memset(&(Byte(res_bv,0)), 0, slen); \ - res_array = caml_alloc_shr(results.size(), 0); \ - for (unsigned int i = 0; i < results.size(); ++i) { \ - j = XMLTREE(tree)->ParentNode(results[i]); \ - Byte(res_bv, j >> 3) |= (1 << (j & 7)); \ - caml_initialize(&Field(res_array, i), Val_int(j)); \ - }; \ - free(cstr); \ - res = caml_alloc(2, 0); \ - Store_field(res, 0, res_bv); \ - Store_field(res, 1, res_array); \ - CAMLreturn(res); \ + extern "C" value caml_text_collection_## pref ##_bv(value tree, value str, value dobvv){ \ + CAMLparam3(tree, str, dobvv); \ + CAMLlocal3(res, res_bv, res_array); \ + int j; \ + uchar * cstr = (uchar *) strdup(String_val(str)); \ + std::vector uresults = XMLTREE(tree)->Pref(cstr); \ + std::vector results = sort_results(uresults); \ + bool dobv = Bool_val(dobvv); \ + res_bv = caml_alloc_string(dobv ? ((XMLTREE(tree)->Size() / 4) + 2) : 0); \ + unsigned long slen = caml_string_length(res_bv); \ + if (dobv) \ + memset(&(Byte(res_bv,0)), 0, slen); \ + res_array = caml_alloc_shr(results.size(), 0); \ + for (unsigned int i = 0; i < results.size(); ++i) { \ + j = XMLTREE(tree)->ParentNode(results[i]); \ + if (dobv) { \ + Byte(res_bv, j >> 3) |= (1 << (j & 7)); \ + }; \ + caml_initialize(&Field(res_array, i), Val_int(j)); \ + }; \ + free(cstr); \ + res = caml_alloc(2, 0); \ + Store_field(res, 0, res_bv); \ + Store_field(res, 1, res_array); \ + CAMLreturn(res); \ } \