+++ /dev/null
-/**************************************
- * OCamlDriver.cpp
- * -------------------
- * An Ocaml Driver which calls the C++ methods and
- * adds a C wrapper interface with OCaml code.
- *
- * Author: Kim Nguyen
- * Date: 04/11/08
- */
-
-/***
- * Conventions:
- * functions never doing any allocation (non caml_alloc*, caml_copy_string,...)
- * have NOALLOC in the comment and their external declaration can have "noalloc"
- */
-
-
-#include <unordered_set>
-#include <algorithm>
-
-#include "XMLTree.h"
-#include "XMLTreeBuilder.h"
-#include "Utils.h"
-#include "common_stub.hpp"
-
-#define CAMLRAISEMSG(msg) (sxsi_raise_msg((char*) (msg)))
-
-#define XMLTREE(x) (Obj_val<XMLTree*>(x))
-
-#define HSET(x) (Obj_val<TagIdSet*>(x))
-
-#define XMLTREEBUILDER(x) (Obj_val<XMLTreeBuilder*>(x))
-
-
-#define TREENODEVAL(i) ((treeNode) (Int_val(i)))
-#define TAGVAL(i) ((TagType) (Int_val(i)))
-#define XMLTREE_ROOT 0
-#define NoAlloc
-
-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)));
-}
-
-
-/**
- * Interface to the TextCollection
- */
-
-/**
- * Utility functions
- */
-
-extern "C" value caml_text_collection_get_text(value tree, value id){
- CAMLparam2(tree,id);
- CAMLlocal1(str);
- uchar* txt = XMLTREE(tree)->GetText((DocID) Int_val(id));
- str = caml_copy_string((const char*)txt);
- CAMLreturn (str);
-}
-
-extern "C" value caml_text_collection_empty_text(value tree,value id){
- CAMLparam2(tree,id);
- CAMLreturn ( Val_int((XMLTREE(tree))->EmptyText((DocID) Int_val(id))));
-}
-
-bool docId_comp(DocID x, DocID y) { return x < y; }
-
-/**
- * Existential queries
- */
-
-extern "C" value caml_text_collection_is_prefix(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_bool((int) XMLTREE(tree)->IsPrefix(cstr)));
-}
-
-extern "C" value caml_text_collection_is_suffix(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_bool((int) XMLTREE(tree)->IsSuffix(cstr)));
-}
-extern "C" value caml_text_collection_is_equal(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_bool((int) XMLTREE(tree)->IsEqual(cstr)));
-}
-extern "C" value caml_text_collection_is_contains(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn ( Val_bool((int) XMLTREE(tree)->IsContains(cstr)));
-}
-
-extern "C" value caml_text_collection_is_lessthan(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn ( Val_bool((int) XMLTREE(tree)->IsLessThan(cstr)));
-}
-
-
-/**
- * Count Queries
- */
-
-/**
- * Global counting
- */
-extern "C" value caml_text_collection_count(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_int((XMLTREE(tree)->Count(cstr))));
-}
-
-extern "C" value caml_text_collection_count_prefix(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_int((XMLTREE(tree)->CountPrefix(cstr))));
-}
-
-extern "C" value caml_text_collection_count_suffix(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_int((XMLTREE(tree)->CountSuffix(cstr))));
-}
-
-extern "C" value caml_text_collection_count_equal(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_int((XMLTREE(tree)->CountEqual(cstr))));
-}
-
-extern "C" value caml_text_collection_count_contains(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_int((XMLTREE(tree)->CountContains(cstr))));
-}
-
-extern "C" value caml_text_collection_count_lessthan(value tree,value str){
- CAMLparam2(tree,str);
- uchar * cstr = (uchar *) String_val(str);
- CAMLreturn (Val_int((XMLTREE(tree)->CountLessThan(cstr))));
-}
-
-static value sort_alloc_array(std::vector<DocID> results, value resarray){
- std::sort(results.begin(), results.end(), docId_comp);
- size_t s = results.size();
- resarray = caml_alloc_tuple(s);
- for (size_t i = 0; i < s ;i++){
- caml_initialize(&Field(resarray,i),Val_int(results[i]));
- };
- return resarray;
-
-}
-
-/**
- * Full reporting queries
- */
-
-extern "C" value caml_text_collection_prefix(value tree,value str){
- CAMLparam2(tree,str);
- CAMLlocal1(resarray);
- uchar * cstr = (uchar *) String_val(str);
- std::vector<DocID> results = XMLTREE(tree)->Prefix(cstr);
- CAMLreturn (sort_alloc_array(results,resarray));
-}
-
-extern "C" value caml_text_collection_suffix(value tree,value str){
- CAMLparam2(tree,str);
- CAMLlocal1(resarray);
- uchar * cstr = (uchar *) String_val(str);
- std::vector<DocID> results = XMLTREE(tree)->Suffix(cstr);
- CAMLreturn (sort_alloc_array(results,resarray));
-}
-
-extern "C" value caml_text_collection_equals(value tree,value str){
- CAMLparam2(tree,str);
- CAMLlocal1(resarray);
- uchar * cstr = (uchar *) strdup(String_val(str));
- std::vector<DocID> results = XMLTREE(tree)->Equals(cstr);
- free(cstr);
- CAMLreturn (sort_alloc_array(results,resarray));
-}
-
-extern "C" value caml_text_collection_contains(value tree,value str){
- CAMLparam2(tree,str);
- CAMLlocal1(resarray);
- uchar * cstr = (uchar *) String_val(str);
- std::vector<DocID> results = XMLTREE(tree)->Contains(cstr);
- CAMLreturn (sort_alloc_array(results,resarray));
-}
-
-extern "C" value caml_text_collection_lessthan(value tree,value str){
- CAMLparam2(tree,str);
- CAMLlocal1(resarray);
- uchar * cstr = (uchar *) String_val(str);
- std::vector<DocID> results = XMLTREE(tree)->LessThan(cstr);
- CAMLreturn (sort_alloc_array(results,resarray));
-}
-
-
-////////////////////// 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()));
-}
--- /dev/null
+#include <cstdlib>
+extern "C" {
+#include <unistd.h>
+}
+#include <bp/bp.h>
+#include "common_stub.hpp"
+
+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)
+ sxsi_raise_msg("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)
+ sxsi_raise_msg("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)
+ sxsi_raise_msg("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)
+ sxsi_raise_msg("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()));
+}