]> git.lizzy.rs Git - rust.git/blob - src/libcore/unicode/bool_trie.rs
revert making internal APIs const fn.
[rust.git] / src / libcore / unicode / bool_trie.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /// BoolTrie is a trie for representing a set of Unicode codepoints. It is
12 /// implemented with postfix compression (sharing of identical child nodes),
13 /// which gives both compact size and fast lookup.
14 ///
15 /// The space of Unicode codepoints is divided into 3 subareas, each
16 /// represented by a trie with different depth. In the first (0..0x800), there
17 /// is no trie structure at all; each u64 entry corresponds to a bitvector
18 /// effectively holding 64 bool values.
19 ///
20 /// In the second (0x800..0x10000), each child of the root node represents a
21 /// 64-wide subrange, but instead of storing the full 64-bit value of the leaf,
22 /// the trie stores an 8-bit index into a shared table of leaf values. This
23 /// exploits the fact that in reasonable sets, many such leaves can be shared.
24 ///
25 /// In the third (0x10000..0x110000), each child of the root node represents a
26 /// 4096-wide subrange, and the trie stores an 8-bit index into a 64-byte slice
27 /// of a child tree. Each of these 64 bytes represents an index into the table
28 /// of shared 64-bit leaf values. This exploits the sparse structure in the
29 /// non-BMP range of most Unicode sets.
30 pub struct BoolTrie {
31     // 0..0x800 (corresponding to 1 and 2 byte utf-8 sequences)
32     pub r1: [u64; 32],   // leaves
33
34     // 0x800..0x10000 (corresponding to 3 byte utf-8 sequences)
35     pub r2: [u8; 992],      // first level
36     pub r3: &'static [u64],  // leaves
37
38     // 0x10000..0x110000 (corresponding to 4 byte utf-8 sequences)
39     pub r4: [u8; 256],       // first level
40     pub r5: &'static [u8],   // second level
41     pub r6: &'static [u64],  // leaves
42 }
43 impl BoolTrie {
44     pub fn lookup(&self, c: char) -> bool {
45         let c = c as u32;
46         if c < 0x800 {
47             trie_range_leaf(c, self.r1[(c >> 6) as usize])
48         } else if c < 0x10000 {
49             let child = self.r2[(c >> 6) as usize - 0x20];
50             trie_range_leaf(c, self.r3[child as usize])
51         } else {
52             let child = self.r4[(c >> 12) as usize - 0x10];
53             let leaf = self.r5[((child as usize) << 6) + ((c >> 6) as usize & 0x3f)];
54             trie_range_leaf(c, self.r6[leaf as usize])
55         }
56     }
57 }
58
59 pub struct SmallBoolTrie {
60     pub(crate) r1: &'static [u8],  // first level
61     pub(crate) r2: &'static [u64],  // leaves
62 }
63
64 impl SmallBoolTrie {
65     pub fn lookup(&self, c: char) -> bool {
66         let c = c as u32;
67         match self.r1.get((c >> 6) as usize) {
68             Some(&child) => trie_range_leaf(c, self.r2[child as usize]),
69             None => false,
70         }
71     }
72 }
73
74 fn trie_range_leaf(c: u32, bitmap_chunk: u64) -> bool {
75     ((bitmap_chunk >> (c & 63)) & 1) != 0
76 }