Initial import of XMLTree
[SXSI/XMLTree.git] / libcds / tests / test_wvtree02.cpp
1
2 #include <sys/stat.h>
3 #include <iostream>
4 #include <sstream>
5 #include <basics.h>
6 #include <static_bitsequence.h>
7 #include <alphabet_mapper.h>
8 #include <static_sequence.h>
9
10 using namespace std;
11
12 int main(int argc, char ** argv) {
13   if(argc!=3) {
14     cout << "usage: " << argv[0] << " <file> <samp>" << endl;
15     return 0;
16   }
17   struct stat text_info;
18   if(stat(argv[1],&text_info)<0) {
19     cout << "could not stat: " << argv[1] << endl;
20     return -1;
21   }
22   
23   uint samp;
24   {
25     stringstream ss;
26     ss << string(argv[2]);
27     
28     ss >> samp;
29   }
30
31   uint n= (uint)text_info.st_size;
32   uint * text = new uint[n+1];
33   FILE * fp = fopen(argv[1],"r");
34   if(fp==NULL) {
35     cout << "could not open " << argv[1] << endl;
36     return -1;
37   }
38
39   cout << "File: " << argv[1] << endl;
40   cout << "Length: " << n << endl;
41
42   uint max_symbol = 0;
43   for(uint i=0;i<n;i++) {
44     uchar c;
45     uint read=fread(&c,sizeof(uchar),1,fp);
46     //assert(read==1);
47     text[i] = (uint)c;
48     c += read;
49     max_symbol = max(max_symbol,text[i]);
50   }
51   max_symbol++;
52
53   fclose(fp);
54
55   /*uint *occ = new uint[max_symbol];
56   for(uint i=0;i<max_symbol;i++)
57     occ[i] = 0;
58   for(uint i=0;i<n;i++)
59     occ[text[i]]++;*/
60
61   alphabet_mapper * am = new alphabet_mapper_none();
62   static_bitsequence_builder * bmb = new static_bitsequence_builder_rrr02(samp);
63         cout << "Building Huffman table..."; cout.flush();
64         wt_coder * wtc = new wt_coder_huff(text,n,am);
65         cout << "done" << endl; cout.flush();
66   static_sequence * wt = new static_sequence_wvtree(text,n,wtc,bmb,am,true);
67   delete bmb;
68
69   char * fname = new char[10+string(argv[1]).length()];
70   sprintf(fname,"%s.wt",argv[1]);
71   fp = fopen(fname,"w");
72   cout << "save: " << wt->save(fp) << endl;
73   fclose(fp);
74   delete wt;
75   fp = fopen(fname,"r");
76   wt = static_sequence::load(fp);
77   fclose(fp);
78   delete [] fname;
79   
80   ((static_sequence_wvtree*)wt)->test_structure(text,n);
81
82   cout << "WT Size: " << wt->size() << endl;
83   cout << "ft = " << 1.*wt->size()/n << endl;
84
85   fname = new char[10+string(argv[1]).length()];
86   sprintf(fname,"%s.wt",argv[1]);
87   fp = fopen(fname,"w");
88   cout << "save: " << wt->save(fp) << endl;
89   fclose(fp);
90   delete [] fname;
91
92   delete [] text;
93   delete wt;
94
95 }