scrunch 0.13.0

Scrunch provides full-text-searching compression.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use buffertk::Unpackable;

use crate::Error;
use crate::builder::{Builder, Helper};

pub mod prefix;

//////////////////////////////////////////// WaveletTree ///////////////////////////////////////////

pub trait WaveletTree {
    /// Construct in the builder a byte-wise representation of the wavelet tree.
    fn construct<H: Helper>(text: &[u32], builder: &mut Builder<'_, H>) -> Result<(), Error>;

    /// The length of this [WaveletTree].  Always the number of symbols.
    fn len(&self) -> usize;
    /// A [WaveletTree] `is_empty` when it has zero symbols.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Computes `access[x]`, the value of the x'th symbol.
    fn access(&self, x: usize) -> Option<u32>;
    /// Computes `rank_q[x]`, the number of symbols q to the left of index x.
    fn rank_q(&self, q: u32, x: usize) -> Option<usize>;
    /// Computes `select_q[x]`, the offset of the x'th symbol q.
    fn select_q(&self, q: u32, x: usize) -> Option<usize>;

    /// Populate `ranges` with each symbol in `[lower, upper)` and its rank interval for that
    /// symbol.  The rank interval is half-open.
    fn symbol_rank_ranges(
        &self,
        lower: usize,
        upper: usize,
        ranges: &mut Vec<(u32, (usize, usize))>,
    ) -> Option<()> {
        if lower > upper || upper > self.len() {
            return None;
        }
        ranges.clear();
        for idx in lower..upper {
            let symbol = self.access(idx)?;
            if ranges.iter().any(|(candidate, _)| *candidate == symbol) {
                continue;
            }
            let lower_rank = self.rank_q(symbol, lower)?;
            let upper_rank = self.rank_q(symbol, upper)?;
            if lower_rank < upper_rank {
                ranges.push((symbol, (lower_rank, upper_rank)));
            }
        }
        Some(())
    }
}

///////////////////////////////////// ReferenceWaveletTreeStub /////////////////////////////////////

#[derive(Debug, Default, prototk_derive::Message)]
pub struct ReferenceWaveletTreeStub {
    #[prototk(1, fixed32)]
    text: Vec<u32>,
}

/////////////////////////////////////// ReferenceWaveletTree ///////////////////////////////////////

pub struct ReferenceWaveletTree {
    text: Vec<u32>,
}

impl WaveletTree for ReferenceWaveletTree {
    fn construct<H: Helper>(text: &[u32], builder: &mut Builder<'_, H>) -> Result<(), Error> {
        let this = ReferenceWaveletTreeStub {
            text: text.to_vec(),
        };
        builder.append_raw_packable(&this);
        Ok(())
    }

    fn len(&self) -> usize {
        self.text.len()
    }

    fn access(&self, x: usize) -> Option<u32> {
        self.text.get(x).copied()
    }

    fn rank_q(&self, q: u32, x: usize) -> Option<usize> {
        let mut rank: usize = 0;
        for i in 0..self.text.len() {
            if i == x {
                return Some(rank);
            }
            if self.text[i] == q {
                rank += 1;
            }
        }
        if x == self.text.len() {
            Some(rank)
        } else {
            None
        }
    }

    fn select_q(&self, q: u32, x: usize) -> Option<usize> {
        let mut rank: usize = 0;
        for i in 0..self.text.len() {
            if rank == x {
                return Some(i);
            }
            if self.text[i] == q {
                rank += 1;
            }
        }
        if rank == x {
            Some(self.text.len())
        } else {
            None
        }
    }
}

impl<'a> Unpackable<'a> for ReferenceWaveletTree {
    type Error = Error;

    fn unpack<'b: 'a>(buf: &'b [u8]) -> Result<(Self, &'b [u8]), Self::Error> {
        let (stub, buf) =
            ReferenceWaveletTreeStub::unpack(buf).map_err(|_| Error::InvalidWaveletTree)?;
        Ok((Self { text: stub.text }, buf))
    }
}

/////////////////////////////////////////////// tests //////////////////////////////////////////////

#[cfg(test)]
pub mod tests {
    use crate::builder::Builder;
    use crate::encoder::FixedWidthEncoder;

    use super::prefix::WaveletTree as PrefixWaveletTree;
    use super::*;

