Skip to content

Commit

Permalink
fix issue simongog#202 and introduce more type safety
Browse files Browse the repository at this point in the history
This fixes issue simongog#202 where I added stuff to the invector before
updating the size.

Additionally, this commit also adds additional type safety features to
the int-vector-mapper. The mapper now has an additional template
parameter:

`
template <t_width,t_mode> class int_vector_mapper
`

where the ``open mode'' can be specified. For example,

`
const int_vector_mapper<0,std::ios_base::in> ivm(tmp_file);
`

maps a file in read only mode. thus, only operations which do not modify
the underlying file are permitted. Using other operations causes a
compilation error.

The default mode remains read+write so existing code is not affected:

`
int_vector_mapper<0,std::ios_base::out|std::ios_base::in> ivm(tmp_file);
`

is equal to

`
int_vector_mapper<> ivm(tmp_file);
`

the examples and tests are adjusted accordingly.
  • Loading branch information
mpetri committed Oct 9, 2014
1 parent ed8c99f commit c675183
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 289 deletions.
63 changes: 47 additions & 16 deletions examples/int-vector-mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ int main(int argc, char* argv[])
store_to_file(iv,tmp_file);
}

// (2) memory map the content of tmp_file
// (2) open readonly! memory map the content of tmp_file
{
int_vector_mapper<> ivm(tmp_file);
const int_vector_mapper<0,std::ios_base::in> ivm(tmp_file);
if (ivm.size() != size) {
std::cerr << "ERROR: ivm.size()="<< ivm.size() << " != " << size << std::endl;
return 1;
Expand All @@ -56,29 +56,60 @@ int main(int argc, char* argv[])
}
}

if(ivm != stdv) {
std::cerr << "ERROR: std::vector CMP failed.";
if (ivm != stdv) {
std::cerr << "ERROR: std::vector CMP failed.";
}
if(ivm != iv) {
std::cerr << "ERROR: iv CMP failed.";
if (ivm != iv) {
std::cerr << "ERROR: iv CMP failed.";
}
if(ivm != ivf) {
std::cerr << "ERROR: ivf CMP failed.";
if (ivm != ivf) {
std::cerr << "ERROR: ivf CMP failed.";
}
}

// (3) remove the file as the mapper does not do that
// (2) open read+write! memory map the content of tmp_file
{
sdsl::remove(tmp_file);
int_vector_mapper<0> 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 if we don't specify it
{
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;
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
Expand Down
26 changes: 13 additions & 13 deletions include/sdsl/int_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include <initializer_list>
#include <type_traits>
#include <vector>

#include <ios>

//! Namespace for the succinct data structure library.
namespace sdsl
Expand Down Expand Up @@ -86,7 +86,7 @@ class int_vector_iterator;
template<class t_int_vector>
class int_vector_const_iterator;

template<uint8_t t_width>
template<uint8_t t_width,std::ios_base::openmode t_mode>
class int_vector_mapper;

template<uint8_t b, uint8_t t_patter_len> // forward declaration
Expand Down Expand Up @@ -273,11 +273,11 @@ class int_vector
friend class int_vector_iterator_base<int_vector>;
friend class int_vector_iterator<int_vector>;
friend class int_vector_const_iterator<int_vector>;
friend class int_vector_mapper<t_width>;
template<uint8_t,std::ios_base::openmode> friend class int_vector_mapper;
friend class coder::elias_delta;
friend class coder::elias_gamma;
friend class coder::fibonacci;
template<uint8_t> friend class coder::comma;
template<uint8_t> friend class coder::comma;
friend class memory_manager;

enum { fixed_int_width = t_width }; // make template parameter accessible
Expand All @@ -304,7 +304,7 @@ class int_vector
int_vector(std::initializer_list<t_T> il) : int_vector() {
resize(il.size());
size_type idx = 0;
for (auto x : il) {
for (auto x : il) {
(*this)[idx++] = x;
}
}
Expand Down Expand Up @@ -1113,7 +1113,7 @@ template<class t_bv>
inline typename std::enable_if<std::is_same<typename t_bv::index_category ,bv_tag>::value, std::ostream&>::type
operator<<(std::ostream& os, const t_bv& bv)
{
for (auto b : bv) {
for (auto b : bv) {
os << b;
}
return os;
Expand Down Expand Up @@ -1206,14 +1206,14 @@ template<uint8_t t_width>
auto int_vector<t_width>::get_int(size_type idx, const uint8_t len)const -> value_type
{
#ifdef SDSL_DEBUG
if (idx+len > m_size) {
throw std::out_of_range("OUT_OF_RANGE_ERROR: int_vector::get_int(size_type, uint8_t); idx+len > size()!");
}
if (len > 64) {
throw std::out_of_range("OUT_OF_RANGE_ERROR: int_vector::get_int(size_type, uint8_t); len>64!");
}
if (idx+len > m_size) {
throw std::out_of_range("OUT_OF_RANGE_ERROR: int_vector::get_int(size_type, uint8_t); idx+len > size()!");
}
if (len > 64) {
throw std::out_of_range("OUT_OF_RANGE_ERROR: int_vector::get_int(size_type, uint8_t); len>64!");
}
#endif
return bits::read_int(m_data+(idx>>6), idx&0x3F, len);
return bits::read_int(m_data+(idx>>6), idx&0x3F, len);
}

template<uint8_t t_width>
Expand Down
Loading

0 comments on commit c675183

Please sign in to comment.