Skip to content

Commit

Permalink
excise: Comment out code dependent on laysakura/louds-rs#14.
Browse files Browse the repository at this point in the history
If that PR gets merged, we'll put it back.
  • Loading branch information
shanecelis committed Apr 28, 2024
1 parent d5f433c commit e35c11e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ categories = ["compression", "data-structures"]
edition = "2021"

[dependencies]
# louds-rs = "0.6"
louds-rs = { path = "../louds-rs" }
louds-rs = "0.6"
mem_dbg = { version = "0.1.4", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

Expand Down
81 changes: 53 additions & 28 deletions src/inc_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//! the loop.
use crate::{
map::Trie,
try_collect::{TryCollect, TryFromIterator},
// try_collect::{TryCollect, TryFromIterator},
};
use louds_rs::LoudsNodeNum;

Expand Down Expand Up @@ -184,16 +184,34 @@ impl<'a, Label: Ord, Value> IncSearch<'a, Label, Value> {
self.trie.value(self.node)
}

/// Return the current prefix for this search.
pub fn prefix<C, M>(&self) -> C
where
C: TryFromIterator<Label, M>,
Label: Clone,
{
let mut v: Vec<Label> = self.trie.child_to_ancestors(self.node)
.map(|node| self.trie.label(node).clone()).collect();
v.reverse();
v.into_iter().try_collect().expect("Could not collect")
// Return the current prefix for this search.
// pub fn prefix<C, M>(&self) -> C
// where
// C: TryFromIterator<Label, M>,
// Label: Clone,
// {
// let mut v: Vec<Label> = self.trie.child_to_ancestors(self.node)
// .map(|node| self.trie.label(node).clone()).collect();
// v.reverse();
// v.into_iter().try_collect().expect("Could not collect")
// }

/// Returne the length of the current prefix for this search.
pub fn prefix_len(&self) -> usize {
// TODO: If PR for child_to_ancestors is accepted. Use the iterator and
// remove `pub(crate)` from Trie.louds field. Also uncomment prefix()
// above.

// self.trie.child_to_ancestors(self.node).count()

let mut node = self.node;
let mut count = 0;
while node.0 > 1 {
let index = self.trie.louds.node_num_to_index(node);
node = self.trie.louds.child_to_parent(index);
count += 1;
}
count
}

// This isn't actually possible.
Expand Down Expand Up @@ -232,38 +250,45 @@ mod search_tests {
fn inc_search() {
let trie = build_trie();
let mut search = trie.inc_search();
assert_eq!("", search.prefix::<String, _>());
// assert_eq!("", search.prefix::<String, _>());
assert_eq!(0, search.prefix_len());
assert_eq!(None, search.query(&b'z'));
assert_eq!("", search.prefix::<String, _>());
// assert_eq!("", search.prefix::<String, _>());
assert_eq!(0, search.prefix_len());
assert_eq!(Answer::PrefixAndMatch, search.query(&b'a').unwrap());
assert_eq!("a", search.prefix::<String, _>());
// assert_eq!("a", search.prefix::<String, _>());
assert_eq!(1, search.prefix_len());
assert_eq!(Answer::Prefix, search.query(&b'p').unwrap());
assert_eq!("ap", search.prefix::<String, _>());
// assert_eq!("ap", search.prefix::<String, _>());
assert_eq!(2, search.prefix_len());
assert_eq!(Answer::PrefixAndMatch, search.query(&b'p').unwrap());
assert_eq!("app", search.prefix::<String, _>());
// assert_eq!("app", search.prefix::<String, _>());
assert_eq!(3, search.prefix_len());
assert_eq!(Answer::Prefix, search.query(&b'l').unwrap());
assert_eq!("appl", search.prefix::<String, _>());
// assert_eq!("appl", search.prefix::<String, _>());
assert_eq!(4, search.prefix_len());
assert_eq!(Answer::Match, search.query(&b'e').unwrap());
assert_eq!("apple", search.prefix::<String, _>());
// assert_eq!("apple", search.prefix::<String, _>());
assert_eq!(5, search.prefix_len());
}

#[test]
fn inc_search_value() {
let trie = build_trie();
let mut search = trie.inc_search();
assert_eq!("", search.prefix::<String, _>());
// assert_eq!("", search.prefix::<String, _>());
assert_eq!(None, search.query(&b'z'));
assert_eq!("", search.prefix::<String, _>());
// assert_eq!("", search.prefix::<String, _>());
assert_eq!(Answer::PrefixAndMatch, search.query(&b'a').unwrap());
assert_eq!("a", search.prefix::<String, _>());
// assert_eq!("a", search.prefix::<String, _>());
assert_eq!(Answer::Prefix, search.query(&b'p').unwrap());
assert_eq!("ap", search.prefix::<String, _>());
// assert_eq!("ap", search.prefix::<String, _>());
assert_eq!(Answer::PrefixAndMatch, search.query(&b'p').unwrap());
assert_eq!("app", search.prefix::<String, _>());
// assert_eq!("app", search.prefix::<String, _>());
assert_eq!(Answer::Prefix, search.query(&b'l').unwrap());
assert_eq!("appl", search.prefix::<String, _>());
// assert_eq!("appl", search.prefix::<String, _>());
assert_eq!(Answer::Match, search.query(&b'e').unwrap());
assert_eq!("apple", search.prefix::<String, _>());
// assert_eq!("apple", search.prefix::<String, _>());
assert_eq!(Some(&2), search.value());
}

Expand All @@ -272,13 +297,13 @@ mod search_tests {
let trie = build_trie();
let mut search = trie.inc_search();
assert_eq!(Err(0), search.query_until("zoo"));
assert_eq!("", search.prefix::<String, _>());
// assert_eq!("", search.prefix::<String, _>());
search.reset();
assert_eq!(Err(1), search.query_until("blue"));
assert_eq!("b", search.prefix::<String, _>());
// assert_eq!("b", search.prefix::<String, _>());
search.reset();
assert_eq!(Answer::Match, search.query_until("apple").unwrap());
assert_eq!("apple", search.prefix::<String, _>());
// assert_eq!("apple", search.prefix::<String, _>());
assert_eq!(Some(&2), search.value());
}

Expand Down
2 changes: 1 addition & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use mem_dbg::MemDbg;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// A trie for sequences of the type `Label`; each sequence has an associated `Value`.
pub struct Trie<Label, Value> {
louds: Louds,
pub(crate) louds: Louds,

/// (LoudsNodeNum - 2) -> TrieLabel
trie_labels: Vec<TrieLabel<Label, Value>>,
Expand Down
8 changes: 4 additions & 4 deletions src/map/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::Trie;
use crate::inc_search::IncSearch;
use crate::iter::{PostfixIter, PrefixIter, SearchIter};
use crate::try_collect::{TryCollect, TryFromIterator};
use louds_rs::{ChildNodeIter, LoudsNodeNum, AncestorNodeIter};
use louds_rs::{ChildNodeIter, LoudsNodeNum};
use std::iter::FromIterator;

impl<Label: Ord, Value> Trie<Label, Value> {
Expand Down Expand Up @@ -211,9 +211,9 @@ impl<Label: Ord, Value> Trie<Label, Value> {
self.trie_labels[(node_num.0 - 2) as usize].value.as_mut()
}

pub (crate) fn child_to_ancestors(&self, node_num: LoudsNodeNum) -> AncestorNodeIter {
self.louds.child_to_ancestors(node_num)
}
// pub (crate) fn child_to_ancestors(&self, node_num: LoudsNodeNum) -> AncestorNodeIter {
// self.louds.child_to_ancestors(node_num)
// }
}

impl<Label, Value, C> FromIterator<(C, Value)> for Trie<Label, Value>
Expand Down

0 comments on commit e35c11e

Please sign in to comment.