mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 02:04:19 +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> {
|
fn segment_commit_id_to_pos(&self, commit_id: &CommitId) -> Option<IndexPosition> {
|
||||||
if self.num_local_commits == 0 {
|
let lookup_pos = self.commit_id_byte_prefix_to_lookup_pos(commit_id)?;
|
||||||
// Avoid overflow when subtracting 1 below
|
let entry = self.lookup_entry(lookup_pos);
|
||||||
return None;
|
if &entry.commit_id() == commit_id {
|
||||||
}
|
|
||||||
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())
|
Some(entry.pos())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
|
||||||
}
|
|
||||||
if commit_id > &entry_commit_id {
|
|
||||||
low = mid + 1;
|
|
||||||
} else {
|
|
||||||
high = mid;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue