Remove unused memory profiling code.
[SXSI/xpathcomp.git] / src / OCamlDriver.cpp
index 074dd1c..b93286c 100644 (file)
@@ -41,9 +41,19 @@ extern "C" {
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <stdio.h>
+#include <bp-darray.h>
 }
 
 
+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<XMLTree*>();
     Obj_val<XMLTree*>(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<DocID> sort_results(std::vector<DocID> v)
+{
+  std::vector<DocID> 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<DocID> 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<DocID> uresults = XMLTREE(tree)->Pref(cstr);           \
+    std::vector<DocID> 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);                                                   \
   }                                                                    \