kangkaisen commented on a change in pull request #1818: Add frame_of_reference 
page
URL: https://github.com/apache/incubator-doris/pull/1818#discussion_r325462681
 
 

 ##########
 File path: be/src/util/frame_of_reference_coding.cpp
 ##########
 @@ -0,0 +1,152 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "frame_of_reference_coding.h"
+
+namespace doris {
+
+static inline uint8_t bits(const uint32_t v) {
+    return v == 0 ? 0 : 32 - __builtin_clz(v);
+}
+
+template<typename T>
+void ForEncoder<T>::put(T value) {
+    _buffered_values[_buffered_values_num++] = value;
+    if (_buffered_values_num == FOR_CODING_FRAME_VALUE_NUM) {
+        bit_packing_buffered_value();
+        _buffered_values_num = 0;
+        _frame_count++;
+    }
+    _count++;
+}
+
+template<typename T>
+void ForEncoder<T>::bit_packing_buffered_value() {
+    _frame_offsets.push_back(_bit_writer.bits_written());
+
+    T min = _buffered_values[0];
+    T max = _buffered_values[0];
+    for(uint8_t i = 1; i < _buffered_values_num; ++i) {
+        if(_buffered_values[i] < min) {
+            min = _buffered_values[i];
+        }
+        if(_buffered_values[i] > max) {
+            max = _buffered_values[i];
+        }
+    }
+    uint8_t bit_width = bits(static_cast<T>(max - min));
+
+    _bit_writer.PutValue(min, sizeof(T) * BYTE_SIZE);
+    for(uint8_t i = 0; i < _buffered_values_num; ++i) {
+        _bit_writer.PutValue(_buffered_values[i] - min, bit_width);
+    }
+}
+
+template<typename T>
+uint32_t ForEncoder<T>::flush() {
+    if (_buffered_values_num != 0) {
+        bit_packing_buffered_value();
+        _frame_count++;
+    }
+
+    // in order to compute the last frame bit_width when decode frame
+    _frame_offsets.push_back(_bit_writer.bits_written());
+
+    // align to bytes
+    _bit_writer.Flush(true);
+
+    // write the footer
+    for(auto i: _frame_offsets) {
+        _bit_writer.PutValue(i, INT32_SIZE);
+    }
+
+    // write last frame value number
+    if (_buffered_values_num != 0) {
+        _bit_writer.PutValue(_buffered_values_num,BYTE_SIZE);
 
 Review comment:
   OK

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to