icu_collections/char16trie/
trie.rs1use zerofrom::ZeroFrom;
6use zerovec::{ZeroSlice, ZeroVec};
7
8const MAX_BRANCH_LINEAR_SUB_NODE_LENGTH: usize = 5;
16
17const MIN_LINEAR_MATCH: u16 = 0x30;
19const MAX_LINEAR_MATCH_LENGTH: u16 = 0x10;
20
21const MIN_VALUE_LEAD: u16 = MIN_LINEAR_MATCH + MAX_LINEAR_MATCH_LENGTH; const NODE_TYPE_MASK: u16 = MIN_VALUE_LEAD - 1; const VALUE_IS_FINAL: u16 = 0x8000;
29
30const MAX_ONE_UNIT_VALUE: u16 = 0x3fff;
32
33const MIN_TWO_UNIT_VALUE_LEAD: u16 = MAX_ONE_UNIT_VALUE + 1; const MAX_ONE_UNIT_NODE_VALUE: u16 = 0xff;
36
37const MIN_TWO_UNIT_NODE_VALUE_LEAD: u16 = MIN_VALUE_LEAD + ((MAX_ONE_UNIT_NODE_VALUE + 1) << 6); const THREE_UNIT_NODE_VALUE_LEAD: u16 = 0x7fc0;
40
41const THREE_UNIT_VALUE_LEAD: u16 = 0x7fff;
42
43const MAX_ONE_UNIT_DELTA: u16 = 0xfbff;
45const MIN_TWO_UNIT_DELTA_LEAD: u16 = MAX_ONE_UNIT_DELTA + 1; const THREE_UNIT_DELTA_LEAD: u16 = 0xffff;
47
48fn skip_value(pos: usize, lead: u16) -> usize {
49 if lead < MIN_TWO_UNIT_VALUE_LEAD {
50 pos
51 } else if lead < THREE_UNIT_VALUE_LEAD {
52 pos + 1
53 } else {
54 pos + 2
55 }
56}
57
58fn skip_node_value(pos: usize, lead: u16) -> usize {
59 if lead < MIN_TWO_UNIT_NODE_VALUE_LEAD {
60 pos
61 } else if lead < THREE_UNIT_NODE_VALUE_LEAD {
62 pos + 1
63 } else {
64 pos + 2
65 }
66}
67
68#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
79#[cfg_attr(feature = "databake", derive(databake::Bake))]
80#[cfg_attr(feature = "databake", databake(path = icu_collections::char16trie))]
81#[derive(#[automatically_derived]
impl<'data> ::core::clone::Clone for Char16Trie<'data> {
#[inline]
fn clone(&self) -> Char16Trie<'data> {
Char16Trie { data: ::core::clone::Clone::clone(&self.data) }
}
}Clone, #[automatically_derived]
impl<'data> ::core::fmt::Debug for Char16Trie<'data> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "Char16Trie",
"data", &&self.data)
}
}Debug, #[automatically_derived]
impl<'data> ::core::cmp::PartialEq for Char16Trie<'data> {
#[inline]
fn eq(&self, other: &Char16Trie<'data>) -> bool {
self.data == other.data
}
}PartialEq, #[automatically_derived]
impl<'data> ::core::cmp::Eq for Char16Trie<'data> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<ZeroVec<'data, u16>>;
}
}Eq, impl<'zf, 'zf_inner> zerofrom::ZeroFrom<'zf, Char16Trie<'zf_inner>> for
Char16Trie<'zf> where {
fn zero_from(this: &'zf Char16Trie<'zf_inner>) -> Self {
match *this {
Char16Trie { data: ref __binding_0 } => {
Char16Trie {
data: <ZeroVec<'zf, u16> as
zerofrom::ZeroFrom<'zf,
ZeroVec<'zf_inner, u16>>>::zero_from(__binding_0),
}
}
}
}
}ZeroFrom)]
82pub struct Char16Trie<'data> {
83 #[cfg_attr(feature = "serde", serde(borrow))]
85 #[doc(hidden)] pub data: ZeroVec<'data, u16>,
87}
88
89impl<'data> Char16Trie<'data> {
90 #[inline]
92 pub fn new(data: ZeroVec<'data, u16>) -> Self {
93 Self { data }
94 }
95
96 #[inline]
98 pub fn iter(&self) -> Char16TrieIterator<'_> {
99 Char16TrieIterator::new(&self.data)
100 }
101}
102
103#[derive(#[automatically_derived]
impl<'a> ::core::clone::Clone for Char16TrieIterator<'a> {
#[inline]
fn clone(&self) -> Char16TrieIterator<'a> {
Char16TrieIterator {
trie: ::core::clone::Clone::clone(&self.trie),
pos: ::core::clone::Clone::clone(&self.pos),
remaining_match_length: ::core::clone::Clone::clone(&self.remaining_match_length),
}
}
}Clone)]
105pub struct Char16TrieIterator<'a> {
106 trie: &'a ZeroSlice<u16>,
108 pos: Option<usize>,
110 remaining_match_length: Option<usize>,
113}
114
115#[derive(#[automatically_derived]
impl ::core::clone::Clone for TrieResult {
#[inline]
fn clone(&self) -> TrieResult {
let _: ::core::clone::AssertParamIsClone<i32>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TrieResult { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for TrieResult {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TrieResult::NoMatch =>
::core::fmt::Formatter::write_str(f, "NoMatch"),
TrieResult::NoValue =>
::core::fmt::Formatter::write_str(f, "NoValue"),
TrieResult::FinalValue(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"FinalValue", &__self_0),
TrieResult::Intermediate(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Intermediate", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for TrieResult {
#[inline]
fn eq(&self, other: &TrieResult) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(TrieResult::FinalValue(__self_0),
TrieResult::FinalValue(__arg1_0)) => __self_0 == __arg1_0,
(TrieResult::Intermediate(__self_0),
TrieResult::Intermediate(__arg1_0)) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq)]
117pub enum TrieResult {
118 NoMatch,
122 NoValue,
125 FinalValue(i32),
129 Intermediate(i32),
133}
134
135fn u16_lead(supplementary: i32) -> u16 {
140 (((supplementary) >> 10) + 0xd7c0) as u16
141}
142
143fn u16_tail(supplementary: i32) -> u16 {
148 (((supplementary) & 0x3ff) | 0xdc00) as u16
149}
150
151macro_rules! trie_unwrap {
155 ($option:expr) => {
156 match $option {
157 Some(x) => x,
158 None => {
159 debug_assert!(false);
161 return TrieResult::NoMatch;
162 }
163 }
164 };
165}
166
167impl<'a> Char16TrieIterator<'a> {
168 #[inline]
170 pub fn new(trie: &'a ZeroSlice<u16>) -> Self {
171 Self {
172 trie,
173 pos: Some(0),
174 remaining_match_length: None,
175 }
176 }
177
178 pub fn next(&mut self, c: char) -> TrieResult {
199 if (c as u32) <= 0xffff {
200 self.next16(c as u16)
201 } else {
202 match self.next16(u16_lead(c as i32)) {
203 TrieResult::NoValue | TrieResult::Intermediate(_) => {
204 self.next16(u16_tail(c as i32))
205 }
206 _ => TrieResult::NoMatch,
207 }
208 }
209 }
210
211 pub fn next32(&mut self, c: u32) -> TrieResult {
232 if c <= 0xffff {
233 self.next16(c as u16)
234 } else {
235 match self.next16(u16_lead(c as i32)) {
236 TrieResult::NoValue | TrieResult::Intermediate(_) => {
237 self.next16(u16_tail(c as i32))
238 }
239 _ => TrieResult::NoMatch,
240 }
241 }
242 }
243
244 pub fn next16(&mut self, c: u16) -> TrieResult {
265 let mut pos = match self.pos {
266 Some(p) => p,
267 None => return TrieResult::NoMatch,
268 };
269 if let Some(length) = self.remaining_match_length {
270 if c == match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) {
272 pos += 1;
273 self.pos = Some(pos);
274 if length == 0 {
275 self.remaining_match_length = None;
276 let node = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos));
277 if node >= MIN_VALUE_LEAD {
278 return self.value_result(pos);
279 }
280 } else {
281 self.remaining_match_length = Some(length - 1);
282 }
283 return TrieResult::NoValue;
284 }
285 self.stop();
286 TrieResult::NoMatch
287 } else {
288 self.next_impl(pos, c)
289 }
290 }
291
292 fn branch_next(&mut self, pos: usize, length: usize, in_unit: u16) -> TrieResult {
293 let mut pos = pos;
294 let mut length = length;
295 if length == 0 {
296 length = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) as usize;
297 pos += 1;
298 }
299 length += 1;
300
301 while length > MAX_BRANCH_LINEAR_SUB_NODE_LENGTH {
304 if in_unit < match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) {
305 length >>= 1;
306 pos = match self.jump_by_delta(pos + 1) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.jump_by_delta(pos + 1));
307 } else {
308 length = length - (length >> 1);
309 pos = match self.skip_delta(pos + 1) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.skip_delta(pos + 1));
310 }
311 }
312 loop {
316 if in_unit == match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) {
317 pos += 1;
318 let mut node = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos));
319 if node & VALUE_IS_FINAL != 0 {
320 self.pos = Some(pos);
321 return self.value_result(pos);
322 }
323 pos += 1;
325
326 if node < MIN_TWO_UNIT_VALUE_LEAD {
327 pos += node as usize;
328 } else if node < THREE_UNIT_VALUE_LEAD {
329 pos += (((node - MIN_TWO_UNIT_VALUE_LEAD) as u32) << 16) as usize
330 | match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) as usize;
331 pos += 1;
332 } else {
333 pos += ((match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) as usize) << 16)
334 | match self.trie.get(pos + 1) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos + 1)) as usize;
335 pos += 2;
336 }
337 node = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos));
338 self.pos = Some(pos);
339
340 if node >= MIN_VALUE_LEAD {
341 return self.value_result(pos);
342 }
343 return TrieResult::NoValue;
344 }
345 length -= 1;
346 pos = match self.skip_value(pos + 1) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.skip_value(pos + 1));
347 if length <= 1 {
348 break;
349 }
350 }
351
352 if in_unit == match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) {
353 pos += 1;
354 self.pos = Some(pos);
355 let node = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos));
356 if node >= MIN_VALUE_LEAD {
357 return self.value_result(pos);
358 }
359 TrieResult::NoValue
360 } else {
361 self.stop();
362 TrieResult::NoMatch
363 }
364 }
365
366 fn next_impl(&mut self, pos: usize, in_unit: u16) -> TrieResult {
367 let mut node = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos));
368 let mut pos = pos + 1;
369 loop {
370 if node < MIN_LINEAR_MATCH {
371 return self.branch_next(pos, node as usize, in_unit);
372 } else if node < MIN_VALUE_LEAD {
373 let length = node - MIN_LINEAR_MATCH;
375 if in_unit == match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos)) {
376 pos += 1;
377 if length == 0 {
378 self.remaining_match_length = None;
379 self.pos = Some(pos);
380 node = match self.trie.get(pos) {
Some(x) => x,
None => {
if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};
return TrieResult::NoMatch;
}
}trie_unwrap!(self.trie.get(pos));
381 if node >= MIN_VALUE_LEAD {
382 return self.value_result(pos);
383 }
384 return TrieResult::NoValue;
385 }
386 self.remaining_match_length = Some(length as usize - 1);
387 self.pos = Some(pos);
388 return TrieResult::NoValue;
389 }
390 break;
392 } else if (node & VALUE_IS_FINAL) != 0 {
393 break;
395 } else {
396 pos = skip_node_value(pos, node);
398 node &= NODE_TYPE_MASK;
399 }
400 }
401 self.stop();
402 TrieResult::NoMatch
403 }
404
405 fn stop(&mut self) {
406 self.pos = None;
407 }
408
409 #[inline(always)] fn jump_by_delta(&self, pos: usize) -> Option<usize> {
411 let delta = self.trie.get(pos)?;
412 let v = if delta < MIN_TWO_UNIT_DELTA_LEAD {
413 pos + 1 + delta as usize
415 } else if delta == THREE_UNIT_DELTA_LEAD {
416 let delta =
417 ((self.trie.get(pos + 1)? as usize) << 16) | (self.trie.get(pos + 2)? as usize);
418 pos + delta + 3
419 } else {
420 let delta = (((delta - MIN_TWO_UNIT_DELTA_LEAD) as usize) << 16)
421 | (self.trie.get(pos + 1)? as usize);
422 pos + delta + 2
423 };
424 Some(v)
425 }
426
427 #[inline(always)] fn skip_value(&self, pos: usize) -> Option<usize> {
429 let lead_unit = self.trie.get(pos)?;
430 Some(skip_value(pos + 1, lead_unit & 0x7fff))
431 }
432
433 #[inline(always)] fn skip_delta(&self, pos: usize) -> Option<usize> {
435 let delta = self.trie.get(pos)?;
436 let v = if delta < MIN_TWO_UNIT_DELTA_LEAD {
437 pos + 1
438 } else if delta == THREE_UNIT_DELTA_LEAD {
439 pos + 3
440 } else {
441 pos + 2
442 };
443 Some(v)
444 }
445
446 fn value_result(&self, pos: usize) -> TrieResult {
447 match self.get_value(pos) {
448 Some(result) => result,
449 None => {
450 if true {
if !false { ::core::panicking::panic("assertion failed: false") };
};debug_assert!(false);
452 TrieResult::NoMatch
453 }
454 }
455 }
456
457 #[inline(always)] fn get_value(&self, pos: usize) -> Option<TrieResult> {
459 let lead_unit = self.trie.get(pos)?;
460 if lead_unit & VALUE_IS_FINAL == VALUE_IS_FINAL {
461 self.read_value(pos + 1, lead_unit & 0x7fff)
462 .map(TrieResult::FinalValue)
463 } else {
464 self.read_node_value(pos + 1, lead_unit)
465 .map(TrieResult::Intermediate)
466 }
467 }
468
469 #[inline(always)] fn read_value(&self, pos: usize, lead_unit: u16) -> Option<i32> {
471 let v = if lead_unit < MIN_TWO_UNIT_VALUE_LEAD {
472 lead_unit.into()
473 } else if lead_unit < THREE_UNIT_VALUE_LEAD {
474 (((lead_unit - MIN_TWO_UNIT_VALUE_LEAD) as i32) << 16) | self.trie.get(pos)? as i32
475 } else {
476 ((self.trie.get(pos)? as i32) << 16) | self.trie.get(pos + 1)? as i32
477 };
478 Some(v)
479 }
480
481 #[inline(always)] fn read_node_value(&self, pos: usize, lead_unit: u16) -> Option<i32> {
483 let v = if lead_unit < (MIN_TWO_UNIT_NODE_VALUE_LEAD) {
484 ((lead_unit >> 6) - 1).into()
485 } else if lead_unit < THREE_UNIT_NODE_VALUE_LEAD {
486 ((((lead_unit & 0x7fc0) - MIN_TWO_UNIT_NODE_VALUE_LEAD) as i32) << 10)
487 | self.trie.get(pos)? as i32
488 } else {
489 ((self.trie.get(pos)? as i32) << 16) | self.trie.get(pos + 1)? as i32
490 };
491 Some(v)
492 }
493}