mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-12 15:16:35 +00:00
index: deduplicate binary search functions of commit_id lookup
This commit is contained in:
parent
049a9261ab
commit
2b7021664d
1 changed files with 6 additions and 24 deletions
|
@ -1233,30 +1233,12 @@ impl IndexSegment for ReadonlyIndex {
|
|||
}
|
||||
|
||||
fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
|
||||
if self.num_local_commits == 0 {
|
||||
// Avoid overflow when subtracting 1 below
|
||||
return None;
|
||||
}
|
||||
let mut low = 0;
|
||||
let mut high = self.num_local_commits - 1;
|
||||
|
||||
// binary search for the commit id
|
||||
loop {
|
||||
let mid = (low + high) / 2;
|
||||
let entry = self.lookup_entry(mid);
|
||||
let entry_commit_id = entry.commit_id();
|
||||
if high == low {
|
||||
return if &entry_commit_id == commit_id {
|
||||
Some(entry.pos())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
if commit_id > &entry_commit_id {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
let lookup_pos = self.commit_id_byte_prefix_to_lookup_pos(commit_id)?;
|
||||
let entry = self.lookup_entry(lookup_pos);
|
||||
if &entry.commit_id() == commit_id {
|
||||
Some(entry.pos())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue