forked from simongog/sdsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add memory mapped int_vector and simplify int_vec
This commit introduces a non-const data() member which allows raw write access to the data stored in an int_vector similar to what is possible with the standard std::vector. this allows "unfriending" several of the util:: helper functions that modify int_vectors. The second addition to the library is a memory mapped int_vector (int_vector_mapper) which provides the same functionality as a regular int_vector but is memory mapped from a file. Thus, operations such as util::bit_compress can now be performed without loading the int_vector to memory. The int_vector_mapper is soley used as a resource handle to the data stored in the file. All operations are forwarded to the int_vector implementation. Thus, unlike the int_vector_buffer, the mapper can be used in regular stl algorithms as it provides const and non const access similar to the regular int_vector. The mapper additionally supports the push_back and resize operations which can be used to write data to disk. Temporary storage on disk can be realized using the temp_file_buffer class which creates a int_vector_mapper object from a temporary file which is deleted after the int_vector_mapper object is destroyed.
- Loading branch information
Showing
8 changed files
with
692 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <sdsl/int_vector_mapper.hpp> | ||
#include <string> | ||
#include <iostream> | ||
|
||
using namespace sdsl; | ||
using namespace std; | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
if (argc < 1) { | ||
cout << "Usage: " << argv[0] << endl; | ||
cout << "(1) Writes an int_vector sequentially to a file" << endl; | ||
cout << "(2) Streams the content from file" << endl; | ||
cout << "(3) Remove the file" << endl; | ||
return 1; | ||
} | ||
string tmp_file = "tmp_file.sdsl"; | ||
size_t size = 10000000; | ||
std::mt19937_64 rng(13); | ||
uint8_t width = 0; | ||
int_vector<> iv(size,0,64); | ||
int_vector<64> ivf(size,0); | ||
std::vector<uint64_t> stdv(size,0); | ||
|
||
// (1) write an int vector to disk | ||
{ | ||
// write sequentially random values to disk | ||
for (uint64_t i=0; i<size; ++i) { | ||
iv[i] = rng(); | ||
stdv[i] = iv[i]; | ||
ivf[i] = iv[i]; | ||
} | ||
|
||
util::bit_compress(iv); | ||
width = iv.width(); | ||
store_to_file(iv,tmp_file); | ||
} | ||
|
||
// (2) memory map the content of tmp_file | ||
{ | ||
int_vector_mapper<> ivm(tmp_file); | ||
if (ivm.size() != size) { | ||
std::cerr << "ERROR: ivm.size()="<< ivm.size() << " != " << size << std::endl; | ||
return 1; | ||
} | ||
if (ivm.width() != width) { | ||
std::cerr << "ERROR: ivm.width()="<< ivm.width() << " != " << width << std::endl; | ||
return 1; | ||
} | ||
rng.seed(13); // To get the same values than before use the same seed | ||
for (uint64_t i=0; i<ivm.size(); ++i) { | ||
uint64_t expected_value = rng(); | ||
if (ivm[i] != expected_value) { | ||
std::cerr << "ERROR: ivm["<< i << "]=" << ivm[i] << " != " << expected_value << "= expected_value" << std::endl; | ||
return 1; | ||
} | ||
} | ||
|
||
if(ivm != stdv) { | ||
std::cerr << "ERROR: std::vector CMP failed."; | ||
} | ||
if(ivm != iv) { | ||
std::cerr << "ERROR: iv CMP failed."; | ||
} | ||
if(ivm != ivf) { | ||
std::cerr << "ERROR: ivf CMP failed."; | ||
} | ||
} | ||
|
||
// (3) remove the file as the mapper does not do that | ||
{ | ||
sdsl::remove(tmp_file); | ||
} | ||
|
||
{ | ||
auto tmp_buf = temp_file_buffer<64>::create(); | ||
for(const auto& val : stdv) { | ||
tmp_buf.push_back(val); | ||
} | ||
if(tmp_buf != stdv) { | ||
std::cerr << "ERROR: tmp_buf CMP failed." << std::endl; | ||
} | ||
|
||
// tmp buf file is deleted automatically | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.