index: use iterator to simplify segment_resolve_prefix() a bit further

This commit is contained in:
Yuya Nishihara 2023-01-26 15:34:22 +09:00
parent 956a2d5f83
commit 770ca72a1f

View file

@ -1337,22 +1337,14 @@ impl IndexSegment for ReadonlyIndex {
let lookup_pos = self let lookup_pos = self
.commit_id_byte_prefix_to_lookup_pos(&min_bytes_prefix) .commit_id_byte_prefix_to_lookup_pos(&min_bytes_prefix)
.unwrap_or(self.num_local_commits); .unwrap_or(self.num_local_commits);
let mut first_match = None; let mut matches = (lookup_pos..self.num_local_commits)
for i in lookup_pos..self.num_local_commits { .map(|pos| self.lookup_entry(pos).commit_id())
let entry = self.lookup_entry(i); .take_while(|id| prefix.matches(id))
let id = entry.commit_id(); .fuse();
if prefix.matches(&id) { match (matches.next(), matches.next()) {
if first_match.is_some() { (Some(id), None) => PrefixResolution::SingleMatch(id),
return PrefixResolution::AmbiguousMatch; (Some(_), Some(_)) => PrefixResolution::AmbiguousMatch,
} (None, _) => PrefixResolution::NoMatch,
first_match = Some(id)
} else {
break;
}
}
match first_match {
None => PrefixResolution::NoMatch,
Some(id) => PrefixResolution::SingleMatch(id),
} }
} }
@ -1437,23 +1429,16 @@ impl IndexSegment for MutableIndex {
fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> { fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution<CommitId> {
let (_, min_bytes_prefix) = prefix.bytes_prefixes(); let (_, min_bytes_prefix) = prefix.bytes_prefixes();
let potential_range = self let mut matches = self
.lookup .lookup
.range((Bound::Included(&min_bytes_prefix), Bound::Unbounded)); .range((Bound::Included(&min_bytes_prefix), Bound::Unbounded))
let mut first_match = None; .map(|(id, _pos)| id)
for (id, _pos) in potential_range { .take_while(|&id| prefix.matches(id))
if prefix.matches(id) { .fuse();
if first_match.is_some() { match (matches.next(), matches.next()) {
return PrefixResolution::AmbiguousMatch; (Some(id), None) => PrefixResolution::SingleMatch(id.clone()),
} (Some(_), Some(_)) => PrefixResolution::AmbiguousMatch,
first_match = Some(id) (None, _) => PrefixResolution::NoMatch,
} else {
break;
}
}
match first_match {
None => PrefixResolution::NoMatch,
Some(id) => PrefixResolution::SingleMatch(id.clone()),
} }
} }