Auto merge of #9792 - GuillaumeGomez:remove_comparator, r=nox

Remove util::vec::Comparator

r? @nox

Fixes #9696

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9792)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-03-01 19:10:45 +05:30
commit 40c52d55e2
5 changed files with 11 additions and 142 deletions

View file

@ -10,7 +10,6 @@ use simd::u32x4;
use std::cmp::{Ordering, PartialOrd};
use std::vec::Vec;
use std::{fmt, mem, u16};
use util::vec::*;
/// GlyphEntry is a port of Gecko's CompressedGlyph scheme for storing glyph data compactly.
///
@ -248,7 +247,7 @@ impl<'a> DetailedGlyphStore {
detail_offset: 0, // unused
};
let i = self.detail_lookup.binary_search_index(&key)
let i = self.detail_lookup.binary_search(&key)
.expect("Invalid index not found in detailed glyph lookup table!");
assert!(i + (count as usize) <= self.detail_buffer.len());
@ -268,7 +267,7 @@ impl<'a> DetailedGlyphStore {
detail_offset: 0, // unused
};
let i = self.detail_lookup.binary_search_index(&key)
let i = self.detail_lookup.binary_search(&key)
.expect("Invalid index not found in detailed glyph lookup table!");
assert!(i + (detail_offset as usize) < self.detail_buffer.len());

View file

@ -12,7 +12,6 @@ use std::cmp::{Ordering, max};
use std::slice::Iter;
use std::sync::Arc;
use text::glyph::{CharIndex, GlyphStore};
use util::vec::{Comparator, FullBinarySearchMethods};
use webrender_traits;
thread_local! {
@ -63,14 +62,12 @@ pub struct NaturalWordSliceIterator<'a> {
reverse: bool,
}
struct CharIndexComparator;
impl Comparator<CharIndex, GlyphRun> for CharIndexComparator {
fn compare(&self, key: &CharIndex, value: &GlyphRun) -> Ordering {
if *key < value.range.begin() {
Ordering::Less
} else if *key >= value.range.end() {
impl GlyphRun {
fn compare(&self, key: &CharIndex) -> Ordering {
if *key < self.range.begin() {
Ordering::Greater
} else if *key >= self.range.end() {
Ordering::Less
} else {
Ordering::Equal
}
@ -314,11 +311,12 @@ impl<'a> TextRun {
}
}
let result = (&**self.glyphs).binary_search_index_by(&index, CharIndexComparator);
if let Some(result) = result {
if let Ok(result) = (&**self.glyphs).binary_search_by(|current| current.compare(&index)) {
index_of_first_glyph_run_cache.set(Some((self_ptr, index, result)));
Some(result)
} else {
None
}
result
})
}

View file

@ -2,69 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::cmp::{Ordering, PartialEq, PartialOrd};
use std::marker::PhantomData;
use std::ops;
use super::smallvec::VecLike;
/// FIXME(pcwalton): Workaround for lack of unboxed closures. This is called in
/// performance-critical code, so a closure is insufficient.
pub trait Comparator<K, T> {
fn compare(&self, key: &K, value: &T) -> Ordering;
}
pub trait BinarySearchMethods<T: Ord + PartialOrd + PartialEq> {
fn binary_search_(&self, key: &T) -> Option<&T>;
fn binary_search_index(&self, key: &T) -> Option<usize>;
}
pub trait FullBinarySearchMethods<T> {
fn binary_search_index_by<K, C: Comparator<K, T>>(&self, key: &K, cmp: C) -> Option<usize>;
}
impl<T: Ord + PartialOrd + PartialEq> BinarySearchMethods<T> for [T] {
fn binary_search_(&self, key: &T) -> Option<&T> {
self.binary_search_index(key).map(|i| &self[i])
}
fn binary_search_index(&self, key: &T) -> Option<usize> {
self.binary_search_index_by(key, DefaultComparator)
}
}
impl<T> FullBinarySearchMethods<T> for [T] {
fn binary_search_index_by<K, C: Comparator<K, T>>(&self, key: &K, cmp: C) -> Option<usize> {
if self.is_empty() {
return None;
}
let mut low: isize = 0;
let mut high: isize = (self.len() as isize) - 1;
while low <= high {
// http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
let mid = ((low as usize) + (high as usize)) >> 1;
let midv = &self[mid];
match cmp.compare(key, midv) {
Ordering::Greater => low = (mid as isize) + 1,
Ordering::Less => high = (mid as isize) - 1,
Ordering::Equal => return Some(mid),
}
}
None
}
}
struct DefaultComparator;
impl<T: PartialEq + PartialOrd + Ord> Comparator<T, T> for DefaultComparator {
fn compare(&self, key: &T, value: &T) -> Ordering {
(*key).cmp(value)
}
}
// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
pub fn byte_swap(data: &mut [u8]) {
let length = data.len();