Skip to content

Commit

Permalink
opmph
Browse files Browse the repository at this point in the history
  • Loading branch information
rsk0315 committed Jun 8, 2024
1 parent 2c64e49 commit d829603
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions nekolib-src/seq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ inversion = { path = "inversion" }
lis = { path = "lis" }
suffix_array = { path = "suffix_array" }
majority_vote = { path = "majority_vote" }
opmph = { path = "opmph" }
9 changes: 9 additions & 0 deletions nekolib-src/seq/opmph/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "opmph"
version = "0.1.0"
edition = "2021"

[dependencies]

[dev-dependencies]
maplit = "1.0.2"
35 changes: 35 additions & 0 deletions nekolib-src/seq/opmph/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::BTreeSet;

// order-preserving minimal perfect hashing
pub trait Opmph<B> {
fn opmph(&self) -> B;
}

impl<I, T, B> Opmph<B> for I
where
for<'a> &'a I: IntoIterator<Item = &'a T>,
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 });
}
}
1 change: 1 addition & 0 deletions nekolib-src/seq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ doc_inline_reexport! {
inversion,
lis,
majority_vote,
opmph,
suffix_array,
}

0 comments on commit d829603

Please sign in to comment.