Skip to content

Commit

Permalink
id_prefix: add IdIndex::has_key()
Browse files Browse the repository at this point in the history
For the support for shorter prefixes within a revset, we'll want to be
able to check if an id is in the index.
  • Loading branch information
martinvonz committed May 11, 2023
1 parent 947ead4 commit 13bf68a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/src/id_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 13bf68a

Please sign in to comment.