daggrs 0.1.0

A fast Double-Array Aho-Corasick implementation for multi-pattern matching
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
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
use super::{DoubleArrayAhoCorasick, ROOT_IDX};
use crate::trie::DEAD_STATE;
use crate::types::{Match, MatchKind, Output};

/// Iterator over matches found in text using the double-array automaton.
pub struct FindIter<'a> {
    daac: &'a DoubleArrayAhoCorasick,
    text: &'a [u8],
    pos: usize,
    state: u32,
    outpos: u32,
}

impl<'a> FindIter<'a> {
    pub(super) fn new(daac: &'a DoubleArrayAhoCorasick, text: &'a [u8]) -> Self {
        Self {
            daac,
            text,
            pos: 0,
            state: ROOT_IDX,
            outpos: u32::MAX,
        }
    }

    /// Transitions to the next state, following failure links as needed.
    /// Returns (next_state, outpos) where outpos is u32::MAX if no output.
    #[inline(always)]
    fn next_state_leftmost(&self, mut state: u32, byte: u8) -> (u32, u32) {
        let states = &self.daac.states;
        let states_len = states.len();

        loop {
            // Load current state once, use all fields
            let current = &states[state as usize];
            let child = current.base ^ (byte as u32);

            // Check if transition is valid
            if (child as usize) < states_len {
                let child_state = &states[child as usize];
                if child_state.check == state {
                    return (child, child_state.outpos);
                }
            }

            // No valid transition, follow failure link
            if state == ROOT_IDX {
                return (ROOT_IDX, u32::MAX);
            }

            let fail = current.fail;
            if fail == DEAD_STATE {
                return (ROOT_IDX, u32::MAX);
            }
            state = fail;
        }
    }

    #[inline]
    fn next_overlapping(&mut self) -> Option<Match> {
        // Yield pending outputs from output chain
        if self.outpos != u32::MAX {
            let output = &self.daac.outputs[self.outpos as usize];
            self.outpos = output.parent;
            return Some(Match {
                pattern_id: output.pattern_id,
                start: self.pos - output.length as usize,
                end: self.pos,
            });
        }

        let states = &self.daac.states;
        let states_len = states.len();

        // Process input bytes
        while self.pos < self.text.len() {
            let byte = self.text[self.pos];
            self.pos += 1;

            // Follow failure links until we find a transition or reach root
            loop {
                let current = &states[self.state as usize];
                let child = current.base ^ (byte as u32);

                // Check if transition is valid
                if (child as usize) < states_len && states[child as usize].check == self.state {
                    self.state = child;
                    break;
                }

                if self.state == ROOT_IDX {
                    break;
                }
                self.state = current.fail;
            }

            // Check for outputs
            self.outpos = states[self.state as usize].outpos;
            if self.outpos != u32::MAX {
                let output = &self.daac.outputs[self.outpos as usize];
                self.outpos = output.parent;
                return Some(Match {
                    pattern_id: output.pattern_id,
                    start: self.pos - output.length as usize,
                    end: self.pos,
                });
            }
        }

        None
    }

    #[inline(always)]
    fn next_leftmost(&mut self) -> Option<Match> {
        let mut state = ROOT_IDX;
        let mut last_outpos: u32 = u32::MAX;
        let text = self.text;
        let mut pos = self.pos;

        while pos < text.len() {
            let byte = text[pos];
            let (next_state, outpos) = self.next_state_leftmost(state, byte);
            pos += 1;

            if next_state == ROOT_IDX {
                if last_outpos != u32::MAX {
                    let output = &self.daac.outputs[last_outpos as usize];
                    return Some(Match {
                        pattern_id: output.pattern_id,
                        start: self.pos - output.length as usize,
                        end: self.pos,
                    });
                }
                state = ROOT_IDX;
            } else {
                state = next_state;
                if outpos != u32::MAX {
                    last_outpos = outpos;
                    self.pos = pos;
                }
            }
        }

        // Yield any remaining match at end of text
        if last_outpos != u32::MAX {
            let output = &self.daac.outputs[last_outpos as usize];
            return Some(Match {
                pattern_id: output.pattern_id,
                start: self.pos - output.length as usize,
                end: self.pos,
            });
        }

        None
    }
}

