-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuccinct_Algorithm_Bit_Rank_Vector.cpp
More file actions
99 lines (66 loc) · 2.28 KB
/
Succinct_Algorithm_Bit_Rank_Vector.cpp
File metadata and controls
99 lines (66 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
#include <time.h>
#include <cmath>
#include <tuple>
using namespace std;
tuple<vector<int>, vector<int>, vector<vector<int>>> SuccinctAlgorithmBitRankVector(vector<bitset<1>> bit_array){
int length = bit_array.size();
int k = floor(log2(length)/2);
int l = k*k;
vector<int> first(int(length/l) + 2,0);
vector<int> second(int(length/k) + 2,0);
for(int i = 1; i <= length;i++){
first[ceil(double(i)/l)] += (bit_array[i] == bitset<1>(0)) ? 0 : 1;
second[ceil(double(i)/k)] += (bit_array[i] == bitset<1>(0)) ? 0 : 1;
if(i % k == 0){
second[int(i/k)] += second[(i/k) - 1];
}
if(i % l == 0){
first[int(i/l)] += first[(i/l) - 1];
second[int(i/k)] = 0;
}
}
vector<vector<int>> third;
for(int i = 0; i < pow(2,(k-1)); i++){
vector<int> pthird(k,0);
for(int j = 0; j < k-1; j++){
pthird[j+1] = pthird[j] + ((i >> (k - 2 - j)) & 1);
}
third.emplace_back(pthird);
}
return(make_tuple(first,second,third));
}
int main(){
vector<bitset<1>> bit_array = {0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,0,1,0,1,0,1,0,1,0,1};
clock_t start_c = clock();
tuple<vector<int>, vector<int>, vector<vector<int>>> tup = SuccinctAlgorithmBitRankVector(bit_array);
int length = bit_array.size();
int k = floor(log2(length)/2);
int l = k*k;
int id = 31;
if(id >= length){
cerr << "Id for bit rank is outofbound for the given vector. " << endl;
return 1;
}
if(id == 0){
cerr << "Id is 1-index based." << endl;
return 1;
}
int start = (id / k) * k + 1;
int end = ((id / k) + 1) * k;
string bit_substring = "";
for (int i = start; i < end; i++) {
bit_substring += bit_array[i].to_string();
}
int row_index = stoi(bit_substring, nullptr, 2);
int value = get<2>(tup)[row_index][id % k];
int rank = get<0>(tup)[int(id/l)] + get<1>(tup)[int(id/k)] + value;
clock_t end_c = clock();
double elapsed = double(end_c - start_c) / CLOCKS_PER_SEC;
cout << "Elapsed time: " << elapsed << " seconds." << std::endl;
cout << rank << endl;
return 0;
}