    pub fn simple_evens<'a, 'b: 'a, WT: WaveletTree + Unpackable<'a>>(buf: &'b mut Vec<u8>) {
        let mut builder = Builder::new(buf);
        // try 010101
        WT::construct(&[0, 1, 0, 1, 0, 1], &mut builder).unwrap();
        drop(builder);
        let wt = WT::unpack(buf).unwrap().0;

        assert_eq!(Some(0), wt.access(0));
        assert_eq!(Some(1), wt.access(1));
        assert_eq!(Some(0), wt.access(2));
        assert_eq!(Some(1), wt.access(3));
        assert_eq!(Some(0), wt.access(4));
        assert_eq!(Some(1), wt.access(5));

        assert_eq!(Some(0), wt.rank_q(1, 0));
        assert_eq!(Some(0), wt.rank_q(1, 1));
        assert_eq!(Some(1), wt.rank_q(1, 2));
        assert_eq!(Some(1), wt.rank_q(1, 3));
        assert_eq!(Some(2), wt.rank_q(1, 4));
        assert_eq!(Some(2), wt.rank_q(1, 5));
        assert_eq!(Some(3), wt.rank_q(1, 6));
        assert_eq!(None, wt.rank_q(1, 7));

        assert_eq!(Some(0), wt.rank_q(0, 0));
        assert_eq!(Some(1), wt.rank_q(0, 1));
        assert_eq!(Some(1), wt.rank_q(0, 2));
        assert_eq!(Some(2), wt.rank_q(0, 3));
        assert_eq!(Some(2), wt.rank_q(0, 4));
        assert_eq!(Some(3), wt.rank_q(0, 5));
        assert_eq!(Some(3), wt.rank_q(0, 6));
        assert_eq!(None, wt.rank_q(0, 7));

        assert_eq!(Some(0), wt.select_q(1, 0));
        assert_eq!(Some(2), wt.select_q(1, 1));
        assert_eq!(Some(4), wt.select_q(1, 2));
        assert_eq!(Some(6), wt.select_q(1, 3));
        assert_eq!(None, wt.select_q(1, 4));

        assert_eq!(Some(0), wt.select_q(0, 0));
        assert_eq!(Some(1), wt.select_q(0, 1));
        assert_eq!(Some(3), wt.select_q(0, 2));
        assert_eq!(Some(5), wt.select_q(0, 3));
        assert_eq!(None, wt.select_q(0, 4));
    }

    pub fn simple_odds<'a, 'b: 'a, WT: WaveletTree + Unpackable<'a>>(buf: &'b mut Vec<u8>) {
        let mut builder = Builder::new(buf);
        // try 010101
        WT::construct(&[1, 0, 1, 0, 1, 0], &mut builder).unwrap();
        drop(builder);
        let wt = WT::unpack(buf).unwrap().0;

        assert_eq!(Some(1), wt.access(0));
        assert_eq!(Some(0), wt.access(1));
        assert_eq!(Some(1), wt.access(2));
        assert_eq!(Some(0), wt.access(3));
        assert_eq!(Some(1), wt.access(4));
        assert_eq!(Some(0), wt.access(5));

        assert_eq!(Some(0), wt.rank_q(1, 0));
        assert_eq!(Some(1), wt.rank_q(1, 1));
        assert_eq!(Some(1), wt.rank_q(1, 2));
        assert_eq!(Some(2), wt.rank_q(1, 3));
        assert_eq!(Some(2), wt.rank_q(1, 4));
        assert_eq!(Some(3), wt.rank_q(1, 5));
        assert_eq!(Some(3), wt.rank_q(1, 6));
        assert_eq!(None, wt.rank_q(1, 7));

        assert_eq!(Some(0), wt.rank_q(0, 0));
        assert_eq!(Some(0), wt.rank_q(0, 1));
        assert_eq!(Some(1), wt.rank_q(0, 2));
        assert_eq!(Some(1), wt.rank_q(0, 3));
        assert_eq!(Some(2), wt.rank_q(0, 4));
        assert_eq!(Some(2), wt.rank_q(0, 5));
        assert_eq!(Some(3), wt.rank_q(0, 6));
        assert_eq!(None, wt.rank_q(0, 7));

        assert_eq!(Some(0), wt.select_q(1, 0));
        assert_eq!(Some(1), wt.select_q(1, 1));
        assert_eq!(Some(3), wt.select_q(1, 2));
        assert_eq!(Some(5), wt.select_q(1, 3));
        assert_eq!(None, wt.select_q(1, 4));

        assert_eq!(Some(0), wt.select_q(0, 0));
        assert_eq!(Some(2), wt.select_q(0, 1));
        assert_eq!(Some(4), wt.select_q(0, 2));
        assert_eq!(Some(6), wt.select_q(0, 3));
        assert_eq!(None, wt.select_q(0, 4));
    }