impl<'a> Iterator for FindIter<'a> {
    type Item = Match;

    #[inline]
    fn next(&mut self) -> Option<Match> {
        match self.daac.match_kind {
            MatchKind::Overlapping => self.next_overlapping(),
            MatchKind::LeftmostFirst
            | MatchKind::LeftmostLongest
            | MatchKind::WordPiece => self.next_leftmost(),
        }
    }
}

/// Iterator over all outputs (pattern matches) at a given state.
///
/// This walks the output chain via parent links, yielding each output
/// that matches at this state. Used for BPE tokenization where we need
/// all tokens ending at each position.
pub struct OutputIter<'a> {
    outputs: &'a [Output],
    outpos: u32,
}

impl<'a> OutputIter<'a> {
    pub(super) fn new(outputs: &'a [Output], outpos: u32) -> Self {
        Self { outputs, outpos }
    }
}

impl<'a> Iterator for OutputIter<'a> {
    type Item = &'a Output;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.outpos == u32::MAX {
            return None;
        }

        let output = &self.outputs[self.outpos as usize];
        self.outpos = output.parent;
        Some(output)
    }
}

#[cfg(test)]
mod tests {
    use super::super::DoubleArrayAhoCorasick;
    use crate::trie::Trie;
    use crate::types::{Match, MatchKind};

