Optimize sdarray.cpp to use g++ builtin instead of doing naive counting.
[SXSI/libcds.git] / 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 void test_static_sequence(uint * symbols, uint n, static_sequence * ss) {
13   cout << "Size: " << ss->size() << endl;
14   uint max_v=0;
15   for(uint i=0;i<n;i++) 
16     max_v = max(max_v,symbols[i]);
17   uint * occ = new uint[max_v+1];
18   for(uint i=0;i<=max_v;i++)
19     occ[i] = 0;
20   bool error = false;
21   for(uint i=0;i<n && !error;i++) {
22     if(i!=0 && i%max(1,(n-1)/100)==0) { cout << "."; cout.flush(); }
23     if(i!=0 && i%max(1,(n-1)/10)==0) cout << endl;
24     occ[symbols[i]]++;
25     uint a = ss->access(i);
26     uint r = ss->rank(symbols[i],i);
27     uint s = ss->select(symbols[i],occ[symbols[i]]);
28     uint rM1 = (i==0)?0:ss->rank(symbols[i],i-1);
29     if(r!=occ[symbols[i]]) {
30       cout << "Error in rank for symbol " << symbols[i] << " at position " << i << endl;
31       cout << "value: " << r << endl;
32       cout << "Expected: " << occ[symbols[i]] << endl;
33       error = true;
34     }
35     if(s!=i) {
36       cout << "Error in select for symbol " << symbols[i] << " at position " << occ[symbols[i]] << endl;
37       cout << "value: " << s << endl;
38       cout << "Expected: " << i << endl;
39       error = true;
40     }
41     if(a!=symbols[i]) {
42       cout << "Error in access at position " << i << endl;
43       cout << "value: " << a << endl;
44       cout << "Expected: " << symbols[i] << endl;
45       error = true;
46     }
47     if(rM1!=occ[symbols[i]]-1) {
48       cout << "Error in rankM1 for symbol " << symbols[i] << " at position " << i-1 << endl;
49       cout << "value: " << rM1 << endl;
50       cout << "Expected: " << occ[symbols[i]]-1 << endl;
51       error = true;
52     }
53   }
54   if(!error)
55     cout << "Test OK! It works :)" << endl;
56   delete [] occ;  
57 }
58
59 int main(int argc, char ** argv) {
60   if(argc!=3) {
61     cout << "usage: " << argv[0] << " <file> <samp>" << endl;
62     return 0;
63   }
64   struct stat text_info;
65   if(stat(argv[1],&text_info)<0) {
66     cout << "could not stat: " << argv[1] << endl;
67     return -1;
68   }
69   
70   stringstream ss;
71   ss << argv[2];
72   uint samp;
73   ss >> samp;
74
75   uint n= (uint)text_info.st_size/4;
76   uint * text = new uint[n+1];
77   FILE * fp = fopen(argv[1],"r");
78   if(fp==NULL) {
79     cout << "could not open " << argv[1] << endl;
80     return -1;
81   }
82
83   cout << "File: " << argv[1] << endl;
84   cout << "Length: " << n << endl;
85
86   uint max_symbol = 0;
87   for(uint i=0;i<n;i++) {
88     uint c;
89     uint read=fread(&c,sizeof(uint),1,fp);
90     //assert(read==1);
91     text[i] = (uint)c;
92     c += read;
93     max_symbol = max(max_symbol,text[i]);
94   }
95   max_symbol++;
96
97   fclose(fp);
98
99   /*uint *occ = new uint[max_symbol];
100   for(uint i=0;i<max_symbol;i++)
101     occ[i] = 0;
102   for(uint i=0;i<n;i++)
103     occ[text[i]]++;*/
104
105   alphabet_mapper * am = new alphabet_mapper_none();
106   static_bitsequence_builder * bmb = new static_bitsequence_builder_rrr02(samp);
107         cout << "Building Huffman table..."; cout.flush();
108         wt_coder * wtc = new wt_coder_huff(text,n,am);
109         cout << "done" << endl; cout.flush();
110   cout << "Building static_sequence..."; cout.flush();
111   static_sequence * wt = new static_sequence_wvtree(text,n,wtc,bmb,am);
112   cout << "done" << endl; cout.flush();
113   delete bmb;
114   
115   char * fname = new char[10+string(argv[1]).length()];
116   sprintf(fname,"%s.wt",argv[1]);
117   fp = fopen(fname,"w");
118   wt->save(fp);
119   fclose(fp);
120   delete wt;
121   fp = fopen(fname,"r");
122   wt = static_sequence::load(fp);
123   fclose(fp);
124   delete [] fname;
125   
126   test_static_sequence(text,n,wt);
127   
128   cout << "WT Size: " << wt->size() << endl;
129   cout << "ft = " << 1.*wt->size()/(bits(max_symbol-1)*n/8) << endl;
130
131   delete [] text;
132   delete wt;
133
134 }