#trie #retrieval-tree #frequency-dict #repetition-dict

t-oc

Trie Occurrence Counter is frequency dictionary for any type implementing Iterator<Item = char>

7 stable releases

6.0.2 Jan 3, 2025
6.0.1 Jan 2, 2025
5.0.3 Dec 15, 2024
4.0.1 Nov 28, 2024
1.0.1 Nov 17, 2024

#334 in Algorithms

Download history 165/week @ 2024-11-13 366/week @ 2024-11-20 248/week @ 2024-11-27 345/week @ 2024-12-04 153/week @ 2024-12-11 16/week @ 2024-12-18 380/week @ 2025-01-01 13/week @ 2025-01-08

416 downloads per month

MIT license

52KB
1K SLoC

t-oc

  • trie occurrence counter is frequency dictionary that uses any impl Iterator<Item = char> type as occurrent
  • since its flexibility it allows to count apples with pears without hassle

basic usage

  • only English alphabet letters are supported oob
  • since core::str::Chars is impl Iterator<Item = char> type usage with str is oob
use t_oc::Toc;
use std::panic::catch_unwind;

let mut toc = Toc::new();
let occurrent = "true";

_ = toc.add(occurrent.chars(), None);
_ = toc.add(true.to_string().chars(), None);

assert_eq!(2, toc.acq(occurrent.chars()).uproot());
toc.put(occurrent.chars(), 15);
assert_eq!(15, toc.acq(occurrent.chars()).uproot());

let catch = catch_unwind(move|| _ = toc.add("#&%".chars(), None));
assert!(catch.is_err());

custom alphabet implementation

  • to use custom alphabet employ Toc::new_with
  • provide it with function for index conversion and alphabet relevant length
  • see also example on new_with
use t_oc::Toc;

struct UsizeCharIterator {
    s: String,
    c: usize,
}

impl Iterator for UsizeCharIterator {
    type Item = char;

    fn next(&mut self) -> Option<char> {
        let c = self.c;

        let opt = self.s.chars().nth(c);

        if opt.is_some() {
            self.c = c + 1;
        }

        opt
    }
}

impl UsizeCharIterator {
    fn new(n: usize) -> Self {
        Self { s: n.to_string(), c: 0 }
    }
}

fn ix(c: char) -> usize {
    c.to_digit(10).unwrap() as usize
}

#[test]
fn test() {
    let nums = [0, 2, 2, 100, 100, 9999];

    let mut toc = Toc::new_with(ix, None, 10);

    for n in nums {
        _ = toc.add(UsizeCharIterator::new(n), None);
    }

    assert_eq!(1, toc.acq(UsizeCharIterator::new(0)).uproot());
    assert_eq!(2, toc.acq(UsizeCharIterator::new(2)).uproot());
    assert_eq!(2, toc.acq(UsizeCharIterator::new(100)).uproot());
    assert_eq!(1, toc.acq(UsizeCharIterator::new(9999)).uproot());
}

No runtime deps

Features