From eda66f2aeabe9d951ea37fbd4f8696eae53cac78 Mon Sep 17 00:00:00 2001 From: Simon Gog Date: Wed, 7 Sep 2016 15:07:00 +0200 Subject: [PATCH 1/4] Fix issue #339 --- extras/pre-commit | 2 +- include/sdsl/int_vector.hpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/extras/pre-commit b/extras/pre-commit index 9023a3560..f3c3676e6 100644 --- a/extras/pre-commit +++ b/extras/pre-commit @@ -43,7 +43,7 @@ then fi version=`astyle --version 2> /dev/null` -if test "x$version" != "x"; then +if test "x${version}" != "x"; then echo "git pre-receive hook:" echo "Did not find astyle, please install it before continuing." exit 1 diff --git a/include/sdsl/int_vector.hpp b/include/sdsl/int_vector.hpp index c6d411454..cbc0d3d53 100644 --- a/include/sdsl/int_vector.hpp +++ b/include/sdsl/int_vector.hpp @@ -474,6 +474,9 @@ class int_vector // Write data (without header) to a stream. size_type write_data(std::ostream& out) const; + // Write raw data (without header) to a stream. + size_type write_raw_data(std::ostream& out) const; + //! Serializes the int_vector to a stream. /*! \return The number of bytes written to out. * \sa load @@ -601,6 +604,7 @@ class int_vector struct raw_wrapper { + typedef int_vector::size_type size_type; const int_vector& vec; raw_wrapper() = delete; raw_wrapper(const int_vector& _vec) : vec(_vec) {} @@ -609,7 +613,7 @@ class int_vector serialize(std::ostream& out, structure_tree_node* v=nullptr, std::string name="")const { structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this)); - auto written_bytes = vec.write_data(out); + auto written_bytes = vec.write_raw_data(out); structure_tree::add_size(child, written_bytes); return written_bytes; } @@ -1520,6 +1524,24 @@ typename int_vector::size_type int_vector::write_data(std::ost return written_bytes; } +template +typename int_vector::size_type int_vector::write_raw_data(std::ostream& out) const +{ + typedef typename int_vector::value_type value_type; + size_type written_bytes = 0; + uint64_t* p = m_data; + size_type idx = 0; + while (idx+conf::SDSL_BLOCK_SIZE < size()/sizeof(value_type) ) { + out.write((char*) p, conf::SDSL_BLOCK_SIZE*sizeof(value_type)); + written_bytes += conf::SDSL_BLOCK_SIZE*sizeof(value_type); + p += conf::SDSL_BLOCK_SIZE; + idx += conf::SDSL_BLOCK_SIZE; + } + out.write((char*) p, ((size()/sizeof(value_type))-idx)*sizeof(value_type)); + written_bytes += ((size()/sizeof(value_type))-idx)*sizeof(value_type); + return written_bytes; +} + template typename int_vector::size_type int_vector::serialize(std::ostream& out, structure_tree_node* v, From 9d47f9de3d342e82edb4c155c289a0bd8b7fe457 Mon Sep 17 00:00:00 2001 From: Simon Gog Date: Wed, 7 Sep 2016 15:09:04 +0200 Subject: [PATCH 2/4] Revert changes in pre-commit --- extras/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/pre-commit b/extras/pre-commit index f3c3676e6..9023a3560 100644 --- a/extras/pre-commit +++ b/extras/pre-commit @@ -43,7 +43,7 @@ then fi version=`astyle --version 2> /dev/null` -if test "x${version}" != "x"; then +if test "x$version" != "x"; then echo "git pre-receive hook:" echo "Did not find astyle, please install it before continuing." exit 1 From f035f588711f1762b8ac487eeaec4f71cbed0b96 Mon Sep 17 00:00:00 2001 From: Simon Gog Date: Tue, 13 Sep 2016 10:29:12 +0200 Subject: [PATCH 3/4] Code housekeeping --- include/sdsl/int_vector.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/sdsl/int_vector.hpp b/include/sdsl/int_vector.hpp index cbc0d3d53..cd478e395 100644 --- a/include/sdsl/int_vector.hpp +++ b/include/sdsl/int_vector.hpp @@ -1531,14 +1531,15 @@ typename int_vector::size_type int_vector::write_raw_data(std: size_type written_bytes = 0; uint64_t* p = m_data; size_type idx = 0; - while (idx+conf::SDSL_BLOCK_SIZE < size()/sizeof(value_type) ) { + const uint64_t elements = size()/sizeof(value_type); + while (idx+conf::SDSL_BLOCK_SIZE < elements ) { out.write((char*) p, conf::SDSL_BLOCK_SIZE*sizeof(value_type)); written_bytes += conf::SDSL_BLOCK_SIZE*sizeof(value_type); p += conf::SDSL_BLOCK_SIZE; idx += conf::SDSL_BLOCK_SIZE; } - out.write((char*) p, ((size()/sizeof(value_type))-idx)*sizeof(value_type)); - written_bytes += ((size()/sizeof(value_type))-idx)*sizeof(value_type); + out.write((char*) p, (elements-idx)*sizeof(value_type)); + written_bytes += (elements-idx)*sizeof(value_type); return written_bytes; } From b4377d6778ee4b94d3b148b10372b1195a81f3e0 Mon Sep 17 00:00:00 2001 From: Simon Gog Date: Tue, 13 Sep 2016 13:48:51 +0200 Subject: [PATCH 4/4] Fixed bug --- include/sdsl/int_vector.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/sdsl/int_vector.hpp b/include/sdsl/int_vector.hpp index cd478e395..420adaa13 100644 --- a/include/sdsl/int_vector.hpp +++ b/include/sdsl/int_vector.hpp @@ -1530,16 +1530,16 @@ typename int_vector::size_type int_vector::write_raw_data(std: typedef typename int_vector::value_type value_type; size_type written_bytes = 0; uint64_t* p = m_data; - size_type idx = 0; - const uint64_t elements = size()/sizeof(value_type); - while (idx+conf::SDSL_BLOCK_SIZE < elements ) { - out.write((char*) p, conf::SDSL_BLOCK_SIZE*sizeof(value_type)); - written_bytes += conf::SDSL_BLOCK_SIZE*sizeof(value_type); + size_type idx = 0; // uin64_t index + while (idx+conf::SDSL_BLOCK_SIZE < ((width()*size())>>6)) { + out.write((char*) p, conf::SDSL_BLOCK_SIZE*sizeof(uint64_t)); + written_bytes += conf::SDSL_BLOCK_SIZE*sizeof(uint64_t); p += conf::SDSL_BLOCK_SIZE; idx += conf::SDSL_BLOCK_SIZE; } - out.write((char*) p, (elements-idx)*sizeof(value_type)); - written_bytes += (elements-idx)*sizeof(value_type); + uint64_t remaining_elements = (width()*size())/(8*sizeof(value_type))-(idx/sizeof(value_type)); + out.write((char*) p, remaining_elements*sizeof(value_type)); + written_bytes += remaining_elements*sizeof(value_type); return written_bytes; }