mirror of
https://github.com/facebook/sapling.git
synced 2024-10-10 08:47:12 +03:00
dag: implement binary_search_by for VecDeque
Summary: This makes it easier to replace Vec<Span> with VecDeque<Span> in SpanSet for efficient push_front and deprecates SpanSetAsc (which uses Id in a bit hacky way - they are not real Ids). Reviewed By: sfilipco Differential Revision: D23385245 fbshipit-source-id: b612cd816223a301e2705084057bd24865beccf0
This commit is contained in:
parent
d8225764a5
commit
71f101054a
65
eden/scm/lib/dag/src/bsearch.rs
Normal file
65
eden/scm/lib/dag/src/bsearch.rs
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This software may be used and distributed according to the terms of the
|
||||
* GNU General Public License version 2.
|
||||
*/
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use Ordering::Equal;
|
||||
use Ordering::Greater;
|
||||
use Ordering::Less;
|
||||
|
||||
// Based on rust stdlib slice::binary_search_by:
|
||||
// https://github.com/rust-lang/rust/blob/dbad8c93680710e80c54cbaf8416821f7a5750c8/library/core/src/slice/mod.rs#L1721
|
||||
|
||||
pub trait BinarySearchBy<T> {
|
||||
/// Binary searches this sorted slice with a comparator function.
|
||||
///
|
||||
/// The comparator function should implement an order consistent
|
||||
/// with the sort order of the underlying slice, returning an
|
||||
/// order code that indicates whether its argument is `Less`,
|
||||
/// `Equal` or `Greater` the desired target.
|
||||
///
|
||||
/// If the value is found then [`Result::Ok`] is returned, containing the
|
||||
/// index of the matching element. If there are multiple matches, then any
|
||||
/// one of the matches could be returned. If the value is not found then
|
||||
/// [`Result::Err`] is returned, containing the index where a matching
|
||||
/// element could be inserted while maintaining sorted order.
|
||||
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
|
||||
where
|
||||
F: FnMut(&T) -> Ordering;
|
||||
}
|
||||
|
||||
impl<T> BinarySearchBy<T> for VecDeque<T> {
|
||||
fn binary_search_by<F>(&self, mut f: F) -> Result<usize, usize>
|
||||
where
|
||||
F: FnMut(&T) -> Ordering,
|
||||
{
|
||||
let s = self;
|
||||
let mut size = s.len();
|
||||
if size == 0 {
|
||||
return Err(0);
|
||||
}
|
||||
let mut base = 0usize;
|
||||
while size > 1 {
|
||||
let half = size / 2;
|
||||
let mid = base + half;
|
||||
// mid is always in [0, size), that means mid is >= 0 and < size.
|
||||
// mid >= 0: by definition
|
||||
// mid < size: mid = size / 2 + size / 4 + size / 8 ...
|
||||
let cmp = f(s.get(mid).unwrap());
|
||||
base = if cmp == Greater { base } else { mid };
|
||||
size -= half;
|
||||
}
|
||||
// base is always in [0, size) because base <= mid.
|
||||
let cmp = f(s.get(base).unwrap());
|
||||
if cmp == Equal {
|
||||
Ok(base)
|
||||
} else {
|
||||
Err(base + (cmp == Less) as usize)
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
//!
|
||||
//! Building blocks for the commit graph used by source control.
|
||||
|
||||
mod bsearch;
|
||||
mod default_impl;
|
||||
mod delegate;
|
||||
pub mod errors;
|
||||
|
Loading…
Reference in New Issue
Block a user