Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
bit-stream-utils.8byte.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_EXPERIMENTS_BIT_STREAM_UTILS_8BYTE_H
17 #define IMPALA_EXPERIMENTS_BIT_STREAM_UTILS_8BYTE_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 
31  public:
35  buffer_(reinterpret_cast<uint64_t*>(buffer)),
36  max_bytes_(buffer_len),
37  offset_(0),
38  bit_offset_(0) {
39  DCHECK_EQ(buffer_len % 8, 0);
40  }
41 
42  void Clear() {
43  offset_ = 0;
44  bit_offset_ = 0;
45  memset(buffer_, 0, max_bytes_);
46  }
47 
48  uint8_t* buffer() const { return reinterpret_cast<uint8_t*>(buffer_); }
49  int buffer_len() const { return max_bytes_; }
50 
51  inline int bytes_written() const {
52  return offset_ * 8 + BitUtil::Ceil(bit_offset_, 8);
53  }
54 
57  bool PutValue(uint64_t v, int num_bits);
58 
60  template<typename T>
61  bool PutAligned(T v, int num_bits);
62 
67  bool PutVlqInt(int32_t v);
68 
72  uint8_t* GetNextBytePtr(int num_bytes = 1);
73 
74  private:
77  int offset_; // Offset into buffer_
78  int bit_offset_; // Offset into current uint64_t
79 };
80 
85  public:
88  BitReader_8byte(uint8_t* buffer, int buffer_len) :
89  buffer_(reinterpret_cast<uint64_t*>(buffer)),
90  max_bytes_(buffer_len),
91  offset_(0),
92  bit_offset_(0) {
93  DCHECK_EQ(buffer_len % 8, 0);
94  }
95 
97 
100  template<typename T>
101  bool GetValue(int num_bits, T* v);
102 
106  template<typename T>
107  bool GetAligned(int num_bits, T* v);
108 
111  bool GetVlqInt(int32_t* v);
112 
115  inline int bytes_left() {
116  return max_bytes_ - (offset_ * 8 + BitUtil::Ceil(bit_offset_, 8));
117  }
118 
120  static const int MAX_VLQ_BYTE_LEN = 5;
121 
122  private:
125  int offset_; // Offset into buffer_
126  int bit_offset_; // Offset into current uint64_t
127 
129  inline void Align();
130 };
131 
132 }
133 
134 #endif
BitReader_8byte(uint8_t *buffer, int buffer_len)
void Align()
Advances offset_ and/or bit_offset_ to next byte boundary in buffer_.
BitWriter_8byte(uint8_t *buffer, int buffer_len)
bool GetValue(int num_bits, T *v)
static int Ceil(int value, int divisor)
Returns the ceil of value/divisor.
Definition: bit-util.h:32
static const int MAX_VLQ_BYTE_LEN
Maximum byte length of a vlq encoded int.
bool GetAligned(int num_bits, T *v)
uint8_t * GetNextBytePtr(int num_bytes=1)
bool PutAligned(T v, int num_bits)
Writes v to the next aligned byte.
bool PutValue(uint64_t v, int num_bits)