From d829603bb021542857778d68b3de948ff48aff91 Mon Sep 17 00:00:00 2001 From: rsk0315 Date: Sat, 8 Jun 2024 14:39:42 +0900 Subject: [PATCH] opmph --- nekolib-src/seq/Cargo.toml | 1 + nekolib-src/seq/opmph/Cargo.toml | 9 ++++++++ nekolib-src/seq/opmph/src/lib.rs | 35 ++++++++++++++++++++++++++++++++ nekolib-src/seq/src/lib.rs | 1 + 4 files changed, 46 insertions(+) create mode 100644 nekolib-src/seq/opmph/Cargo.toml create mode 100644 nekolib-src/seq/opmph/src/lib.rs diff --git a/nekolib-src/seq/Cargo.toml b/nekolib-src/seq/Cargo.toml index c1e1525648..f043c2d2a6 100644 --- a/nekolib-src/seq/Cargo.toml +++ b/nekolib-src/seq/Cargo.toml @@ -11,3 +11,4 @@ inversion = { path = "inversion" } lis = { path = "lis" } suffix_array = { path = "suffix_array" } majority_vote = { path = "majority_vote" } +opmph = { path = "opmph" } diff --git a/nekolib-src/seq/opmph/Cargo.toml b/nekolib-src/seq/opmph/Cargo.toml new file mode 100644 index 0000000000..b9dfff0151 --- /dev/null +++ b/nekolib-src/seq/opmph/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "opmph" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[dev-dependencies] +maplit = "1.0.2" diff --git a/nekolib-src/seq/opmph/src/lib.rs b/nekolib-src/seq/opmph/src/lib.rs new file mode 100644 index 0000000000..ce8970ebf3 --- /dev/null +++ b/nekolib-src/seq/opmph/src/lib.rs @@ -0,0 +1,35 @@ +use std::collections::BTreeSet; + +// order-preserving minimal perfect hashing +pub trait Opmph { + fn opmph(&self) -> B; +} + +impl Opmph for I +where + for<'a> &'a I: IntoIterator, + T: Clone + Ord, + B: FromIterator<(T, usize)>, +{ + fn opmph(&self) -> B { + let seen: BTreeSet<_> = self.into_iter().cloned().collect(); + seen.into_iter().zip(0..).collect() + } +} + +#[cfg(test)] +mod tests { + use std::collections::BTreeMap; + + use maplit::btreemap; + + use super::*; + + #[test] + fn sanity_check() { + let a = vec![3, 5, 1, 2, 5]; + let enc: BTreeMap<_, _> = a.opmph(); + + assert_eq!(enc, btreemap! { 1 => 0, 2 => 1, 3 => 2, 5 => 3 }); + } +} diff --git a/nekolib-src/seq/src/lib.rs b/nekolib-src/seq/src/lib.rs index a55df3e21b..c42fa59101 100644 --- a/nekolib-src/seq/src/lib.rs +++ b/nekolib-src/seq/src/lib.rs @@ -4,5 +4,6 @@ doc_inline_reexport! { inversion, lis, majority_vote, + opmph, suffix_array, }