提交 4bedc314 编写于 作者: M Michael Woerister

Cleanup SortedMap by wrapping element lookup in a method.

上级 e850d78b
......@@ -42,9 +42,7 @@ pub fn from_presorted_elements(elements: Vec<(K, V)>) -> SortedMap<K, V>
#[inline]
pub fn insert(&mut self, key: K, mut value: V) -> Option<V> {
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(&key));
match index {
match self.lookup_index_for(&key) {
Ok(index) => {
let mut slot = unsafe {
self.data.get_unchecked_mut(index)
......@@ -61,9 +59,7 @@ pub fn insert(&mut self, key: K, mut value: V) -> Option<V> {
#[inline]
pub fn remove(&mut self, key: &K) -> Option<V> {
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key));
match index {
match self.lookup_index_for(key) {
Ok(index) => {
Some(self.data.remove(index).1)
}
......@@ -75,9 +71,7 @@ pub fn remove(&mut self, key: &K) -> Option<V> {
#[inline]
pub fn get(&self, key: &K) -> Option<&V> {
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key));
match index {
match self.lookup_index_for(key) {
Ok(index) => {
unsafe {
Some(&self.data.get_unchecked(index).1)
......@@ -91,9 +85,7 @@ pub fn get(&self, key: &K) -> Option<&V> {
#[inline]
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key));
match index {
match self.lookup_index_for(key) {
Ok(index) => {
unsafe {
Some(&mut self.data.get_unchecked_mut(index).1)
......@@ -168,12 +160,9 @@ pub fn insert_presorted(&mut self, mut elements: Vec<(K, V)>) {
debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0));
let index = {
let first_element = &elements[0].0;
self.data.binary_search_by(|&(ref x, _)| x.cmp(first_element))
};
let start_index = self.lookup_index_for(&elements[0].0);
let drain = match index {
let drain = match start_index {
Ok(index) => {
let mut drain = elements.drain(..);
self.data[index] = drain.next().unwrap();
......@@ -200,18 +189,24 @@ pub fn insert_presorted(&mut self, mut elements: Vec<(K, V)>) {
}
}
/// Looks up the key in `self.data` via `slice::binary_search()`.
#[inline(always)]
fn lookup_index_for(&self, key: &K) -> Result<usize, usize> {
self.data.binary_search_by(|&(ref x, _)| x.cmp(key))
}
#[inline]
fn range_slice_indices<R>(&self, range: R) -> (usize, usize)
where R: RangeBounds<K>
{
let start = match range.start() {
Bound::Included(ref k) => {
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
match self.lookup_index_for(k) {
Ok(index) | Err(index) => index
}
}
Bound::Excluded(ref k) => {
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
match self.lookup_index_for(k) {
Ok(index) => index + 1,
Err(index) => index,
}
......@@ -221,13 +216,13 @@ fn range_slice_indices<R>(&self, range: R) -> (usize, usize)
let end = match range.end() {
Bound::Included(ref k) => {
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
match self.lookup_index_for(k) {
Ok(index) => index + 1,
Err(index) => index,
}
}
Bound::Excluded(ref k) => {
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
match self.lookup_index_for(k) {
Ok(index) | Err(index) => index,
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册