    pub fn bad_case_1<'a, 'b: 'a, WT: WaveletTree + Unpackable<'a>>(buf: &'b mut Vec<u8>) {
        const TEXT: &[u32] = &[
            32, 73, 73, 32, 32, 73, 73, 73, 73, 60, 73, 60, 73, 73, 73, 60, 60, 73, 73, 60, 60, 60,
            60, 60, 73, 73, 60, 73, 73, 73, 73, 60, 74, 73, 73, 73, 73, 73, 73, 73, 60, 73, 73, 73,
            61, 73, 73, 73, 73, 73, 73, 73, 73, 60, 32, 60, 73, 60, 73, 73, 60, 60, 60, 60, 73, 73,
            32, 60, 60, 73, 73, 60, 32, 73, 73, 73, 60, 60, 60, 73, 60, 60, 60, 60, 60, 60, 60, 73,
            61, 74, 73, 74, 73, 73, 60, 60, 73, 73, 60, 60, 32, 60, 60, 60, 60, 60, 60, 60, 60, 60,
            60, 60, 60, 60, 32, 60, 60, 60, 32, 32, 32, 60, 32, 60, 60, 60, 60, 60, 60, 60, 60, 32,
            60, 60, 60, 60, 60, 60, 60, 60, 32, 60, 73, 60, 60, 60, 60, 60, 60, 60, 60, 60, 32, 60,
            60, 60, 32, 32, 60, 60, 32, 60, 60, 60, 60, 60, 60, 60, 60, 32, 60, 60, 60, 32, 60, 32,
            32, 60, 60, 32, 32, 60, 32, 60, 60, 60, 32, 32, 60, 60, 60, 60, 32, 32, 60, 60, 60, 60,
            60, 32, 32, 32, 60, 32, 32, 60, 73, 73, 73, 73, 32, 73, 73, 73, 60, 73, 73, 60, 60, 32,
            73, 73, 60, 60, 32, 60, 60, 74, 73, 73, 73, 73, 73, 60, 73, 61, 73, 73, 60, 73, 61, 73,
            60, 74, 73, 60, 32, 60, 32, 60, 73, 73, 73, 73, 73, 73, 73, 73, 60, 73, 60, 60, 60, 60,
            60, 60, 73, 73, 73, 60, 73, 73, 60, 60, 32, 60, 60, 73, 60, 73, 73, 73, 73, 66, 73, 73,
            73, 61, 73, 73, 73, 73, 73, 73, 73, 60, 60, 32, 60, 60, 60, 60, 60, 60, 73, 32, 60, 73,
            73, 60, 61, 73, 73, 73, 60, 60, 73, 73, 74, 73, 60, 60, 73, 73, 73, 73, 73, 32, 73, 60,
            60, 61, 73, 60, 73, 73, 61, 73, 73, 73, 73, 74, 32, 60, 73, 73, 60, 60, 73, 60, 73, 73,
            60, 60, 73, 73, 73, 60, 73, 60, 73, 73, 60, 73, 73, 73, 73, 73, 60, 73, 73, 73, 73, 73,
            73, 60, 73, 32, 72, 60, 74, 73, 73, 60, 73, 73, 73, 73, 73, 73, 73, 73, 70, 73, 73, 73,
            73, 73, 60, 60, 73, 60, 73, 60, 60, 60, 60, 73, 73, 73, 60, 32, 32, 32, 73, 73, 73, 73,
            60, 60, 60, 60, 32, 73, 60, 60, 60, 60, 60, 32, 60, 32, 60, 60, 32, 60, 60, 32, 32, 60,
            60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 32, 73, 73, 73, 60, 60, 73, 73, 32,
            32, 73, 32, 73, 73, 61, 73, 73, 73, 73, 61, 66, 73, 73, 73, 73, 60, 60, 73, 73, 73, 60,
            73, 73, 60, 60, 60, 73, 74, 74, 73, 61, 32, 32, 73, 60, 74, 73, 73, 73, 73, 74, 32, 32,
            60, 73, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 74, 32, 60, 32, 60, 73, 32, 60, 32,
            32, 32, 60, 60, 32, 60, 73, 60, 74, 32, 32, 73, 73, 73, 73, 73, 73, 74, 73, 73, 73, 74,
            73, 73, 74, 73, 73, 73, 73, 60, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 60, 60,
            60, 60, 73, 60, 60, 60, 73, 60, 60, 32, 60, 73, 74, 60, 60, 60, 60, 32, 60, 60, 60, 60,
            60, 32, 32, 32, 32, 60, 32, 32, 60, 74, 73, 73, 74, 74, 73, 60, 60, 32, 60, 60, 60, 73,
            73, 73, 73, 73, 73, 73, 73, 73, 74, 73, 60, 66, 60, 60, 60, 60, 60, 74, 73, 73, 73, 73,
            73, 73, 32, 73, 73, 60, 73, 74, 74, 73, 73, 60, 73, 60, 73, 60, 73, 73, 73, 60, 73, 73,
            74, 74, 32, 60, 73, 60, 74, 73, 60, 73, 73, 74, 61, 60, 60, 60, 32, 73, 73, 60, 60, 73,
            60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 32, 60, 60, 60, 60, 60, 32, 73, 60, 32,
            73, 73, 73, 73, 73, 73, 73, 73, 73, 60, 73, 73, 60, 73, 73, 73, 73, 73, 73, 73, 73, 73,
            73, 73, 73, 73, 73, 73, 60, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
            73, 73, 73, 45, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73,
            73, 73, 45, 73, 73, 73, 73, 73, 73, 73, 73, 32, 73, 61, 61, 61, 61, 61, 73, 74, 74, 74,
            74, 73, 74, 74, 74, 74, 74, 73, 73, 60, 74, 73, 73, 60, 60, 60, 60, 60, 60, 60, 60, 60,
            32, 32, 74, 74, 73, 73, 74, 73, 60, 73, 73, 61, 32, 60, 73, 32, 60, 73, 73, 60, 73, 60,
            60, 60, 73, 73, 73, 73, 73, 60, 60, 73, 73, 73, 32, 60, 60, 60, 60, 60, 60, 60, 60, 60,
            32, 73, 61, 60, 60, 32, 60, 60, 73, 74, 73, 60, 73, 60, 60, 32, 73, 60, 60, 60, 60, 73,
            73, 73, 73, 73, 73, 73, 60, 73, 60, 73, 73, 73, 73, 73, 73, 60, 73, 32, 73, 32, 60, 73,
            60, 60, 32, 73, 73, 73, 73, 60, 73, 60, 32, 74, 73, 73, 60, 60, 32, 74, 32, 60, 60, 60,
            46, 73, 73, 60, 73, 73, 73, 73, 73, 74, 60, 66, 73, 74, 73, 73, 60, 60, 73, 61, 61, 73,
            73, 73, 60, 60, 32, 73, 73, 60, 66, 60, 60, 60, 60, 60, 73, 73, 60, 60, 73, 73, 32, 73,
            60, 73, 60, 73, 73, 73, 32, 73, 60, 60, 32, 60, 73, 73, 60, 74, 73, 73, 60, 73, 32, 73,
            32, 32, 60, 46, 73, 73, 73, 32, 60, 73, 60, 60, 73, 73, 73, 73, 73, 74, 60, 60, 73, 73,
            60, 60, 73, 32, 60, 73, 73, 73, 73, 60, 60, 74, 32, 73, 74, 73, 73, 73, 74, 32, 60, 32,
            32, 32, 74, 60, 60, 45, 60, 32, 32, 60, 32, 73, 60, 60, 32, 60, 60, 60, 60, 73, 60, 60,
            60, 32, 60, 60, 60, 60, 60, 73, 73, 60, 60, 60, 60, 73, 32, 73, 32, 74, 60, 73, 32, 73,
            73, 60, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 60, 73,
            73, 73, 73, 73, 73, 73, 73, 73, 61, 74, 73, 32, 73, 73, 74, 32, 73, 73, 73, 73, 73, 60,
            73, 73, 60, 73, 73, 74, 60, 73, 60, 73, 61, 74, 74, 74, 66, 73, 73, 73, 73, 73, 73, 60,
            73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 60, 60, 60, 60, 60, 60, 60, 61, 73, 32,
            60, 32, 60, 32, 60, 60, 60, 60, 60, 60, 60, 32, 60, 60, 32, 73, 32, 60, 60, 60, 32, 32,
            32, 32, 32, 32, 60, 60, 32, 32, 32, 60, 60, 32, 60, 60, 60, 60, 60, 73, 60, 60, 60, 61,
            60, 60, 32, 73, 73, 32, 32, 32, 73, 32, 73, 60, 73, 73, 73, 73, 60, 32, 60, 60, 60, 61,
            60, 60, 32, 60, 60, 73, 60, 60, 32, 60, 32, 32, 60, 60, 32, 60, 32, 32, 32, 60, 60, 32,
            32, 32, 60, 60, 32, 60, 60, 32, 32, 60, 32, 73, 73, 73, 61, 74, 32, 73, 74, 61, 73, 74,
            74, 73, 78, 78, 50, 73, 73, 78, 73, 73, 78, 61, 60, 73, 60, 60, 73, 73, 73, 60, 32, 73,
            73, 73, 61, 73, 60, 60, 32, 73, 60, 60, 73, 73, 73, 73, 73, 32, 73, 73, 73, 73, 73, 73,
            60, 60, 73, 32, 73, 32, 60, 73, 32, 60, 73, 78, 78, 50, 73, 73, 73, 73, 32, 73, 73, 32,
            73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 72, 73, 73, 73, 73, 72, 73, 73, 73, 73,
            73, 73, 74, 76, 74, 74, 74, 74, 74, 74, 73, 73, 73, 70, 70, 70, 70, 60, 70, 70, 70, 73,
            73, 73, 70, 70, 60, 78, 70, 61, 70, 72, 78, 70, 76, 60, 70, 70, 72, 70, 70, 70, 74, 60,
            72, 70, 74, 70, 72, 72, 60, 70, 61, 70, 70, 70, 70, 70, 70, 61, 60, 60, 76, 72, 76, 70,
            73, 60, 78, 73, 61, 60, 73, 60, 73, 60, 73, 73, 73, 73, 73, 78, 60, 70, 70, 70, 70, 70,
            70, 70, 70, 70, 70, 70, 72, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 72, 72, 70, 70,
            70, 70, 70, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
            37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
            37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
            37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 65,
            65, 65, 65, 65, 65, 37, 78, 74, 73, 73, 74, 74, 74, 72, 72, 72, 72, 72, 72, 72, 72, 72,
            72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
            72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 73, 73, 73, 76, 73, 73, 73,
            76, 73, 73, 76, 73, 76, 76, 76, 76, 73, 73, 73, 73, 73, 73, 76, 73, 76, 76, 73, 73, 73,
            76, 63, 73, 76, 76, 73, 73, 73, 76, 76, 73, 73, 73, 73, 73, 73, 59, 76, 49, 49, 49, 49,
            49, 49, 49, 49, 49, 49, 49, 73, 73, 73, 73, 73, 76, 31, 59, 59, 59, 59, 59, 73, 73, 59,
            62, 62, 62, 62, 62, 62, 59, 62, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59,
            59, 1, 73, 73, 73, 60, 60, 60, 60, 71, 60, 73, 73, 73, 60, 60, 60, 73, 60, 59, 60, 59,
            70, 59, 70, 59, 70, 70, 78, 60, 70, 61, 60, 70, 60, 60, 60, 70, 70, 60, 70, 78, 70, 61,
            70, 60, 70, 70, 61, 59, 59, 61, 59, 60, 60, 70, 61, 60, 60, 73, 73, 73, 73, 2, 2, 2,
            73, 73, 63, 63, 63, 73, 73, 61, 73, 73, 73, 61, 73, 72, 45, 73, 73, 73, 73, 73, 73, 73,
            73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 45, 73, 73, 73, 73, 73, 73, 73,
            73, 73, 73, 73, 60, 60, 61, 60, 60, 68, 60, 2, 66, 66, 1, 66, 66, 71, 71, 2, 70, 70,
            71, 2, 71, 66, 66, 2, 65, 71, 71, 2, 2, 2, 2, 1, 66, 66, 66, 66, 66, 66, 66, 66, 66,
            66, 74, 74, 74, 74, 74, 66, 74, 46, 74, 74, 74, 68, 61, 60, 60, 60, 60, 60, 60, 60, 60,
            60, 60, 60, 74, 74, 59, 59, 59, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 73, 73,
            59, 59, 62, 62, 62, 59, 62, 62, 62, 62, 59, 62, 59, 62, 59, 59, 62, 59,
        ];
        const CHARS: &[u32] = &[
            1, 2, 31, 32, 37, 45, 46, 49, 50, 59, 60, 61, 62, 63, 64, 65, 66, 68, 70, 71, 72, 73,
            74, 76, 78,
        ];
        let expected = ReferenceWaveletTree {
            text: TEXT.to_vec(),
        };
        let mut builder = Builder::new(buf);
        WT::construct(TEXT, &mut builder).unwrap();
        drop(builder);
        let wt = WT::unpack(buf).unwrap().0;
        for (i, t) in TEXT.iter().enumerate() {
            assert_eq!(Some(*t), expected.access(i), "i = {i}");
            assert_eq!(Some(*t), wt.access(i), "i = {i}");
        }
        for c in CHARS.iter() {
            for i in 0..TEXT.len() {
                assert_eq!(expected.rank_q(*c, i), wt.rank_q(*c, i));
                assert_eq!(expected.select_q(*c, i), wt.select_q(*c, i));
            }
        }
    }

