From 13bf68ae60b21f45dff37e2dff120a8be909c15e Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 8 May 2023 06:30:25 -0700 Subject: [PATCH] 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. --- lib/src/id_prefix.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/src/id_prefix.rs b/lib/src/id_prefix.rs index 73b257b45c..a93dfdeabe 100644 --- a/lib/src/id_prefix.rs +++ b/lib/src/id_prefix.rs @@ -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