    #[test]
    fn test_find_single_pattern() {
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        let matches = daac.find(b"she");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].pattern_id, 0);
        assert_eq!(matches[0].start, 1);
        assert_eq!(matches[0].end, 3);
    }

    #[test]
    fn test_find_multiple_patterns() {
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.add(b"she", 1);
        trie.add(b"hers", 2);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // "ushers" should find: "she" at 1..4, "he" at 2..4, "hers" at 2..6
        let matches = daac.find(b"ushers");
        assert_eq!(matches.len(), 3);

        let match_tuples: Vec<(u32, usize, usize)> = matches
            .iter()
            .map(|m| (m.pattern_id, m.start, m.end))
            .collect();

        assert!(match_tuples.contains(&(1, 1, 4))); // "she"
        assert!(match_tuples.contains(&(0, 2, 4))); // "he"
        assert!(match_tuples.contains(&(2, 2, 6))); // "hers"
    }

    #[test]
    fn test_find_no_match() {
        let mut trie = Trie::new();
        trie.add(b"xyz", 0);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        let matches = daac.find(b"abc");
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_find_empty_text() {
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        let matches = daac.find(b"");
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_find_at_start() {
        let mut trie = Trie::new();
        trie.add(b"hello", 0);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        let matches = daac.find(b"hello world");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].start, 0);
        assert_eq!(matches[0].end, 5);
    }

    #[test]
    fn test_find_at_end() {
        let mut trie = Trie::new();
        trie.add(b"end", 0);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        let matches = daac.find(b"the end");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].start, 4);
        assert_eq!(matches[0].end, 7);
    }

    #[test]
    fn test_find_overlapping() {
        let mut trie = Trie::new();
        trie.add(b"a", 0);
        trie.add(b"aa", 1);
        trie.add(b"aaa", 2);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // "aaaa" should find many overlapping matches
        let matches = daac.find(b"aaaa");

        // "a" at positions 0, 1, 2, 3 (4 matches)
        // "aa" at positions 0, 1, 2 (3 matches)
        // "aaa" at positions 0, 1 (2 matches)
        // Total: 9 matches
        assert_eq!(matches.len(), 9);
    }

    #[test]
    fn test_find_iter_count() {
        let mut trie = Trie::new();
        trie.add(b"a", 0);
        trie.add(b"aa", 1);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // Test that iterator count matches find().len()
        let count = daac.find_iter(b"aaaa").count();
        let matches = daac.find(b"aaaa");
        assert_eq!(count, matches.len());
    }

    #[test]
    fn test_find_iter_early_termination() {
        let mut trie = Trie::new();
        trie.add(b"a", 0);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // Take only first 2 matches
        let first_two: Vec<Match> = daac.find_iter(b"aaaaa").take(2).collect();
        assert_eq!(first_two.len(), 2);
        assert_eq!(first_two[0].start, 0);
        assert_eq!(first_two[1].start, 1);
    }

    #[test]
    fn test_find_iter_matches_find() {
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.add(b"she", 1);
        trie.add(b"his", 2);
        trie.add(b"hers", 3);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        let text = b"ushers and his";

        // Both methods should return the same matches
        let iter_matches: Vec<Match> = daac.find_iter(text).collect();
        let find_matches = daac.find(text);

        assert_eq!(iter_matches.len(), find_matches.len());
        for (a, b) in iter_matches.iter().zip(find_matches.iter()) {
            assert_eq!(a.pattern_id, b.pattern_id);
            assert_eq!(a.start, b.start);
            assert_eq!(a.end, b.end);
        }
    }

    #[test]
    fn test_find_suffix_outputs() {
        // Test that suffix outputs (output chains) work correctly
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.add(b"she", 1);
        trie.build(MatchKind::Overlapping);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // "she" should match both "she" and "he" (suffix)
        let matches = daac.find(b"she");
        assert_eq!(matches.len(), 2);

        let match_ids: Vec<u32> = matches.iter().map(|m| m.pattern_id).collect();
        assert!(match_ids.contains(&0)); // "he"
        assert!(match_ids.contains(&1)); // "she"
    }

    // ============ LeftmostLongest mode tests (longest match wins) ============

    #[test]
    fn test_leftmost_longest_find_non_overlapping() {
        let mut trie = Trie::new();
        trie.add(b"a", 0);
        trie.add(b"aa", 1);
        trie.add(b"aaa", 2);
        trie.build(MatchKind::LeftmostLongest);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // LeftmostLongest prefers the longest match at each position
        let matches = daac.find(b"aaaa");

        // Should find "aaa" at 0..3, then "a" at 3..4
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].pattern_id, 2); // "aaa"
        assert_eq!(matches[0].start, 0);
        assert_eq!(matches[0].end, 3);
        assert_eq!(matches[1].pattern_id, 0); // "a"
        assert_eq!(matches[1].start, 3);
        assert_eq!(matches[1].end, 4);
    }

    #[test]
    fn test_leftmost_longest_find_she_he() {
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.add(b"she", 1);
        trie.build(MatchKind::LeftmostLongest);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // "she" should only match "she", not "he" (non-overlapping)
        let matches = daac.find(b"she");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].pattern_id, 1); // "she"
        assert_eq!(matches[0].start, 0);
        assert_eq!(matches[0].end, 3);
    }

    #[test]
    fn test_leftmost_longest_find_ushers() {
        let mut trie = Trie::new();
        trie.add(b"he", 0);
        trie.add(b"she", 1);
        trie.add(b"hers", 2);
        trie.build(MatchKind::LeftmostLongest);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // "ushers" with leftmost-longest
        let matches = daac.find(b"ushers");

        // Should find "she" at 1..4, then nothing overlapping with it
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].pattern_id, 1); // "she"
    }

    #[test]
    fn test_leftmost_longest_stores_match_kind() {
        let mut trie = Trie::new();
        trie.add(b"test", 0);
        trie.build(MatchKind::LeftmostLongest);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // DAAC should have inherited match_kind from Trie
        assert!(matches!(daac.match_kind, MatchKind::LeftmostLongest));
    }

    #[test]
    fn test_leftmost_longest_matches_trie() {
        // Ensure DAAC leftmost-longest matches Trie leftmost-longest results
        let mut trie1 = Trie::new();
        trie1.add(b"a", 0);
        trie1.add(b"aa", 1);
        trie1.add(b"aaa", 2);
        trie1.build(MatchKind::LeftmostLongest);

        let mut trie2 = Trie::new();
        trie2.add(b"a", 0);
        trie2.add(b"aa", 1);
        trie2.add(b"aaa", 2);
        trie2.build(MatchKind::LeftmostLongest);

        let daac = DoubleArrayAhoCorasick::from_trie(trie2);

        let text = b"aaaaaaa";
        let trie_matches = trie1.find(text);
        let daac_matches = daac.find(text);

        assert_eq!(trie_matches.len(), daac_matches.len());
        for (t, d) in trie_matches.iter().zip(daac_matches.iter()) {
            assert_eq!(t.pattern_id, d.pattern_id);
            assert_eq!(t.start, d.start);
            assert_eq!(t.end, d.end);
        }
    }

    // ============ LeftmostFirst mode tests (first-added pattern wins) ============

    #[test]
    fn test_leftmost_first_short_pattern_wins() {
        let mut trie = Trie::new();
        // Add shorter pattern first
        trie.add(b"a", 0);
        trie.add(b"aa", 1);
        trie.add(b"aaa", 2);
        trie.build(MatchKind::LeftmostFirst);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // LeftmostFirst: "a" was added first, so it wins at every position
        let matches = daac.find(b"aaaa");

        // Should find "a" at positions 0, 1, 2, 3 (4 matches)
        assert_eq!(matches.len(), 4);
        for (i, m) in matches.iter().enumerate() {
            assert_eq!(m.pattern_id, 0); // "a"
            assert_eq!(m.start, i);
            assert_eq!(m.end, i + 1);
        }
    }

    #[test]
    fn test_leftmost_first_long_pattern_first() {
        let mut trie = Trie::new();
        // Add longer pattern first
        trie.add(b"aaa", 0);
        trie.add(b"aa", 1);
        trie.add(b"a", 2);
        trie.build(MatchKind::LeftmostFirst);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // When longer patterns are added first, they're not pruned
        let matches = daac.find(b"aaaa");

        // Should find "aaa" at 0..3, then "a" at 3..4
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].pattern_id, 0); // "aaa"
        assert_eq!(matches[0].start, 0);
        assert_eq!(matches[0].end, 3);
        assert_eq!(matches[1].pattern_id, 2); // "a"
        assert_eq!(matches[1].start, 3);
        assert_eq!(matches[1].end, 4);
    }

    #[test]
    fn test_leftmost_first_stores_match_kind() {
        let mut trie = Trie::new();
        trie.add(b"test", 0);
        trie.build(MatchKind::LeftmostFirst);

        let daac = DoubleArrayAhoCorasick::from_trie(trie);

        // DAAC should have inherited match_kind from Trie
        assert!(matches!(daac.match_kind, MatchKind::LeftmostFirst));
    }

    #[test]
    fn test_leftmost_first_matches_trie() {
        // Ensure DAAC leftmost-first matches Trie leftmost-first results
        let mut trie1 = Trie::new();
        trie1.add(b"a", 0);
        trie1.add(b"aa", 1);
        trie1.add(b"aaa", 2);
        trie1.build(MatchKind::LeftmostFirst);

        let mut trie2 = Trie::new();
        trie2.add(b"a", 0);
        trie2.add(b"aa", 1);
        trie2.add(b"aaa", 2);
        trie2.build(MatchKind::LeftmostFirst);

        let daac = DoubleArrayAhoCorasick::from_trie(trie2);

        let text = b"aaaaaaa";
        let trie_matches = trie1.find(text);
        let daac_matches = daac.find(text);

        assert_eq!(trie_matches.len(), daac_matches.len());
        for (t, d) in trie_matches.iter().zip(daac_matches.iter()) {
            assert_eq!(t.pattern_id, d.pattern_id);
            assert_eq!(t.start, d.start);
            assert_eq!(t.end, d.end);
        }
    }
}