    macro_rules! test_WaveletTree {
        ($name:ident, $WT:path) => {
            mod $name {
                use buffertk::Unpackable;

                use $crate::builder::Builder;
                use $crate::wavelet_tree::ReferenceWaveletTree;
                use $crate::wavelet_tree::WaveletTree;

                #[test]
                fn simple_evens() {
                    let mut buf = vec![];
                    $crate::wavelet_tree::tests::simple_evens::<$WT>(&mut buf);
                }

                #[test]
                fn simple_odds() {
                    let mut buf = vec![];
                    $crate::wavelet_tree::tests::simple_odds::<$WT>(&mut buf);
                }

                #[test]
                fn bad_case_1() {
                    let mut buf = vec![];
                    $crate::wavelet_tree::tests::bad_case_1::<$WT>(&mut buf);
                }

                proptest::prop_compose! {
                    pub fn arb_wavelet_tree()(bv in proptest::collection::vec(1u32..=16u32, 0..64)) -> Vec<u32> {
                        bv
                    }
                }

                proptest::proptest! {
                    #[test]
                    fn properties(bv in arb_wavelet_tree()) {
                        let mut exp_buf = vec![];
                        let mut exp_builder = Builder::new(&mut exp_buf);
                        ReferenceWaveletTree::construct(&bv, &mut exp_builder).unwrap();
                        drop(exp_builder);
                        let exp = ReferenceWaveletTree::unpack(exp_buf.as_slice()).unwrap().0;

                        let mut got_buf = vec![];
                        let mut got_builder = Builder::new(&mut got_buf);
                        <$WT as WaveletTree>::construct(&bv, &mut got_builder).unwrap();
                        drop(got_builder);
                        let got = <$WT as Unpackable>::unpack(got_buf.as_slice()).unwrap().0;

                        assert_eq!(exp.len(), got.len());
                        assert_eq!(exp.is_empty(), got.is_empty());
                        for x in 0..=bv.len() {
                            assert_eq!(exp.access(x), got.access(x));
                            for q in bv.iter() {
                                assert_eq!(exp.rank_q(*q, x), got.rank_q(*q, x));
                                assert_eq!(exp.select_q(*q, x), got.select_q(*q, x));
                            }
                        }
                    }
                }
            }
        };
    }

    test_WaveletTree!(reference, super::ReferenceWaveletTree);
    test_WaveletTree!(
        prefix_fixed,
        super::PrefixWaveletTree<super::FixedWidthEncoder>
    );
}