Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] Support map::Trie#children #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/map/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,73 @@ impl<Label: Ord, Value> Trie<Label, Value> {
self.postfix_search([])
}

/// Returns an iterator across the direct dependents of a trie node.
///
/// For example, in the tree (A (B, C (D, E))), children(C) would return an
/// iterator over D and E.
pub fn children(&self, query: impl AsRef<[Label]>) -> Option<ChildNodeIter>
where
Label: Clone,
{

let mut cur_node_num = LoudsNodeNum(1);
let mut buffer = Vec::new();

// Consumes query (prefix)
for chr in query.as_ref() {
let children_node_nums: Vec<_> = self.children_node_nums(cur_node_num).collect();
let res = self.bin_search_by_children_labels(chr, &children_node_nums[..]);
match res {
Ok(i) => {
cur_node_num = children_node_nums[i];
buffer.push(cur_node_num);
}
Err(_) => {
return None;
}
}
}

Some(self.children_node_nums(cur_node_num))
// .filter(|x| self.is_terminal(*x))
// .map(|x| self.label(x))
// .collect::<Vec<_>>()
}

/// Returns an iterator across the direct dependents of a trie node.
/// This function returns the values of the children.
/// If a child does not have a value, it is not included in the result.
/// Note that this function does not take a `terminals_only` argument
/// (as `children_labels` does) because only terminal nodes have values.
pub fn children_values(&self, query: impl AsRef<[Label]>) -> Option<Vec<&Value>>
where
Label: Clone,
{
let children = self.children(query)?;

Some(children
.map(|x| self.value(x))
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.collect::<Vec<_>>())
}

/// Returns an iterator across the direct dependents of a trie node.
/// This function returns the labels of the children.
/// If `terminals_only` is true, only terminal nodes are included in the
/// resulting iterator.
pub fn children_labels(&self, query: impl AsRef<[Label]>, terminals_only: bool) -> Option<Vec<Label>>
where
Label: Clone,
{
let children = self.children(query)?;

Some(children
.filter(|x| !terminals_only || self.is_terminal(*x))
.map(|x| self.label(x).clone())
.collect::<Vec<_>>())
}

/// Return the common prefixes of `query`.
pub fn common_prefix_search<C, M>(
&self,
Expand Down