id_prefix: add IdIndex::has_key()

For the support for shorter prefixes within a revset, we'll want to be
able to check if an id is in the index.
This commit is contained in:
Martin von Zweigbergk 2023-05-08 06:30:25 -07:00 committed by Martin von Zweigbergk
parent 481b8c5d0e
commit 2e12aad1f7

View file

@ -108,6 +108,10 @@ where
.map(|(k, v)| (k, v))
}
pub fn has_key(&self, key: &K) -> bool {
self.0.binary_search_by(|(k, _)| k.cmp(key)).is_ok()
}
/// This function returns the shortest length of a prefix of `key` that
/// disambiguates it from every other key in the index.
///
@ -192,6 +196,18 @@ mod tests {
);
}
#[test]
fn test_has_key() {
// No crash if empty
let id_index = IdIndex::from_vec(vec![] as Vec<(ChangeId, ())>);
assert!(!id_index.has_key(&ChangeId::from_hex("00")));
let id_index = IdIndex::from_vec(vec![(ChangeId::from_hex("ab"), ())]);
assert!(!id_index.has_key(&ChangeId::from_hex("aa")));
assert!(id_index.has_key(&ChangeId::from_hex("ab")));
assert!(!id_index.has_key(&ChangeId::from_hex("ac")));
}
#[test]
fn test_id_index_shortest_unique_prefix_len() {
// No crash if empty