added a dummy print function to the lexindex class so that
[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 struct myclass {
14   xml_tree * tree;
15   bool operator() (int32_t i,int32_t j) { 
16   return (strcmp((const char*) tree->get_text(i),
17                  (const char*) tree->get_text(j))<0);}
18 } myobject;
19
20 //define a type for the lexicographic index
21 class lex_index {
22 public:
23   //The tag IDs
24   xml_tree::tag_t tag;
25   xml_tree::tag_t tag2;
26   //The text data
27   vector<int32_t> tagVector;           
28   vector<int32_t>::iterator tagVectorIt;
29   vector<int32_t> tag2Vector;           
30   vector<int32_t>::iterator tag2VectorIt;
31   void print();
32 };
33
34 void lex_index::print(){printf("Print called\n");}
35
36 // class prefix_treeNode {
37 // public:
38   //  std::map<char, prefix_treeNode> Children;
39 //  std::array<prefix_treeNode, 256> Children;
40 //  uint32 textID;
41 //};
42
43 using namespace SXSI;
44
45 static xml_tree*& XMLTREE(value v)
46 {
47   return Obj_val<xml_tree*>(v);
48 }
49
50 static lex_index*& LEXINDEX(value v)
51 {
52   return Obj_val<lex_index*>(v);
53 }
54
55 static xml_tree::node_t TREENODE(value i)
56 {
57   return static_cast<xml_tree::node_t>(Int_val(i));
58 }
59
60 static xml_tree::tag_t TAG(value i)
61 {
62   return static_cast<xml_tree::tag_t>(Int_val(i));
63 }
64
65 void preorderTraverse(xml_tree * tree, xml_tree::tag_t parent_tag, xml_tree::node_t node, lex_index* lindex){  
66   if (tree->tag(node)==tree->PCDATA_OPEN_TAG_ID)
67   if (parent_tag==lindex->tag) lindex->tagVectorIt = 
68                                lindex->tagVector.insert(lindex->tagVectorIt, tree->text_id(node));
69   else if (parent_tag==lindex->tag2) lindex->tag2VectorIt = 
70                                      lindex->tag2Vector.insert(lindex->tag2VectorIt, tree->text_id(node));
71   if (tree->tag(tree->first_child(node))!=0) preorderTraverse(tree,tree->tag(node),tree->first_child(node),lindex);
72   if (tree->tag(tree->next_sibling(node))!=0) preorderTraverse(tree,parent_tag,tree->next_sibling(node),lindex);
73 }
74
75 vector<int32_t> mergeJoin(xml_tree * tree, vector<int32_t> v1, vector<int32_t> v2){
76   vector<int32_t> v;
77   vector<int32_t>::iterator i=v.begin();
78   vector<int32_t>::iterator i1=v1.begin();
79   vector<int32_t>::iterator i2=v2.begin();
80   int k;
81
82   while((i1!=v1.end()) && (i2!=v2.end())){
83     k = strcmp((const char*) tree->get_text(*i1),
84                (const char*) tree->get_text(*i2));
85     if (k==0) 
86       {
87         i = v.insert( i, *i1 );
88         //advance left
89         i1++;
90         //advance right     
91         i2++;
92       }
93     else if (k<0) i1++; //advance left
94     else i2++; //advance right
95   }
96   return(v);
97 }
98
99 void printVector(const char * label, vector<int32_t> v){
100   vector<int32_t>::iterator i=v.begin();
101   if (i!=v.end()) {
102       printf("%s-vector: [%i", label, *i);
103       for (++i; i!=v.end(); ++i)
104         printf(",%i",  *i);
105       printf("]\n");
106     }
107 }
108
109 ML_BINDING value caml_build_lex_index(value vtree, value vtag, value vtag2)
110 {
111   CAMLparam2(vtree, vtag);
112   CAMLlocal1(vindex);
113   vindex = sxsi_alloc_custom<lex_index*>();
114   xml_tree * tree = XMLTREE(vtree);
115   myobject.tree = tree;
116
117   //allocate a lex index
118   lex_index* mylindex = new lex_index();
119
120   //take the tag parameter given by ocaml and convert
121   //it to a C++ tag and store it in the tag field of mylindex
122   if ((TAG(vtag)==-1) || (TAG(vtag2)==-1)) caml_failwith("<INVALID TAG>");
123   mylindex->tag = TAG(vtag);
124   mylindex->tag2 = TAG(vtag2);
125
126   //initialize iterators for the two vectors 
127   mylindex->tagVectorIt=mylindex->tagVector.begin();
128   mylindex->tag2VectorIt=mylindex->tag2Vector.begin();
129
130   preorderTraverse(tree, 0, tree->first_child(tree->ROOT), mylindex);
131   sort(mylindex->tagVector.begin(), mylindex->tagVector.end(), myobject); 
132   sort(mylindex->tag2Vector.begin(), mylindex->tag2Vector.end(), myobject); 
133   printVector(tree->get_tag_name_by_ref(mylindex->tag), mylindex->tagVector);
134   printVector(tree->get_tag_name_by_ref(mylindex->tag2), mylindex->tag2Vector);
135   printVector("Result" , mergeJoin(tree, mylindex->tagVector, mylindex->tag2Vector));
136
137   //Uncomment the following and comment the failwith line
138   //LEXINDEX(vindex) = ... return a lex_index* ....  
139   LEXINDEX(vindex)=mylindex;
140 }
141
142 ML_BINDING value caml_print_lex_index(value vindex)
143 {
144   CAMLparam1(vindex);
145   lex_index* index = LEXINDEX(vindex);
146
147   //Print the index to the terminal
148   //  caml_failwith("print_lex_index not implemented");
149   index->print();
150   CAMLreturn (Val_unit);
151 }