This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch iotdb
in repository https://gitbox.apache.org/repos/asf/tsfile.git

commit 08e0fad43bc43ea7991d52b41502e62f6a12ba32
Author: Hongzhi Gao <[email protected]>
AuthorDate: Thu Jun 27 11:36:28 2024 +0800

    fix ZigZagCodec (#124)
    
    * fix ZigZagCodec
    
    * fix ZigZagCodec
---
 cpp/src/encoding/zigzag_decoder.h | 22 ++++++++--------------
 cpp/src/encoding/zigzag_encoder.h | 28 ++++++++++++----------------
 2 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/cpp/src/encoding/zigzag_decoder.h 
b/cpp/src/encoding/zigzag_decoder.h
index 54be5ee4..176c0ccd 100644
--- a/cpp/src/encoding/zigzag_decoder.h
+++ b/cpp/src/encoding/zigzag_decoder.h
@@ -61,14 +61,8 @@ class ZigzagDecoder {
     }
 
     void read_header(common::ByteStream &in) {
-        flush_byte_if_empty(in);
-        zigzag_length_ = buffer_;
-        buffer_ = 0;
-        bits_left_ = 0;
-        flush_byte_if_empty(in);
-        int_length_ = buffer_;
-        buffer_ = 0;
-        bits_left_ = 0;
+        common::SerializationUtil::read_var_uint(zigzag_length_, in);
+        common::SerializationUtil::read_var_uint(int_length_, in);
     }
 
     void flush_byte_if_empty(common::ByteStream &in) {
@@ -112,7 +106,7 @@ class ZigzagDecoder {
     }
 
     int64_t zigzag_decoder(int64_t stored_value_) {
-        stored_value_ = (stored_value_ >> 1) ^ -(stored_value_ & 1);
+        stored_value_ = ((uint64_t)stored_value_ >> 1) ^ -(stored_value_ & 1);
         return stored_value_;
     }
 
@@ -127,8 +121,8 @@ class ZigzagDecoder {
     int first_bit_of_byte_;
     int num_of_sorts_of_zigzag_;
     bool first_read_;
-    uint8_t zigzag_length_;
-    uint8_t int_length_;
+    uint32_t zigzag_length_;
+    uint32_t int_length_;
     std::vector<uint8_t> list_transit_in_zd_;
 };
 
@@ -141,7 +135,7 @@ int32_t ZigzagDecoder<int32_t>::decode(common::ByteStream 
&in) {
         buffer_ = 0;
         first_read_ = false;
         list_transit_in_zd_.clear();
-        for (uint8_t i = 0; i < zigzag_length_; i++) {
+        for (uint32_t i = 0; i < zigzag_length_; i++) {
             flush_byte_if_empty(in);
             list_transit_in_zd_.push_back(buffer_);
             buffer_ = 0;
@@ -174,7 +168,7 @@ int64_t ZigzagDecoder<int64_t>::decode(common::ByteStream 
&in) {
         buffer_ = 0;
         first_read_ = false;
         list_transit_in_zd_.clear();
-        for (uint8_t i = 0; i < zigzag_length_; i++) {
+        for (uint32_t i = 0; i < zigzag_length_; i++) {
             flush_byte_if_empty(in);
             list_transit_in_zd_.push_back(buffer_);
             buffer_ = 0;
@@ -202,4 +196,4 @@ typedef ZigzagDecoder<int32_t> IntZigzagDecoder;
 typedef ZigzagDecoder<int64_t> LongZigzagDecoder;
 
 }  // end namespace storage
-#endif  // ENCODING_zigzag_DECODER_H
+#endif  // ENCODING_zigzag_DECODER_H
\ No newline at end of file
diff --git a/cpp/src/encoding/zigzag_encoder.h 
b/cpp/src/encoding/zigzag_encoder.h
index bcb3aa4f..887ba07d 100644
--- a/cpp/src/encoding/zigzag_encoder.h
+++ b/cpp/src/encoding/zigzag_encoder.h
@@ -97,15 +97,15 @@ int ZigzagEncoder<int32_t>::encode(int32_t value) {
     int32_t value_zigzag = (value << 1) ^ (value >> 31);
     if ((value_zigzag & ~0x7F) != 0) {
         write_byte_with_subsequence(value_zigzag);
-        value_zigzag = value_zigzag >> 7;
+        value_zigzag = (uint32_t)value_zigzag >> 7;
         while ((value_zigzag & ~0x7F) != 0) {
             write_byte_with_subsequence(value_zigzag);
-            value_zigzag = value_zigzag >> 7;
+            value_zigzag = (uint32_t)value_zigzag >> 7;
         }
     }
 
     write_byte_without_subsequence(value_zigzag);
-    value_zigzag = value_zigzag >> 7;
+    value_zigzag = (uint32_t)value_zigzag >> 7;
 
     return common::E_OK;
 }
@@ -120,26 +120,24 @@ int ZigzagEncoder<int64_t>::encode(int64_t value) {
     int64_t value_zigzag = (value << 1) ^ (value >> 63);
     if ((value_zigzag & ~0x7F) != 0) {
         write_byte_with_subsequence(value_zigzag);
-        value_zigzag = value_zigzag >> 7;
+        value_zigzag = (uint64_t)value_zigzag >> 7;
         while ((value_zigzag & ~0x7F) != 0) {
             write_byte_with_subsequence(value_zigzag);
-            value_zigzag = value_zigzag >> 7;
+            value_zigzag = (uint64_t)value_zigzag >> 7;
         }
     }
 
     write_byte_without_subsequence(value_zigzag);
-    value_zigzag = value_zigzag >> 7;
+    value_zigzag = (uint64_t)value_zigzag >> 7;
 
     return common::E_OK;
 }
 
 template <>
 int ZigzagEncoder<int32_t>::flush(common::ByteStream &out) {
-    buffer_ = (uint8_t)(length_of_encode_bytestream_);
-    flush_byte(out);
-
-    buffer_ = (uint8_t)(length_of_input_bytestream_);
-    flush_byte(out);
+    common::SerializationUtil::write_var_uint(length_of_encode_bytestream_,
+                                              out);
+    common::SerializationUtil::write_var_uint(length_of_input_bytestream_, 
out);
 
     for (int i = 0; i < length_of_encode_bytestream_; i++) {
         buffer_ = (uint8_t)(list_transit_in_ze_[i]);
@@ -151,11 +149,9 @@ int ZigzagEncoder<int32_t>::flush(common::ByteStream &out) 
{
 
 template <>
 int ZigzagEncoder<int64_t>::flush(common::ByteStream &out) {
-    buffer_ = (uint8_t)(length_of_encode_bytestream_);
-    flush_byte(out);
-
-    buffer_ = (uint8_t)(length_of_input_bytestream_);
-    flush_byte(out);
+    common::SerializationUtil::write_var_uint(length_of_encode_bytestream_,
+                                              out);
+    common::SerializationUtil::write_var_uint(length_of_input_bytestream_, 
out);
 
     for (int i = 0; i < length_of_encode_bytestream_; i++) {
         buffer_ = (uint8_t)(list_transit_in_ze_[i]);

Reply via email to