Add text-attribute tags to the star tagset.
[SXSI/xpathcomp.git] / src / OCamlDriver.cpp
index 8eb3a22..4a31dad 100644 (file)
 #include "Utils.h"
 #include "common_stub.hpp"
 
-extern "C" {
-#include <stdio.h>
-}
-
 #define CAMLRAISEMSG(msg) (sxsi_raise_msg((char*) (msg)))
 
-#define XMLTREE(x) (Obj_val<XMLTree>(x))
+#define XMLTREE(x) (Obj_val<XMLTree*>(x))
 
-#define HSET(x) (Obj_val<TagIdSet>(x))
+#define HSET(x) (Obj_val<TagIdSet*>(x))
 
-#define XMLTREEBUILDER(x) (Obj_val<XMLTreeBuilder>(x))
+#define XMLTREEBUILDER(x) (Obj_val<XMLTreeBuilder*>(x))
 
 
 #define TREENODEVAL(i) ((treeNode) (Int_val(i)))
@@ -44,6 +40,8 @@ extern "C" {
 extern "C" {
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <stdio.h>
+#include <bp-darray.h>
 }
 
 
@@ -56,8 +54,9 @@ extern "C" value caml_xml_tree_builder_create(value unit)
 {
   CAMLparam1(unit);
   CAMLlocal1(result);
-  result = sxsi_alloc_custom<XMLTreeBuilder>();
-  Obj_val<XMLTreeBuilder>(result) = new XMLTreeBuilder();
+  result = sxsi_alloc_custom<XMLTreeBuilder*>();
+  Obj_val<XMLTreeBuilder*>(result) = new XMLTreeBuilder();
+
   CAMLreturn(result);
 }
 
@@ -102,8 +101,8 @@ extern "C" value caml_xml_tree_builder_close_document(value vbuilder)
   XMLTree * tree = XMLTREEBUILDER(vbuilder)->CloseDocument();
   if (tree == NULL)
     CAMLRAISEMSG("CloseDocument");
-  result = sxsi_alloc_custom<XMLTree>();
-  Obj_val<XMLTree>(result) = tree;
+  result = sxsi_alloc_custom<XMLTree*>();
+  Obj_val<XMLTree*>(result) = tree;
   CAMLreturn (result);
 }
 
@@ -158,9 +157,10 @@ extern "C"  value caml_xml_tree_load(value fd, value name, value load_tc,value s
   CAMLlocal1(result);
   XMLTree * tree;
   try {
-    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;
+
+    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);
   }
   catch (const std::exception& e){ CAMLRAISEMSG(e.what()); }
@@ -280,6 +280,10 @@ NoAlloc extern "C"  value caml_xml_tree_next_element(value tree, value node){
   return (Val_int(XMLTREE(tree)->NextElement(TREENODEVAL(node))));
 }
 
+NoAlloc extern "C"  value caml_xml_tree_next_node_before(value tree, value node, value ctx){
+  return (Val_int(XMLTREE(tree)->NextNodeBefore(TREENODEVAL(node), TREENODEVAL(ctx))));
+}
+
 NoAlloc extern "C"  value caml_xml_tree_prev_sibling(value tree, value node){
   return (Val_int(XMLTREE(tree)->PrevSibling(TREENODEVAL(node))));
 }
@@ -416,8 +420,8 @@ NoAlloc extern "C"  value caml_unordered_set_length(value hset){
 extern "C"  value caml_unordered_set_alloc(value unit){
   CAMLparam1(unit);
   CAMLlocal1(hset);
-  hset = sxsi_alloc_custom<TagIdSet>();
-  Obj_val<TagIdSet>(hset) = new TagIdSet();
+  hset = sxsi_alloc_custom<TagIdSet*>();
+  Obj_val<TagIdSet*>(hset) = new TagIdSet();
   CAMLreturn (hset);
 }
 
@@ -888,3 +892,113 @@ BV_QUERY(suffix, Suffix)
 BV_QUERY(equals, Equals)
 BV_QUERY(contains, Contains)
 BV_QUERY(lessthan, LessThan)
+
+
+////////////////////// BP
+
+extern "C" value caml_bitmap_create(value size)
+{
+  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_bitmap_resize(value bitmap, value nsize)
+{
+  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_bitmap_setbit(value bitmap, value i, value b)
+{
+  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" void caml_bp_delete(value b)
+{
+  CAMLparam1(b);
+  bp * B = Obj_val<bp*>(b);
+  bp_delete(B);
+  CAMLreturn0;
+}
+
+extern "C" value caml_bp_construct(value bitmap, value npar)
+{
+  CAMLparam2(bitmap, npar);
+  CAMLlocal1(res);
+  bp * b = bp_construct(Int_val(npar), (unsigned int *) bitmap, OPT_DEGREE);
+  res = sxsi_alloc_custom<bp*>(caml_bp_delete);
+  Obj_val<bp*>(res) = b;
+  CAMLreturn(res);
+}
+
+extern "C" value caml_bp_first_child(value b, value idx)
+{
+  CAMLparam2(b, idx);
+  CAMLreturn (Val_int( bp_first_child(Obj_val<bp*>(b), Int_val(idx))));
+}
+
+
+extern "C" value caml_bp_next_sibling(value b, value idx)
+{
+  CAMLparam2(b, idx);
+  CAMLreturn (Val_int(bp_next_sibling(Obj_val<bp*>(b), Int_val(idx))));
+}
+
+extern "C" value caml_bp_preorder_rank(value b, value idx)
+{
+  CAMLparam2(b, idx);
+  CAMLreturn (Val_int(bp_preorder_rank(Obj_val<bp*>(b), Int_val(idx)) - 1));
+}
+
+
+extern "C" value caml_bp_load(value file)
+{
+  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<bp*>(caml_bp_delete);
+  Obj_val<bp*>(result) = B;
+  CAMLreturn(result);
+}
+
+extern "C" value caml_bp_save(value b, value file)
+{
+  CAMLparam2(b, file);
+  bp *B = Obj_val<bp*>(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_bp_alloc_stats(value unit)
+{
+  CAMLparam1(unit);
+  CAMLreturn (Val_long(bp_get_alloc_stats()));
+}