Initial import of XMLTree
[SXSI/XMLTree.git] / libcds / tests / test_wvtree01.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   stringstream ss;
24   ss << argv[2];
25   uint samp;
26   ss >> samp;
27
28   uint n= (uint)text_info.st_size/4;
29   uint * text = new uint[n+1];
30   FILE * fp = fopen(argv[1],"r");
31   if(fp==NULL) {
32     cout << "could not open " << argv[1] << endl;
33     return -1;
34   }
35
36   cout << "File: " << argv[1] << endl;
37   cout << "Length: " << n << endl;
38
39   uint max_symbol = 0;
40   for(uint i=0;i<n;i++) {
41     uint c;
42     uint read=fread(&c,sizeof(uint),1,fp);
43     //assert(read==1);
44     text[i] = (uint)c;
45     c += read;
46     max_symbol = max(max_symbol,text[i]);
47   }
48   max_symbol++;
49
50   fclose(fp);
51
52   /*uint *occ = new uint[max_symbol];
53   for(uint i=0;i<max_symbol;i++)
54     occ[i] = 0;
55   for(uint i=0;i<n;i++)
56     occ[text[i]]++;*/
57
58   alphabet_mapper * am = new alphabet_mapper_none();
59   static_bitsequence_builder * bmb = new static_bitsequence_builder_rrr02(samp);
60         cout << "Building Huffman table..."; cout.flush();
61         wt_coder * wtc = new wt_coder_huff(text,n,am);
62         cout << "done" << endl; cout.flush();
63   static_sequence * wt = new static_sequence_wvtree(text,n,wtc,bmb,am,true);
64   delete bmb;
65   
66   char * fname = new char[10+string(argv[1]).length()];
67   sprintf(fname,"%s.wt",argv[1]);
68   fp = fopen(fname,"w");
69   wt->save(fp);
70   fclose(fp);
71   delete wt;
72   fp = fopen(fname,"r");
73   wt = static_sequence::load(fp);
74   fclose(fp);
75   delete [] fname;
76   
77
78   cout << "WT Size: " << wt->size() << endl;
79   cout << "ft = " << 1.*wt->size()/(bits(max_symbol-1)*n/8) << endl;
80
81   delete [] text;
82   delete wt;
83
84 }