Skip to content

Commit

Permalink
Merge pull request #103 from Qrlew/impl_full_path
Browse files Browse the repository at this point in the history
Implement `Hierarchy::get_key_value`
  • Loading branch information
ngrislain authored Sep 8, 2023
2 parents 1daaca7 + 7aa86cc commit fed0341
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implemented `least` and `greatest` (support qualified and unqualified columns)[MR102](https://github.com/Qrlew/qrlew/pull/102)
### Fixed
- `And` for struct of structs [MR100](https://github.com/Qrlew/qrlew/pull/100)
### Added
- `Hierarchy::get_key_value` [MR103](https://github.com/Qrlew/qrlew/pull/103)

## [0.2.2] - 2023-08-29
### Changed
Expand Down
83 changes: 68 additions & 15 deletions src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,29 @@ impl<T: Clone> Hierarchy<T> {
.collect()
}

pub fn get(&self, path: &[String]) -> Option<&T> {
self.0.get(path).or_else(|| {
self.0
.iter()
.fold(Found::Zero, |f, (qualified_path, object)| {
if is_suffix_of(path, qualified_path) {
match f {
Found::Zero => Found::One(object),
_ => Found::More,
pub fn get<'a>(&'a self, path: &[String]) -> Option<&'a T> {
self.get_key_value(path).and_then(|(_, v)| Some(v))
}

pub fn get_key_value<'a>(&'a self, path: &[String]) -> Option<(&'a [String], &'a T)> {
self.0
.get_key_value(path)
.map(|(k, v)| (k.as_slice(), v))
.or_else(|| {
self.0
.iter()
.fold(Found::Zero, |f, (qualified_path, object)| {
if is_suffix_of(path, qualified_path) {
match f {
Found::Zero => Found::One((qualified_path.as_ref(), object)),
_ => Found::More,
}
} else {
f
}
} else {
f
}
})
.into()
})
})
.into()
})
}

pub fn filter(&self, path: &[String]) -> Self {
Expand Down Expand Up @@ -382,4 +389,50 @@ mod tests {
])
)
}

#[test]
fn test_full_path() {
let values = Hierarchy::from([
(vec!["a", "b", "c"], 1),
(vec!["a", "b", "d"], 2),
(vec!["a", "c"], 3),
(vec!["a", "e"], 4),
(vec!["a", "e", "f"], 5),
(vec!["b", "c"], 6),
]);
assert_eq!(
values.get_key_value(&["a".to_string(), "c".to_string()]),
Some((["a".to_string(), "c".to_string()].as_slice(), &3))
);
assert_eq!(values.get_key_value(&vec!["c".to_string()]), None);
assert_eq!(
values.get_key_value(&["b".into(), "c".into()]),
Some((["b".to_string(), "c".to_string()].as_slice(), &6))
);
assert_eq!(
values.get_key_value(&["e".to_string()]),
Some((["a".to_string(), "e".to_string()].as_slice(), &4))
);
assert_eq!(
values.get_key_value(&["e".to_string(), "f".to_string()]),
Some((
["a".to_string(), "e".to_string(), "f".to_string()].as_slice(),
&5
))
);
assert_eq!(
values.get_key_value(&["b".to_string(), "d".to_string()]),
Some((
["a".to_string(), "b".to_string(), "d".to_string()].as_slice(),
&2
))
);
assert_eq!(
values.get_key_value(&["d".to_string()]),
Some((
["a".to_string(), "b".to_string(), "d".to_string()].as_slice(),
&2
))
);
}
}

0 comments on commit fed0341

Please sign in to comment.