daachorse/bytewise.rs
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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
//! A byte-wise version of the Double-Array Aho-Corasick.
mod builder;
pub mod iter;
use core::mem;
use core::num::NonZeroU32;
use alloc::vec::Vec;
use crate::build_helper::BuildHelper;
use crate::errors::{DaachorseError, Result};
use crate::intpack::{U24nU8, U24};
use crate::serializer::{Serializable, SerializableVec};
use crate::utils::FromU32;
use crate::{MatchKind, Output};
pub use builder::DoubleArrayAhoCorasickBuilder;
use iter::{
FindIterator, FindOverlappingIterator, FindOverlappingNoSuffixIterator, LestmostFindIterator,
U8SliceIterator,
};
// The root index position.
const ROOT_STATE_IDX: u32 = 0;
// The dead index position.
const DEAD_STATE_IDX: u32 = 1;
/// A fast multiple pattern match automaton implemented with the Aho-Corasick algorithm and compact
/// double-array data structure.
///
/// [`DoubleArrayAhoCorasick`] implements a pattern match automaton based on the
/// [Aho-Corasick algorithm](https://dl.acm.org/doi/10.1145/360825.360855), supporting linear-time
/// pattern matching. The internal data structure employs the
/// [compact double-array structure](https://doi.org/10.1016/j.ipm.2006.04.004), the fastest
/// trie representation technique. It supports constant-time state-to-state traversal, allowing for
/// very fast pattern matching. Moreover, each state is represented in the space of only 12 bytes.
///
/// # Build instructions
///
/// [`DoubleArrayAhoCorasick`] supports the following two types of input data:
///
/// - [`DoubleArrayAhoCorasick::new`] builds an automaton from a set of byte strings while
/// assigning unique identifiers in the input order.
///
/// - [`DoubleArrayAhoCorasick::with_values`] builds an automaton from a set of pairs of a byte
/// string and a user-defined value.
///
/// # Limitations
///
/// The maximum number of patterns is limited to 2^24-1. If a larger number of patterns is given,
/// [`DaachorseError`](super::errors::DaachorseError) will be reported.
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct DoubleArrayAhoCorasick<V> {
states: Vec<State>,
outputs: Vec<Output<V>>,
match_kind: MatchKind,
num_states: u32,
}
impl<V> DoubleArrayAhoCorasick<V> {
/// Creates a new [`DoubleArrayAhoCorasick`] from input patterns. The value `i` is
/// automatically associated with `patterns[i]`.
///
/// # Arguments
///
/// * `patterns` - List of patterns.
///
/// # Errors
///
/// [`DaachorseError`](super::errors::DaachorseError) is returned when
/// - `patterns` is empty,
/// - `patterns` contains entries of length zero,
/// - `patterns` contains duplicate entries,
/// - the conversion from the index `i` to the specified type `V` fails,
/// - the scale of `patterns` exceeds the expected one, or
/// - the scale of the resulting automaton exceeds the expected one.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let mut it = pma.find_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn new<I, P>(patterns: I) -> Result<Self>
where
I: IntoIterator<Item = P>,
P: AsRef<[u8]>,
V: Copy + TryFrom<usize>,
{
DoubleArrayAhoCorasickBuilder::new().build(patterns)
}
/// Creates a new [`DoubleArrayAhoCorasick`] from input pattern-value pairs.
///
/// # Arguments
///
/// * `patvals` - List of pattern-value pairs.
///
/// # Errors
///
/// [`DaachorseError`](super::errors::DaachorseError) is returned when
/// - `patvals` is empty,
/// - `patvals` contains patterns of length zero,
/// - `patvals` contains duplicate patterns,
/// - the scale of `patvals` exceeds the expected one, or
/// - the scale of the resulting automaton exceeds the expected one.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patvals = vec![("bcd", 0), ("ab", 1), ("a", 2), ("e", 1)];
/// let pma = DoubleArrayAhoCorasick::with_values(patvals).unwrap();
///
/// let mut it = pma.find_iter("abcde");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((4, 5, 1), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn with_values<I, P>(patvals: I) -> Result<Self>
where
I: IntoIterator<Item = (P, V)>,
P: AsRef<[u8]>,
V: Copy,
{
DoubleArrayAhoCorasickBuilder::new().build_with_values(patvals)
}
/// Returns an iterator of non-overlapping matches in the given haystack.
///
/// # Arguments
///
/// * `haystack` - String to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::Standard`] in the construction, the iterator is not
/// supported and the function will panic.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let mut it = pma.find_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn find_iter<P>(&self, haystack: P) -> FindIterator<U8SliceIterator<P>, V>
where
P: AsRef<[u8]>,
{
assert!(
self.match_kind.is_standard(),
"Error: match_kind must be standard."
);
FindIterator {
pma: self,
haystack: U8SliceIterator::new(haystack).enumerate(),
}
}
/// Returns an iterator of non-overlapping matches in the given haystack iterator.
///
/// # Arguments
///
/// * `haystack` - [`u8`] iterator to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::Standard`] in the construction, the iterator is not
/// supported and the function will panic.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let haystack = "ab".as_bytes().iter().chain("cd".as_bytes()).copied();
///
/// let mut it = pma.find_iter_from_iter(haystack);
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn find_iter_from_iter<P>(&self, haystack: P) -> FindIterator<P, V>
where
P: Iterator<Item = u8>,
{
assert!(
self.match_kind.is_standard(),
"Error: match_kind must be standard."
);
FindIterator {
pma: self,
haystack: haystack.enumerate(),
}
}
/// Returns an iterator of overlapping matches in the given haystack.
///
/// # Arguments
///
/// * `haystack` - String to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::Standard`] in the construction, the iterator is not
/// supported and the function will panic.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let mut it = pma.find_overlapping_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 2, 1), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn find_overlapping_iter<P>(
&self,
haystack: P,
) -> FindOverlappingIterator<U8SliceIterator<P>, V>
where
P: AsRef<[u8]>,
{
assert!(
self.match_kind.is_standard(),
"Error: match_kind must be standard."
);
FindOverlappingIterator {
pma: self,
haystack: U8SliceIterator::new(haystack).enumerate(),
state_id: ROOT_STATE_IDX,
output_pos: None,
pos: 0,
}
}
/// Returns an iterator of overlapping matches in the given haystack iterator.
///
/// # Arguments
///
/// * `haystack` - [`u8`] iterator to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::Standard`] in the construction, the iterator is not
/// supported and the function will panic.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let haystack = "ab".as_bytes().iter().chain("cd".as_bytes()).copied();
///
/// let mut it = pma.find_overlapping_iter_from_iter(haystack);
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 2, 1), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn find_overlapping_iter_from_iter<P>(&self, haystack: P) -> FindOverlappingIterator<P, V>
where
P: Iterator<Item = u8>,
{
assert!(
self.match_kind.is_standard(),
"Error: match_kind must be standard."
);
FindOverlappingIterator {
pma: self,
haystack: haystack.enumerate(),
state_id: ROOT_STATE_IDX,
output_pos: None,
pos: 0,
}
}
/// Returns an iterator of overlapping matches without suffixes in the given haystack iterator.
///
/// The Aho-Corasick algorithm reads through the haystack from left to right and reports
/// matches when it reaches the end of each pattern. In the overlapping match, more than one
/// pattern can be returned per report.
///
/// This iterator returns the first match on each report.
///
/// # Arguments
///
/// * `haystack` - String to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::Standard`] in the construction, the iterator is not
/// supported and the function will panic.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "cd", "abc"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let mut it = pma.find_overlapping_no_suffix_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 3, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn find_overlapping_no_suffix_iter<P>(
&self,
haystack: P,
) -> FindOverlappingNoSuffixIterator<U8SliceIterator<P>, V>
where
P: AsRef<[u8]>,
{
assert!(
self.match_kind.is_standard(),
"Error: match_kind must be standard."
);
FindOverlappingNoSuffixIterator {
pma: self,
haystack: U8SliceIterator::new(haystack).enumerate(),
state_id: ROOT_STATE_IDX,
}
}
/// Returns an iterator of overlapping matches without suffixes in the given haystack iterator.
///
/// The Aho-Corasick algorithm reads through the haystack from left to right and reports
/// matches when it reaches the end of each pattern. In the overlapping match, more than one
/// pattern can be returned per report.
///
/// This iterator returns the first match on each report.
///
/// # Arguments
///
/// * `haystack` - [`u8`] to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::Standard`] in the construction, the iterator is not
/// supported and the function will panic.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "cd", "abc"];
/// let pma = DoubleArrayAhoCorasick::new(patterns).unwrap();
///
/// let haystack = "ab".as_bytes().iter().chain("cd".as_bytes()).copied();
///
/// let mut it = pma.find_overlapping_no_suffix_iter_from_iter(haystack);
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 3, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn find_overlapping_no_suffix_iter_from_iter<P>(
&self,
haystack: P,
) -> FindOverlappingNoSuffixIterator<P, V>
where
P: Iterator<Item = u8>,
{
assert!(
self.match_kind.is_standard(),
"Error: match_kind must be standard."
);
FindOverlappingNoSuffixIterator {
pma: self,
haystack: haystack.enumerate(),
state_id: ROOT_STATE_IDX,
}
}
/// Returns an iterator of leftmost matches in the given haystack.
///
/// The leftmost match greedily searches the longest possible match at each iteration, and
/// the match results do not overlap positionally such as
/// [`DoubleArrayAhoCorasick::find_iter()`].
///
/// According to the [`MatchKind`] option you specified in the construction, the behavior is
/// changed for multiple possible matches, as follows.
///
/// - If you set [`MatchKind::LeftmostLongest`], it reports the match
/// corresponding to the longest pattern.
///
/// - If you set [`MatchKind::LeftmostFirst`], it reports the match
/// corresponding to the pattern earlier registered to the automaton.
///
/// # Arguments
///
/// * `haystack` - String to search for.
///
/// # Panics
///
/// If you do not specify [`MatchKind::LeftmostFirst`] or [`MatchKind::LeftmostLongest`] in
/// the construction, the iterator is not supported and the function will panic.
///
/// # Examples
///
/// ## LeftmostLongest
///
/// ```
/// use daachorse::{DoubleArrayAhoCorasickBuilder, MatchKind};
///
/// let patterns = vec!["ab", "a", "abcd"];
/// let pma = DoubleArrayAhoCorasickBuilder::new()
/// .match_kind(MatchKind::LeftmostLongest)
/// .build(&patterns)
/// .unwrap();
///
/// let mut it = pma.leftmost_find_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 4, 2), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
///
/// ## LeftmostFirst
///
/// ```
/// use daachorse::{DoubleArrayAhoCorasickBuilder, MatchKind};
///
/// let patterns = vec!["ab", "a", "abcd"];
/// let pma = DoubleArrayAhoCorasickBuilder::new()
/// .match_kind(MatchKind::LeftmostFirst)
/// .build(&patterns)
/// .unwrap();
///
/// let mut it = pma.leftmost_find_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 2, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
pub fn leftmost_find_iter<P>(&self, haystack: P) -> LestmostFindIterator<P, V>
where
P: AsRef<[u8]>,
{
assert!(
self.match_kind.is_leftmost(),
"Error: match_kind must be leftmost."
);
LestmostFindIterator {
pma: self,
haystack,
pos: 0,
}
}
/// Returns the total amount of heap used by this automaton in bytes.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
///
/// assert_eq!(3108, pma.heap_bytes());
/// ```
#[must_use]
pub fn heap_bytes(&self) -> usize {
self.states.len() * mem::size_of::<State>()
+ self.outputs.len() * mem::size_of::<Output<V>>()
}
/// Returns the total number of states this automaton has.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::<usize>::new(patterns).unwrap();
///
/// assert_eq!(pma.num_states(), 6);
/// ```
#[must_use]
pub fn num_states(&self) -> usize {
usize::from_u32(self.num_states)
}
/// Serializes the automaton into a [`Vec`].
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
/// let bytes = pma.serialize();
/// ```
#[must_use]
pub fn serialize(&self) -> Vec<u8>
where
V: Serializable,
{
let mut result = Vec::with_capacity(
self.states.serialized_bytes()
+ self.outputs.serialized_bytes()
+ MatchKind::serialized_bytes()
+ u32::serialized_bytes(),
);
self.states.serialize_to_vec(&mut result);
self.outputs.serialize_to_vec(&mut result);
self.match_kind.serialize_to_vec(&mut result);
self.num_states.serialize_to_vec(&mut result);
result
}
/// Deserializes the automaton from a given slice.
///
/// # Arguments
///
/// * `source` - A source slice.
///
/// # Returns
///
/// A tuple of the automaton and the slice not used for the deserialization.
///
/// # Safety
///
/// The given data must be a correct automaton exported by
/// [`DoubleArrayAhoCorasick::serialize()`] function.
///
/// # Examples
///
/// ```
/// use daachorse::DoubleArrayAhoCorasick;
///
/// let patterns = vec!["bcd", "ab", "a"];
/// let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
/// let bytes = pma.serialize();
///
/// let (pma, _) = unsafe { DoubleArrayAhoCorasick::<u32>::deserialize_unchecked(&bytes) };
///
/// let mut it = pma.find_overlapping_iter("abcd");
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 1, 2), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((0, 2, 1), (m.start(), m.end(), m.value()));
///
/// let m = it.next().unwrap();
/// assert_eq!((1, 4, 0), (m.start(), m.end(), m.value()));
///
/// assert_eq!(None, it.next());
/// ```
#[must_use]
pub unsafe fn deserialize_unchecked(source: &[u8]) -> (Self, &[u8])
where
V: Serializable,
{
let (states, source) = Vec::<State>::deserialize_from_slice(source);
let (outputs, source) = Vec::<Output<V>>::deserialize_from_slice(source);
let (match_kind, source) = MatchKind::deserialize_from_slice(source);
let (num_states, source) = u32::deserialize_from_slice(source);
(
Self {
states,
outputs,
match_kind,
num_states,
},
source,
)
}
/// # Safety
///
/// `state_id` must be smaller than the length of states.
#[inline(always)]
unsafe fn child_index_unchecked(&self, state_id: u32, c: u8) -> Option<u32> {
// child_idx is always smaller than states.len() because
// - states.len() is 256 * k for some integer k, and
// - base() returns smaller than states.len() when it is Some.
self.states
.get_unchecked(usize::from_u32(state_id))
.base()
.and_then(|base| {
let child_idx = base.get() ^ u32::from(c);
Some(child_idx)
.filter(|&x| self.states.get_unchecked(usize::from_u32(x)).check() == c)
})
}
/// # Safety
///
/// `state_id` must be smaller than the length of states.
#[inline(always)]
unsafe fn next_state_id_unchecked(&self, mut state_id: u32, c: u8) -> u32 {
// In the loop, state_id is always set to values smaller than states.len(),
// because child_index_unchecked() and fail() return such values.
loop {
if let Some(state_id) = self.child_index_unchecked(state_id, c) {
return state_id;
}
if state_id == ROOT_STATE_IDX {
return ROOT_STATE_IDX;
}
state_id = self.states.get_unchecked(usize::from_u32(state_id)).fail();
}
}
/// # Safety
///
/// `state_id` must be smaller than the length of states.
#[inline(always)]
unsafe fn next_state_id_leftmost_unchecked(&self, mut state_id: u32, c: u8) -> u32 {
// In the loop, state_id is always set to values smaller than states.len(),
// because child_index_unchecked() and fail() return such values.
loop {
if let Some(state_id) = self.child_index_unchecked(state_id, c) {
return state_id;
}
if state_id == ROOT_STATE_IDX {
return ROOT_STATE_IDX;
}
let fail_id = self.states.get_unchecked(usize::from_u32(state_id)).fail();
if fail_id == DEAD_STATE_IDX {
return ROOT_STATE_IDX;
}
state_id = fail_id;
}
}
}
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq)]
struct State {
base: Option<NonZeroU32>,
fail: u32,
// 3 bytes for output_pos and 1 byte for check.
opos_ch: U24nU8,
}
impl State {
#[inline(always)]
pub const fn base(&self) -> Option<NonZeroU32> {
self.base
}
#[inline(always)]
pub fn check(&self) -> u8 {
self.opos_ch.b()
}
#[inline(always)]
pub const fn fail(&self) -> u32 {
self.fail
}
#[inline(always)]
pub const fn output_pos(&self) -> Option<NonZeroU32> {
NonZeroU32::new(self.opos_ch.a().get())
}
#[inline(always)]
pub fn set_base(&mut self, x: NonZeroU32) {
self.base = Some(x);
}
#[inline(always)]
pub fn set_check(&mut self, x: u8) {
self.opos_ch.set_b(x);
}
#[inline(always)]
pub fn set_fail(&mut self, x: u32) {
self.fail = x;
}
#[inline(always)]
pub fn set_output_pos(&mut self, x: Option<NonZeroU32>) -> Result<()> {
let x = x.map_or(0, NonZeroU32::get);
if let Ok(x) = U24::try_from(x) {
self.opos_ch.set_a(x);
Ok(())
} else {
Err(DaachorseError::automaton_scale("output_pos", U24::MAX))
}
}
}
impl Serializable for State {
#[inline(always)]
fn serialize_to_vec(&self, dst: &mut Vec<u8>) {
self.base.serialize_to_vec(dst);
self.fail.serialize_to_vec(dst);
self.opos_ch.serialize_to_vec(dst);
}
#[inline(always)]
fn deserialize_from_slice(src: &[u8]) -> (Self, &[u8]) {
let (base, src) = Option::<NonZeroU32>::deserialize_from_slice(src);
let (fail, src) = u32::deserialize_from_slice(src);
let (opos_ch, src) = U24nU8::deserialize_from_slice(src);
(
Self {
base,
fail,
opos_ch,
},
src,
)
}
#[inline(always)]
fn serialized_bytes() -> usize {
Option::<NonZeroU32>::serialized_bytes()
+ u32::serialized_bytes()
+ U24nU8::serialized_bytes()
}
}
impl core::fmt::Debug for State {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("State")
.field("base", &self.base())
.field("check", &self.check())
.field("fail", &self.fail())
.field("output_pos", &self.output_pos())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_double_array() {
/*
* a--> 4
* /
* a--> 1 --c--> 5
* /
* 0 --b--> 2 --c--> 6
* \
* c--> 3
*
* a = 0
* b = 1
* c = 2
*/
let patterns = vec![vec![0, 0], vec![0, 2], vec![1, 2], vec![2]];
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
let base_expected = vec![
NonZeroU32::new(4), // 0 (state=0)
None, // 1 (reserved)
None, // 2
None, // 3 (state=6)
NonZeroU32::new(8), // 4 (state=1)
NonZeroU32::new(1), // 5 (state=2)
None, // 6 (state=3)
None, // 7
None, // 8 (state=4)
None, // 9
None, // 10 (state=5)
];
let check_expected = vec![
0, // 0 (state=0)
1, // 1
2, // 2
2, // 3 (state=6)
0, // 4 (state=1)
1, // 5 (state=2)
2, // 6 (state=3)
7, // 7
0, // 8 (state=4)
9, // 9
2, // 10 (state=5)
];
let fail_expected = vec![
ROOT_STATE_IDX, // 0 (state=0)
ROOT_STATE_IDX, // 1 (reserved)
ROOT_STATE_IDX, // 2
6, // 3 (state=6)
ROOT_STATE_IDX, // 4 (state=1)
ROOT_STATE_IDX, // 5 (state=2)
ROOT_STATE_IDX, // 6 (state=3)
ROOT_STATE_IDX, // 7
4, // 8 (state=4)
ROOT_STATE_IDX, // 9
6, // 10 (state=5)
];
let pma_base: Vec<_> = pma.states[0..11].iter().map(|state| state.base()).collect();
let pma_check: Vec<_> = pma.states[0..11]
.iter()
.map(|state| state.check())
.collect();
let pma_fail: Vec<_> = pma.states[0..11].iter().map(|state| state.fail()).collect();
assert_eq!(base_expected, pma_base);
assert_eq!(check_expected, pma_check);
assert_eq!(fail_expected, pma_fail);
}
#[test]
fn test_num_states() {
/*
* b-*-a-*-a-*-b-*-a-*
* /
* *-a-*-b-*-b-*-a-*
* \
* a-*-b-*-a-*
*/
let patterns = vec!["abba", "baaba", "ababa"];
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
assert_eq!(13, pma.num_states());
}
#[test]
fn test_input_order() {
let patvals_sorted = vec![("ababa", 0), ("abba", 1), ("baaba", 2)];
let patvals_unsorted = vec![("abba", 1), ("baaba", 2), ("ababa", 0)];
let pma_sorted = DoubleArrayAhoCorasick::with_values(patvals_sorted).unwrap();
let pma_unsorted = DoubleArrayAhoCorasick::with_values(patvals_unsorted).unwrap();
assert_eq!(pma_sorted.states, pma_unsorted.states);
assert_eq!(pma_sorted.outputs, pma_unsorted.outputs);
}
#[test]
fn test_n_blocks_1_1() {
let mut patterns = vec![];
// state 0: reserved for the root state
// state 1: reserved for the dead state
// base = 0xfe; fills 0x02..=0xff
for i in 0x00..=0xfd {
let pattern = vec![i];
patterns.push(pattern);
}
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
assert_eq!(255, pma.num_states());
assert_eq!(256, pma.states.len());
assert_eq!(0xfe, pma.states[0].base().unwrap().get());
}
#[test]
fn test_n_blocks_1_2() {
let mut patterns = vec![];
// state 0: reserved for the root state
// state 1: reserved for the dead state
// base = 0x100; fills 0x100, 0x102, 0x104..=0x1ff
patterns.push(vec![0x00]);
patterns.push(vec![0x02]);
for i in 0x04..=0xff {
patterns.push(vec![i]);
}
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
assert_eq!(255, pma.num_states());
assert_eq!(512, pma.states.len());
assert_eq!(0x100, pma.states[0].base().unwrap().get());
}
#[test]
fn test_n_blocks_2_1() {
let mut patterns = vec![];
// state 0: reserved for the root state
// state 1: reserved for the dead state
// base = 0x80; fills 0x80..=0xff
for i in 0x00..=0x7f {
let pattern = vec![i];
patterns.push(pattern);
}
// base = 0x7e; fills 0x02..=0x7f
for i in 0x00..=0x7d {
let pattern = vec![0x00, i];
patterns.push(pattern);
}
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
assert_eq!(255, pma.num_states());
assert_eq!(256, pma.states.len());
assert_eq!(0x80, pma.states[0].base().unwrap().get());
assert_eq!(0x7e, pma.states[0x80].base().unwrap().get());
}
#[test]
fn test_n_blocks_2_2() {
let mut patterns = vec![];
// state 0: reserved for the root state
// state 1: reserved for the dead state
// base = 0x80; fills 0x80..=0xff
for i in 0..=0x7f {
let pattern = vec![i];
patterns.push(pattern);
}
// base = 0x100; fills 0x100, 0x102, 0x104..=0x17f
patterns.push(vec![0, 0x00]);
patterns.push(vec![0, 0x02]);
for i in 0x04..=0x7f {
let pattern = vec![0x00, i];
patterns.push(pattern);
}
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
assert_eq!(255, pma.num_states());
assert_eq!(512, pma.states.len());
assert_eq!(0x80, pma.states[0].base().unwrap().get());
assert_eq!(0x100, pma.states[0x80].base().unwrap().get());
}
#[test]
fn test_serialize_state() {
let mut opos_ch = U24nU8::default();
opos_ch.set_a(U24::try_from(57).unwrap());
opos_ch.set_b(77);
let x = State {
base: NonZeroU32::new(42),
fail: 13,
opos_ch,
};
let mut data = vec![];
x.serialize_to_vec(&mut data);
assert_eq!(data.len(), State::serialized_bytes());
let (y, rest) = State::deserialize_from_slice(&data);
assert!(rest.is_empty());
assert_eq!(x, y);
}
#[test]
fn test_serialize_pma() {
let patterns = vec!["abba", "baaba", "ababa"];
let pma = DoubleArrayAhoCorasick::<u32>::new(patterns).unwrap();
let bytes = pma.serialize();
let (other, rest) = unsafe { DoubleArrayAhoCorasick::deserialize_unchecked(&bytes) };
assert!(rest.is_empty());
assert_eq!(pma.states, other.states);
assert_eq!(pma.outputs, other.outputs);
assert_eq!(pma.match_kind, other.match_kind);
assert_eq!(pma.num_states, other.num_states);
}
}