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
use unic_char_range::CharRange;
#[derive(Copy, Clone, Debug)]
pub enum CharDataTable<V: 'static> {
#[doc(hidden)]
Direct(&'static [(char, V)]),
#[doc(hidden)]
Range(&'static [(CharRange, V)]),
}
impl<V> Default for CharDataTable<V> {
fn default() -> Self {
CharDataTable::Direct(&[])
}
}
impl<V> CharDataTable<V> {
pub fn contains(&self, needle: char) -> bool {
match *self {
CharDataTable::Direct(table) => {
table.binary_search_by_key(&needle, |&(k, _)| k).is_ok()
}
CharDataTable::Range(table) => table
.binary_search_by(|&(range, _)| range.cmp_char(needle))
.is_ok(),
}
}
}
impl<V: Copy> CharDataTable<V> {
pub fn find(&self, needle: char) -> Option<V> {
match *self {
CharDataTable::Direct(table) => table
.binary_search_by_key(&needle, |&(k, _)| k)
.map(|idx| table[idx].1)
.ok(),
CharDataTable::Range(table) => table
.binary_search_by(|&(range, _)| range.cmp_char(needle))
.map(|idx| table[idx].1)
.ok(),
}
}
pub fn find_with_range(&self, needle: char) -> Option<(CharRange, V)> {
match *self {
CharDataTable::Direct(_) => None,
CharDataTable::Range(table) => table
.binary_search_by(|&(range, _)| range.cmp_char(needle))
.map(|idx| table[idx])
.ok(),
}
}
}
impl<V: Copy + Default> CharDataTable<V> {
pub fn find_or_default(&self, needle: char) -> V {
self.find(needle).unwrap_or_else(Default::default)
}
}
#[derive(Debug)]
pub struct CharDataTableIter<'a, V: 'static>(&'a CharDataTable<V>, usize);
impl<'a, V: Copy> Iterator for CharDataTableIter<'a, V> {
type Item = (CharRange, V);
fn next(&mut self) -> Option<Self::Item> {
match *self.0 {
CharDataTable::Direct(arr) => {
if self.1 >= arr.len() {
None
} else {
let idx = self.1;
self.1 += 1;
let (ch, v) = arr[idx];
Some((chars!(ch..=ch), v))
}
}
CharDataTable::Range(arr) => {
if self.1 >= arr.len() {
None
} else {
let idx = self.1;
self.1 += 1;
Some(arr[idx])
}
}
}
}
}
impl<V> CharDataTable<V> {
pub fn iter(&self) -> CharDataTableIter<'_, V> {
CharDataTableIter(self, 0)
}
}