6 #include "xml-tree.hpp"
7 #include "utils_stub.hpp"
10 // #include <algorithm>
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();
21 //define a type for the lexicographic index
28 std::vector<std::pair<std::string, xml_tree::node_t> > data;
31 // class prefix_treeNode {
33 // std::map<char, prefix_treeNode> Children;
34 // std::array<prefix_treeNode, 256> Children;
41 static xml_tree*& XMLTREE(value v)
43 return Obj_val<xml_tree*>(v);
46 static lex_index*& LEXINDEX(value v)
48 return Obj_val<lex_index*>(v);
51 static xml_tree::node_t TREENODE(value i)
53 return static_cast<xml_tree::node_t>(Int_val(i));
56 static xml_tree::tag_t TAG(value i)
58 return static_cast<xml_tree::tag_t>(Int_val(i));
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));
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);
74 vector<int32_t> mergeJoin(vector<int32_t> v1, vector<int32_t> v2){
76 vector<int32_t>::iterator i=v.begin();
77 vector<int32_t>::iterator i1=v1.begin();
78 vector<int32_t>::iterator i2=v2.begin();
81 while((i1!=v1.end()) && (i2!=v2.end())){
82 k = strcmp((const char*) tree->get_text(*i1),
83 (const char*) tree->get_text(*i2));
86 i = v.insert( i, *i1 );
92 else if (k<0) i1++; //advance left
93 else i2++; //advance right
98 void printIndex(const char * label, vector<int32_t> v){
99 vector<int32_t>::iterator i=v.begin();
101 printf("%s-vector: [%i", label, *i);
102 for (++i; i!=v.end(); ++i)
108 extern "C" value caml_build_lex_index(value vtree, value vtag, value vtag2)
110 CAMLparam2(vtree, vtag);
113 vindex = sxsi_alloc_custom<lex_index*>();
114 tree = XMLTREE(vtree);
118 //Uncomment the following and comment the failwith line
119 //LEXINDEX(vindex) = ... return a lex_index* ....
121 if ((tag==-1) || (tag2==-1)) caml_failwith("<INVALID TAG>");
123 preorderTraverse(0, tree->first_child(tree->ROOT));
124 sort(myvector.begin(), myvector.end(), myfunction);
125 sort(myvector2.begin(), myvector2.end(), myfunction);
126 printIndex(tree->get_tag_name_by_ref(tag), myvector);
127 printIndex(tree->get_tag_name_by_ref(tag2), myvector2);
128 printIndex("Result" , mergeJoin(myvector, myvector2));
131 extern "C" value caml_print_lex_index(value vindex)
134 lex_index* index = LEXINDEX(vindex);
136 //Print the index to the terminal
138 caml_failwith("print_lex_index not implemented");
140 CAMLreturn (Val_unit);