Skip to content

Commit

Permalink
Added tutorial examples
Browse files Browse the repository at this point in the history
  • Loading branch information
simongog committed Sep 12, 2013
1 parent 112aabf commit db4ca53
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 11 deletions.
26 changes: 15 additions & 11 deletions include/sdsl/wt_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,26 @@ struct _byte_tree {
// we can classify nodes as right child and left child with an easy criterion:
// node is a left child, if node%2==1
// node is a right child, if node%2==0
for (uint32_t c=0; c<fixed_sigma; ++c) {
for (uint32_t c=0, prev_c=0; c<fixed_sigma; ++c) {
if (m_c_to_leaf[c] != undef) { // if char exists in the alphabet
node_type v = m_c_to_leaf[c];
uint64_t w = 0; // path
uint64_t l = 0; // path len
uint64_t pw = 0; // path
uint64_t pl = 0; // path len
while (v != root()) { // while node is not the root
w <<= 1;
pw <<= 1;
if (m_nodes[m_nodes[v].parent].child[1] == v) // if the node is a right child
w |= 1ULL;
++l;
pw |= 1ULL;
++pl;
v = m_nodes[v].parent; // go up the tree
}
if (l > 56) {
if (pl > 56) {
throw std::logic_error("Code depth greater than 56!!!");
}
m_path[c] = w | (l << 56);
m_path[c] = pw | (pl << 56);
prev_c = c;
} else {
m_path[c] = 0;// i.e. len is 0, good for special case in rank
uint64_t pl = 0; // len is 0, good for special case in rank
m_path[c] = prev_c | (pl << 56);
}
}
}
Expand Down Expand Up @@ -407,7 +409,7 @@ struct _int_tree {
// we can classify nodes as right child and left child with an easy criterion:
// node is a left child, if node%2==1
// node is a right child, if node%2==0
for (value_type c=0; c < m_c_to_leaf.size(); ++c) {
for (value_type c=0, prev_c=0; c < m_c_to_leaf.size(); ++c) {
if (m_c_to_leaf[c] != undef) { // if char exists in the alphabet
node_type v = m_c_to_leaf[c];
uint64_t w = 0; // path
Expand All @@ -423,8 +425,10 @@ struct _int_tree {
throw std::logic_error("Code depth greater than 56!!!");
}
m_path[c] = w | (l << 56);
prev_c = c;
} else {
m_path[c] = 0;// i.e. len is 0, good for special case in rank
uint64_t pl = 0; // len is 0, good for special case in rank
m_path[c] = prev_c | (pl << 56);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tutorial/expl-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <sdsl/vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
int_vector<> v = {3,2,1,0,2,1,3,4,1,1,1,3,2,3};
v[1] = 0;
util::bit_compress(v);
cout << v << endl;
cout << size_in_bytes(v) << endl;
}
18 changes: 18 additions & 0 deletions tutorial/expl-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <sdsl/vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
int_vector<> v(10000000);
for (size_t i=0; i<10; ++i)
for (size_t j=0; j<1000000; ++j)
v[i*1000000+j] = j;
cout << size_in_mega_bytes(v) << endl;
util::bit_compress(v);
cout << size_in_mega_bytes(v) << endl;
enc_vector<> ev(v);
cout << size_in_mega_bytes(ev) << endl;
}
15 changes: 15 additions & 0 deletions tutorial/expl-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <sdsl/vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
int_vector<> v(10000000, 3);
v[0] = 1ULL<<63;
util::bit_compress(v);
cout << size_in_mega_bytes(v) << endl;
vlc_vector<> vv(v);
cout << size_in_mega_bytes(vv) << endl;
}
15 changes: 15 additions & 0 deletions tutorial/expl-04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = {1,1,0,1,0,0,1};
cout << b << endl;
b = bit_vector(80000000, 0);
for (size_t i=0; i < b.size(); i+=100)
b[i] = 1;
cout << size_in_mega_bytes(b) << endl;
}
17 changes: 17 additions & 0 deletions tutorial/expl-05.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = bit_vector(80000000, 0);
for (size_t i=0; i < b.size(); i+=100)
b[i] = 1;
cout << size_in_mega_bytes(b) << endl;
rrr_vector<63> rrrb(b);
cout << size_in_mega_bytes(rrrb) << endl;
sd_vector<> sdb(b);
cout << size_in_mega_bytes(sdb) << endl;
}
14 changes: 14 additions & 0 deletions tutorial/expl-06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = bit_vector(80000000, 0);
for (size_t i=0; i < b.size(); i+=100)
b[i] = 1;
sd_vector<> sdb(b);
write_structure<JSON_FORMAT>(sdb, cout);
}
16 changes: 16 additions & 0 deletions tutorial/expl-07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = bit_vector(8000, 0);
for (size_t i=0; i < b.size(); i+=100)
b[i] = 1;
rank_support_v<1> b_rank(&b);
for (size_t i=0; i<=b.size(); i+= b.size()/4)
cout << "(" << i << ", " << b_rank(i) << ") ";
cout << endl;
}
22 changes: 22 additions & 0 deletions tutorial/expl-08.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = bit_vector(8000, 0);
for (size_t i=0; i < b.size(); i+=100)
b[i] = 1;
sd_vector<> sdb(b);
sd_vector<>::rank_1_type sdb_rank(&sdb);
for (size_t i=0; i<=sdb.size(); i+= sdb.size()/4)
cout << "(" << i << ", " << sdb_rank(i) << ") ";
cout << endl;
rrr_vector<> rrrb(b);
rrr_vector<>::rank_1_type rrrb_rank(&rrrb);
for (size_t i=0; i<=rrrb.size(); i+= rrrb.size()/4)
cout << "(" << i << ", " << rrrb_rank(i) << ") ";
cout << endl;
}
17 changes: 17 additions & 0 deletions tutorial/expl-09.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = {0,1,0,1};
rank_support_v<1> b_r1(&b);
rank_support_v<0> b_r0(&b);
rank_support_v<10,2> b_r10(&b);
rank_support_v<01,2> b_r01(&b);
for (size_t i=0; i<=b.size(); ++i)
cout << i << ": "<< b_r1(i) << " " << b_r0(i)
<< " " << b_r10(i) << " " << b_r01(i) << endl;
}
17 changes: 17 additions & 0 deletions tutorial/expl-10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = {0,1,0,1,1,1,0,0,0,1,1};
size_t zeros = rank_support_v<0>(&b)(b.size());
bit_vector::select_0_type b_sel(&b);

