New rank/select data structure based on sadakane's code
[SXSI/XMLTree.git] / libcds / tests / static_bitsequence_tester.cpp
1 /* static_bitsequence_tester.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * static_bitsequence_tester
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <iostream>
23 #include <sstream>
24 #include <basics.h>
25 #include <static_bitsequence.h>
26
27 using namespace std;
28
29 /* Time meassuring */
30 double ticks= (double)sysconf(_SC_CLK_TCK);
31 struct tms t1,t2;
32
33 void start_clock() {
34   times (&t1);
35 }
36
37
38 double stop_clock() {
39   times (&t2);
40   return (t2.tms_utime-t1.tms_utime)/ticks;
41 }
42 /* end Time meassuring */
43
44 uint NQUERIES=10000000;
45 uint SEED=47;
46
47 void load(char *fname, uint ** text, uint * n) {
48   FILE * fp = fopen(fname,"r");
49   if(fp==NULL) {
50     cout << "could not open " << fname << endl;
51     return;
52   }
53   if(fread(n,sizeof(uint),1,fp)!=1) {
54     cout << "Error reading file " << fname << endl;
55     return;
56   }
57   *text = new uint[uint_len(*n,1)];
58
59   if(fread(*text,sizeof(uint),uint_len(*n,1),fp)!=uint_len(*n,1)) {
60     cout << "Error reading file " << fname << endl;
61     return;
62   }
63 }
64
65 void test_bitsequence(uint * bitseq, uint len, static_bitsequence * bs) {
66   uint ones = 0;
67   bool error = false;
68   for(uint i=0;i<len && !error;i++) {
69     //cout << "i="<<i<< endl;
70     if(i>0) {
71       if(i%max(1,(bs->length()/100))==0) { cout << "."; cout.flush(); }
72       if(i%max(1,(bs->length()/10))==0) { cout << endl; cout.flush(); }
73     }
74     if(bitget(bitseq,i)) ones++;
75     if(bs->access(i) != (bitget(bitseq,i)!=0)) {
76       cout << "Access error for position " << i << endl;
77       cout << " got: " << bs->access(i) << " expected: " << (bitget(bitseq,i)!=0) << endl;
78       error = true;
79     }
80     if(bs->rank1(i) != ones) {
81       cout << "Rank1 error for position " << i << endl;
82       cout << " got: " << bs->rank1(i) << " expected: " << ones << endl;
83       error = true;
84     } 
85     if(bitget(bitseq,i) && bs->select1(ones) != i) {
86       cout << "Select1 error for position " << i << " ones:" << ones << endl;
87       cout << " got: " << bs->select1(ones) << " expected: " << i << endl;
88       error = true;
89     }
90     if(bs->rank0(i) != i+1-ones) {
91       cout << "Rank0 error for position " << i << endl;
92       cout << " got: " << bs->rank0(i) << " expected: " << ones << endl;
93       error = true;
94     }
95     if(!bitget(bitseq,i) && bs->select0(i+1-ones) != i) {
96       cout << "Select0 error for position " << i << endl;
97       cout << " got: " << bs->select0(i+1-ones) << " expected: " << i << endl;
98       error = true;
99     }
100   }
101         cout << "." << endl;
102 }
103
104 void speed_access(static_bitsequence * ss, uint * bitseq, uint n) {
105   uint acc=0;
106   srand(SEED);
107
108   start_clock();
109   for(uint i=0;i<NQUERIES;i++) {
110     uint pos = rand()%n;
111     acc += ss->access(pos);
112   }
113   double t = stop_clock();
114   cout << " * Time for " << NQUERIES << " accesses: " << t << " secs" << endl;
115   cout << " * Time per access: " << 1000*t/NQUERIES << " msecs" << endl;
116   cout << " - Check sum: " << acc << endl;
117 }
118
119 void speed_rank0(static_bitsequence * ss, uint * bitseq, uint n) {
120   uint acc=0;
121   srand(SEED);
122
123   start_clock();
124   for(uint i=0;i<NQUERIES;i++) {
125     uint pos = rand()%n;
126     acc += ss->rank0(pos);
127   }
128   double t = stop_clock();
129   cout << " * Time for " << NQUERIES << " rank0s: " << t << " secs" << endl;
130   cout << " * Time per rank0: " << 1000*t/NQUERIES << " msecs" << endl;
131   cout << " - Check sum: " << acc << endl;
132 }
133
134 void speed_rank1(static_bitsequence * ss, uint * bitseq, uint n) {
135   uint acc=0;
136   srand(SEED);
137
138   start_clock();
139   for(uint i=0;i<NQUERIES;i++) {
140     uint pos = rand()%n;
141     acc += ss->rank1(pos);
142   }
143   double t = stop_clock();
144   cout << " * Time for " << NQUERIES << " rank1s: " << t << " secs" << endl;
145   cout << " * Time per rank1: " << 1000*t/NQUERIES << " msecs" << endl;
146   cout << " - Check sum: " << acc << endl;
147 }
148
149 void speed_select0(static_bitsequence * ss, uint * bitseq, uint n) {
150   uint acc=0;
151   uint ones=ss->rank0(n-1);
152   srand(SEED);
153
154   start_clock();
155   for(uint i=0;i<NQUERIES;i++) {
156     uint pos = rand()%ones+1;
157     acc += ss->select0(pos);
158   }
159   double t = stop_clock();
160   cout << " * Time for " << NQUERIES << " select0s: " << t << " secs" << endl;
161   cout << " * Time per select0: " << 1000*t/NQUERIES << " msecs" << endl;
162   cout << " - Check sum: " << acc << endl;
163 }
164
165 void speed_select1(static_bitsequence * ss, uint * bitseq, uint n) {
166   uint acc=0;
167   uint ones=ss->rank1(n-1);
168   srand(SEED);
169
170   start_clock();
171   for(uint i=0;i<NQUERIES;i++) {
172     uint pos = rand()%ones+1;
173     acc += ss->select1(pos);
174   }
175   double t = stop_clock();
176   cout << " * Time for " << NQUERIES << " select1s: " << t << " secs" << endl;
177   cout << " * Time per select1: " << 1000*t/NQUERIES << " msecs" << endl;
178   cout << " - Check sum: " << acc << endl;
179 }