Further optimisations, changed the prototype of Tree.mli
[SXSI/xpathcomp.git] / OCamlDriver.cpp
index 8603c11..5b7e0b6 100644 (file)
@@ -16,9 +16,28 @@ extern "C" {
 #include <caml/callback.h>
 #include <caml/fail.h>
 #include <caml/custom.h>
+
+
+#include <unistd.h>
+#include <sys/times.h>
+#include <time.h>
+#include <sys/stat.h>
+
+  struct tms t1;
+  struct tms t2;
+  double ticks = (double) sysconf(_SC_CLK_TCK)/1000;
   
+  void start_clock() {
+    times (&t1);
+  }
+
+
+  double stop_clock() {
+    times (&t2);
+    return (t2.tms_utime-t1.tms_utime)/ticks;
+  }
+} //extern C  
 
-} //extern C
 
 //#include "TextCollection/TextCollection.h"
 #include "XMLDocShredder.h"
@@ -30,6 +49,7 @@ extern "C" {
 #define XMLTREE(x) ((XMLTree *)(* (XMLTree**) Data_custom_val(x)))
 #define TEXTCOLLECTION(x)
 #define TREENODEVAL(i) ((treeNode) (Int_val(i)))
+#define XMLTREE_ROOT 0
 
 extern "C" {
   static struct custom_operations ops;
@@ -124,7 +144,7 @@ void traversal_rec(XMLTree* tree, treeNode id){
 
 extern "C" CAMLprim value caml_cpp_traversal(value tree){
   CAMLparam1(tree);
-  traversal_rec(XMLTREE(tree),XMLTREE(tree)->Root());
+  traversal_rec(XMLTREE(tree),XMLTREE_ROOT);
   CAMLreturn(Val_unit);
 }
 
@@ -146,14 +166,6 @@ extern "C" CAMLprim value caml_text_collection_get_cached_text(value tree, value
   CAMLreturn (str);
 }
 
-extern "C" CAMLprim value caml_text_collection_size(value tree){
-  CAMLparam1(tree);
-  //  CAMLreturn (Val_int( XMLTREE(tree)->CachedText.size()));
-  NOT_IMPLEMENTED("text_collection_size");
-  CAMLreturn (Val_unit);
-}
-
-
 
 extern "C" CAMLprim value caml_text_collection_empty_text(value tree,value id){
   CAMLparam2(tree,id);
@@ -194,11 +206,21 @@ extern "C" CAMLprim value caml_text_collection_contains(value tree,value str){
   };
   CAMLreturn (resarray);  
 }
+extern "C" CAMLprim value caml_text_collection_unsorted_contains(value tree,value str){
+  CAMLparam2(tree,str);
+  uchar * cstr = (uchar *) String_val(str);  
+  std::vector<DocID> results;
+  start_clock();
+  results = XMLTREE(tree)->Contains(cstr);
+  double d = stop_clock();
+  std::cerr << "Internal timing " << d <<" ms\n";
+  CAMLreturn (Val_unit);  
+}
 
 
 extern "C" CAMLprim value caml_xml_tree_root(value tree){
   CAMLparam1(tree);
-  CAMLreturn (Val_int(TREENODEVAL(XMLTREE(tree)->Root())));
+  CAMLreturn (Val_int(TREENODEVAL(XMLTREE_ROOT)));
 }
 extern "C" CAMLprim value caml_xml_tree_text_collection(value tree){
   CAMLparam1(tree);
@@ -240,12 +262,32 @@ extern "C" CAMLprim value caml_xml_tree_unserialize(value filename){
   CAMLreturn(Val_unit);
 }
 
+extern "C" CAMLprim value caml_xml_tree_last_child(value tree, value id){
+  CAMLparam2(tree,id);
+  CAMLreturn(Val_int (XMLTREE(tree)->LastChild(TREENODEVAL(id))));
+}
+
+extern "C" CAMLprim value caml_xml_tree_is_first_child(value tree, value id){
+  CAMLparam2(tree,id);
+  CAMLreturn(Val_bool (XMLTREE(tree)->IsFirstChild(TREENODEVAL(id))));
+}
 
 extern "C" CAMLprim value caml_xml_tree_first_child(value tree, value id){
   CAMLparam2(tree,id);
   CAMLreturn(Val_int (XMLTREE(tree)->FirstChild(TREENODEVAL(id))));
 }
 
+extern "C" CAMLprim value caml_xml_tree_tagged_child(value tree, value id, value tag){
+  CAMLparam3(tree,id,tag);
+  CAMLreturn(Val_int (XMLTREE(tree)->TaggedChild(TREENODEVAL(id),Int_val(tag))));
+}
+
+extern "C" CAMLprim value caml_xml_tree_tagged_sibling(value tree, value id, value tag){
+  CAMLparam3(tree,id,tag);
+  CAMLreturn(Val_int (XMLTREE(tree)->TaggedFollSibling(TREENODEVAL(id),Int_val(tag))));
+}
+
+
 extern "C" CAMLprim value caml_xml_tree_is_leaf(value tree, value id){
   CAMLparam2(tree,id);
   CAMLreturn(Val_bool (XMLTREE(tree)->IsLeaf(TREENODEVAL(id))));
@@ -274,9 +316,7 @@ extern "C" CAMLprim value caml_xml_tree_next_sibling(value tree, value id){
 
 extern "C" CAMLprim value caml_xml_tree_prev_text(value tree, value id){
   CAMLparam2(tree,id);
-  CAMLlocal1(res);
   CAMLreturn(Val_int((XMLTREE(tree)->PrevText(TREENODEVAL(id)))));
-  CAMLreturn(res);
 }
 extern "C" CAMLprim value caml_xml_tree_next_text(value tree, value id){
   CAMLparam2(tree,id);
@@ -369,14 +409,45 @@ extern "C" CAMLprim value caml_int_vector_alloc(value len){
 }
 
 extern "C" CAMLprim value caml_int_vector_set(value vec, value i, value v){
-  CAMLparam3(vec,i,v);
-  
+  CAMLparam3(vec,i,v);  
   ((int*) vec)[Int_val(i)+1] = Int_val(v);
   CAMLreturn (Val_unit);
 }
 
 
 #define VECT(x)  ((int*) (x))
+extern "C" CAMLprim value caml_xml_tree_select_desc(value tree, value node, value tags){
+  CAMLparam3(tree,node,tags);
+   
+  CAMLreturn (Val_int (XMLTREE(tree)->SelectDesc(TREENODEVAL(node),
+                                                 &(VECT(tags)[1]),
+                                                VECT(tags)[0])));
+}
+extern "C" CAMLprim value caml_xml_tree_select_child(value tree, value node, value tags){
+  CAMLparam3(tree,node,tags);
+   
+  CAMLreturn (Val_int (XMLTREE(tree)->SelectChild(TREENODEVAL(node),
+                                                 &(VECT(tags)[1]),
+                                                 VECT(tags)[0])));
+}
+extern "C" CAMLprim value caml_xml_tree_select_foll_sibling(value tree, value node, value tags){
+  CAMLparam3(tree,node,tags);
+  
+  CAMLreturn (Val_int (XMLTREE(tree)->SelectFollSibling(TREENODEVAL(node),
+                                                       &(VECT(tags)[1]),
+                                                       VECT(tags)[0])));
+}
+extern "C" CAMLprim value caml_xml_tree_select_foll_below(value tree, value node, value tags,value ctx){
+  CAMLparam4(tree,node,tags,ctx);
+  
+  CAMLreturn (Val_int (XMLTREE(tree)->SelectFollBelow(TREENODEVAL(node),
+                                                     &(VECT(tags)[1]),
+                                                     VECT(tags)[0],Int_val(ctx))));
+}
+
+
+                       
+/*
 extern "C" CAMLprim value caml_xml_tree_select_below(value tree, value node, value ctags, value dtags){
   CAMLparam4(tree,node,ctags,dtags);
    
@@ -386,8 +457,9 @@ extern "C" CAMLprim value caml_xml_tree_select_below(value tree, value node, val
                                                   VECT(ctags)[0],
                                                   &(VECT(dtags)[1]),
                                                   VECT(dtags)[0]))));                                     
-}
-
+                                                  }
+*/
+/*
 extern "C" CAMLprim value caml_xml_tree_select_next(value tree, value node, value ctags, value ftags,value root){
   CAMLparam5(tree,node,ctags,ftags,root);
   CAMLreturn (Val_int (
@@ -398,7 +470,8 @@ extern "C" CAMLprim value caml_xml_tree_select_next(value tree, value node, valu
                                                  VECT(ftags)[0],
                                                  TREENODEVAL(root)))));
 }
-
+*/
+/*
 extern "C" CAMLprim value caml_xml_tree_select_desc_only(value tree, value node,value dtags){
   CAMLparam3(tree,node,dtags);
    
@@ -425,7 +498,7 @@ extern "C" CAMLprim value caml_xml_tree_select_desc_or_foll_only(value tree, val
                                                  VECT(ftags)[0],
                                                  TREENODEVAL(root)))));
 }
-
+*/
 extern "C" CAMLprim value caml_xml_tree_doc_ids(value tree, value node){
   CAMLparam2(tree,node);
   CAMLlocal1(tuple);