for (size_t i=1; i <= zeros; ++i) {
cout << b_sel(i) << " ";
}
cout << endl;
}
16 changes: 16 additions & 0 deletions tutorial/expl-11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
bit_vector b = {0,1,0,1,1,1,0,0,0,1,1};
size_t cnt10 = rank_support_v<10,2>(&b)(b.size());
select_support_mcl<10,2> b_sel10(&b);

for (size_t i=1; i <= cnt10; ++i)
cout << b_sel10(i) << " ";
cout << endl;
}
21 changes: 21 additions & 0 deletions tutorial/expl-12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <sdsl/bit_vectors.hpp>
#include <sdsl/vectors.hpp>

using namespace std;
using namespace sdsl;

int main()
{
sd_vector<> sd_b = bit_vector {1,0,1,1,1,0,1,1,0,0,1,0,0,1};
size_t ones = sd_vector<>::rank_1_type(&sd_b)(sd_b.size());
sd_vector<>::select_1_type sdb_sel(&sd_b);

cout << sd_b << endl;

for (size_t i=1; i <= ones; ++i)
cout << sdb_sel(i) << " ";
cout << endl;
}


16 changes: 16 additions & 0 deletions tutorial/expl-13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <sdsl/wavelet_trees.hpp>
#include <iostream>

using namespace std;
using namespace sdsl;

int main()
{
wt_blcd<> wt;
construct(wt, "expl-13.cpp", 1);
for (size_t i=0; i < wt.size() and wt[i]!='\n'; ++i)
cout << wt[i];
cout << endl;
cout << "number of lines : " << wt.rank(wt.size(), '\n') << endl;
cout << "first '=' in line: " << wt.rank(wt.select(1, '='),'\n')+1 << endl;
}

0 comments on commit db4ca53

Please sign in to comment.