ab595aec2c92096c017a8ac12a0a8f5ed5433130
[SXSI/xpathcomp.git] / src / lexindex_stub.cpp
1 #include <cstring>
2 #include <utility>
3 #include <algorithm>
4 #include <vector>
5 #include <array>
6 #include "xml-tree.hpp"
7 #include "utils_stub.hpp"
8 #include <iostream>
9
10 // #include <algorithm>
11 using namespace std;
12
13 vector<int32_t> myvector;           
14 vector<int32_t>::iterator it=myvector.begin();
15 vector<int32_t> myvector2;           
16 vector<int32_t>::iterator it2=myvector2.begin();
17 xml_tree * tree;
18 xml_tree::tag_t tag;
19 xml_tree::tag_t tag2;
20
21 //define a type for the lexicographic index
22
23 class lex_index {
24 public:
25   //The tag ID
26   xml_tree::tag_t tag;
27   //The text data
28   std::vector<std::pair<std::string, xml_tree::node_t> > data;
29 };
30
31 // class prefix_treeNode {
32 // public:
33   //  std::map<char, prefix_treeNode> Children;
34 //  std::array<prefix_treeNode, 256> Children;
35 //  uint32 textID;
36 //};
37
38 using namespace SXSI;
39
40
41 static xml_tree*& XMLTREE(value v)
42 {
43   return Obj_val<xml_tree*>(v);
44 }
45
46 static lex_index*& LEXINDEX(value v)
47 {
48   return Obj_val<lex_index*>(v);
49 }
50
51 static xml_tree::node_t TREENODE(value i)
52 {
53   return static_cast<xml_tree::node_t>(Int_val(i));
54 }
55
56 static xml_tree::tag_t TAG(value i)
57 {
58   return static_cast<xml_tree::tag_t>(Int_val(i));
59 }
60
61 void preorderTraverse(xml_tree::tag_t parent_tag, xml_tree::node_t node){  
62   if (tree->tag(node)==tree->PCDATA_OPEN_TAG_ID)
63   if (parent_tag==tag) it = myvector.insert(it, tree->text_id(node));
64   else if (parent_tag==tag2) it2 = myvector2.insert(it2, tree->text_id(node));
65   if (tree->tag(tree->first_child(node))!=0) preorderTraverse(tree->tag(node), tree->first_child(node));
66   if (tree->tag(tree->next_sibling(node))!=0) preorderTraverse(parent_tag, tree->next_sibling(node));
67 }
68
69 bool myfunction (int32_t i,int32_t j) { 
70   return (strcmp((const char*) tree->get_text(i),
71                  (const char*) tree->get_text(j))<0);
72 }
73
74 vector<int32_t> mergeJoin(vector<int32_t> v1, vector<int32_t> v2){
75   vector<int32_t> v;
76   vector<int32_t>::iterator i=v.begin();
77   vector<int32_t>::iterator i1=v1.begin();
78   vector<int32_t>::iterator i2=v2.begin();
79   int k;
80
81   while((i1!=v1.end()) && (i2!=v2.end())){
82     k = strcmp((const char*) tree->get_text(*i1),
83                (const char*) tree->get_text(*i2));
84     if (k==0) 
85       {
86         i = v.insert( i, *i1 );
87         //advance left
88         i1++;
89         //advance right     
90         i2++;
91       }
92     else if (k<0) i1++; //advance left
93     else i2++; //advance right
94   }
95   return(v);
96 }
97
98 void printIndex(const char * label, vector<int32_t> v){
99   vector<int32_t>::iterator i=v.begin();
100   if (i!=v.end()) {
101       printf("%s-vector: [%i", label, *i);
102       for (++i; i!=v.end(); ++i)
103         printf(",%i",  *i);
104       printf("]\n");
105     }
106 }
107
108 extern "C" value caml_build_lex_index(value vtree, value vtag, value vtag2)
109 {
110   CAMLparam2(vtree, vtag);
111   CAMLlocal1(vindex);
112   const char * s;
113   vindex = sxsi_alloc_custom<lex_index*>();
114   tree = XMLTREE(vtree);
115   tag = TAG(vtag);
116   tag2 = TAG(vtag2);
117   //Uncomment the following and comment the failwith line
118   //LEXINDEX(vindex) = ... return a lex_index* ....
119
120   if ((tag==-1) || (tag2==-1)) caml_failwith("<INVALID TAG>");
121   preorderTraverse(0, tree->first_child(tree->ROOT));
122   sort(myvector.begin(), myvector.end(), myfunction); 
123   sort(myvector2.begin(), myvector2.end(), myfunction); 
124   printIndex(tree->get_tag_name_by_ref(tag), myvector);
125   printIndex(tree->get_tag_name_by_ref(tag2), myvector2);
126   printIndex("Result" , mergeJoin(myvector, myvector2));
127 }
128
129 extern "C" value caml_print_lex_index(value vindex)
130 {
131   CAMLparam1(vindex);
132   lex_index* index = LEXINDEX(vindex);
133
134   //Print the index to the terminal
135   caml_failwith("print_lex_index not implemented");
136   CAMLreturn (Val_unit);
137 }