Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bit-stream-utils.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_BIT_STREAM_UTILS_H
17 #define IMPALA_UTIL_BIT_STREAM_UTILS_H
18 
19 #include <boost/cstdint.hpp>
20 #include <string.h>
21 #include "common/compiler-util.h"
22 #include "common/logging.h"
23 #include "util/bit-util.h"
24 
25 namespace impala {
26 
30 class BitWriter {
31  public:
34  BitWriter(uint8_t* buffer, int buffer_len) :
35  buffer_(buffer),
36  max_bytes_(buffer_len) {
37  Clear();
38  }
39 
40  void Clear() {
41  buffered_values_ = 0;
42  byte_offset_ = 0;
43  bit_offset_ = 0;
44  }
45 
48  int bytes_written() const { return byte_offset_ + BitUtil::Ceil(bit_offset_, 8); }
49  uint8_t* buffer() const { return buffer_; }
50  int buffer_len() const { return max_bytes_; }
51 
54  bool PutValue(uint64_t v, int num_bits);
55 
58  template<typename T>
59  bool PutAligned(T v, int num_bytes);
60 
65  bool PutVlqInt(int32_t v);
66 
70  uint8_t* GetNextBytePtr(int num_bytes = 1);
71 
75  void Flush(bool align=false);
76 
77  private:
78  uint8_t* buffer_;
80 
84 
85  int byte_offset_; // Offset in buffer_
86  int bit_offset_; // Offset in buffered_values_
87 };
88 
92 class BitReader {
93  public:
95  BitReader(uint8_t* buffer, int buffer_len) :
96  buffer_(buffer),
97  max_bytes_(buffer_len),
98  byte_offset_(0),
99  bit_offset_(0) {
100  int num_bytes = std::min(8, max_bytes_ - byte_offset_);
101  memcpy(&buffered_values_, buffer_ + byte_offset_, num_bytes);
102  }
103 
104  BitReader() : buffer_(NULL), max_bytes_(0) {}
105 
108  template<typename T>
109  bool GetValue(int num_bits, T* v);
110 
115  template<typename T>
116  bool GetAligned(int num_bytes, T* v);
117 
120  bool GetVlqInt(int32_t* v);
121 
125 
127  static const int MAX_VLQ_BYTE_LEN = 5;
128 
129  private:
130  uint8_t* buffer_;
132 
136 
137  int byte_offset_; // Offset in buffer_
138  int bit_offset_; // Offset in buffered_values_
139 };
140 
141 }
142 
143 #endif
bool GetAligned(int num_bytes, T *v)
bool PutValue(uint64_t v, int num_bits)
int bytes_written() const
uint8_t * buffer() const
BitWriter(uint8_t *buffer, int buffer_len)
uint8_t * GetNextBytePtr(int num_bytes=1)
bool PutAligned(T v, int num_bytes)
bool GetVlqInt(int32_t *v)
void Flush(bool align=false)
static int Ceil(int value, int divisor)
Returns the ceil of value/divisor.
Definition: bit-util.h:32
int buffer_len() const
BitReader(uint8_t *buffer, int buffer_len)
'buffer' is the buffer to read from. The buffer's length is 'buffer_len'.
static const int MAX_VLQ_BYTE_LEN
Maximum byte length of a vlq encoded int.
bool GetValue(int num_bits, T *v)