xapian-core  2.0.0
bitstream.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004,2005,2006,2008,2012,2013,2014,2017,2018 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_BITSTREAM_H
22 #define XAPIAN_INCLUDED_BITSTREAM_H
23 
24 #include <xapian/types.h>
25 
26 #include "api/smallvector.h"
27 
28 #include <string>
29 #include <vector>
30 
31 namespace Xapian {
32 
34 class BitWriter {
35  std::string buf;
36  int n_bits = 0;
38 
39  public:
41  BitWriter() { }
42 
44  explicit BitWriter(const std::string& seed)
45  : buf(seed) { }
46 
48  void encode(Xapian::termpos value, Xapian::termpos outof);
49 
51  std::string& freeze() {
52  if (n_bits) {
53  buf += char(acc);
54  n_bits = 0;
55  acc = 0;
56  }
57  return buf;
58  }
59 
62  int j, int k);
63 };
64 
66 class BitReader {
67  const char* p;
68 
69  const char* end;
70 
71  int n_bits;
72 
74 
75  Xapian::termpos read_bits(int count);
76 
77  struct DIStack {
78  int j, k;
80  };
81 
82  struct DIState : public DIStack {
84 
85  void set_j(int j_, Xapian::termpos pos_j_) {
86  j = j_;
87  pos_j = pos_j_;
88  }
89  void set_k(int k_, Xapian::termpos pos_k_) {
90  k = k_;
91  pos_k = pos_k_;
92  }
93  void uninit() {
94  j = 1;
95  k = 0;
96  }
97  DIState() { uninit(); }
98  DIState(int j_, int k_,
99  Xapian::termpos pos_j_, Xapian::termpos pos_k_) {
100  set_j(j_, pos_j_);
101  set_k(k_, pos_k_);
102  }
103  void operator=(const DIStack& o) {
104  j = o.j;
105  set_k(o.k, o.pos_k);
106  }
107  bool is_next() const { return j + 1 < k; }
108  bool is_initialized() const {
109  return j <= k;
110  }
111  // Given pos[j] = pos_j and pos[k] = pos_k, how many possible position
112  // values are there for the value midway between?
114  return pos_k - pos_j - Xapian::termpos(k - j) + 1;
115  }
116  };
117 
118  std::vector<DIStack> di_stack;
120 
121  public:
122  // Construct.
123  BitReader() { }
124 
125  // Construct and set data.
126  BitReader(const char* p_, const char* end_)
127  : p(p_), end(end_), n_bits(0), acc(0) { }
128 
129  // Initialise with fresh data.
130  void init(const char* p_, const char* end_) {
131  p = p_;
132  end = end_;
133  n_bits = 0;
134  acc = 0;
135  di_stack.clear();
136  di_current.uninit();
137  }
138 
139  // Decode value, known to be less than outof.
140  Xapian::termpos decode(Xapian::termpos outof, bool force = false);
141 
142  // Check all the data has been read. Because it'll be zero padded
143  // to fill a byte, the best we can actually do is check that
144  // there's less than a byte left and that all remaining bits are
145  // zero.
146  bool check_all_gone() const {
147  return (p == end && n_bits <= 7 && acc == 0);
148  }
149 
151  void decode_interpolative(int j, int k,
152  Xapian::termpos pos_j, Xapian::termpos pos_k);
153 
156 };
157 
158 }
159 
160 using Xapian::BitWriter;
161 using Xapian::BitReader;
162 
163 #endif // XAPIAN_INCLUDED_BITSTREAM_H
Read a stream created by BitWriter.
Definition: bitstream.h:66
const char * p
Definition: bitstream.h:67
Xapian::termpos read_bits(int count)
Definition: bitstream.cc:203
std::vector< DIStack > di_stack
Definition: bitstream.h:118
bool check_all_gone() const
Definition: bitstream.h:146
BitReader(const char *p_, const char *end_)
Definition: bitstream.h:126
const char * end
Definition: bitstream.h:69
DIState di_current
Definition: bitstream.h:119
Xapian::termpos acc
Definition: bitstream.h:73
void init(const char *p_, const char *end_)
Definition: bitstream.h:130
Xapian::termpos decode(Xapian::termpos outof, bool force=false)
Definition: bitstream.cc:178
void decode_interpolative(int j, int k, Xapian::termpos pos_j, Xapian::termpos pos_k)
Perform interpolative decoding between elements between j and k.
Definition: bitstream.cc:229
Xapian::termpos decode_interpolative_next()
Perform on-demand interpolative decoding.
Definition: bitstream.cc:239
Create a stream to which non-byte-aligned values can be written.
Definition: bitstream.h:34
BitWriter(const std::string &seed)
Construct with the contents of seed already in the stream.
Definition: bitstream.h:44
BitWriter()
Construct empty.
Definition: bitstream.h:41
Xapian::termpos acc
Definition: bitstream.h:37
void encode(Xapian::termpos value, Xapian::termpos outof)
Encode value, known to be less than outof.
Definition: bitstream.cc:92
std::string buf
Definition: bitstream.h:35
std::string & freeze()
Finish encoding and return the encoded data as a std::string.
Definition: bitstream.h:51
void encode_interpolative(const Xapian::VecCOW< Xapian::termpos > &pos, int j, int k)
Perform interpolative encoding of pos elements between j and k.
Definition: bitstream.cc:158
Suitable for "simple" type T.
Definition: smallvector.h:62
Xapian::termpos pos
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_TERMPOS_BASE_TYPE termpos
A term position within a document or query.
Definition: types.h:75
Custom vector implementations using small vector optimisation.
static int seed
Definition: stemtest.cc:46
Xapian::termpos pos_k
Definition: bitstream.h:79
Xapian::termpos outof() const
Definition: bitstream.h:113
void set_j(int j_, Xapian::termpos pos_j_)
Definition: bitstream.h:85
bool is_initialized() const
Definition: bitstream.h:108
DIState(int j_, int k_, Xapian::termpos pos_j_, Xapian::termpos pos_k_)
Definition: bitstream.h:98
void set_k(int k_, Xapian::termpos pos_k_)
Definition: bitstream.h:89
void operator=(const DIStack &o)
Definition: bitstream.h:103
Xapian::termpos pos_j
Definition: bitstream.h:83
typedefs for Xapian