servo: Merge #9792 - Remove util::vec::Comparator (from GuillaumeGomez:remove_comparator); r=nox

r? @nox

Fixes #9696

Source-Repo: https://github.com/servo/servo
Source-Revision: 40c52d55e2037a8dc154718f43260bd347739260
This commit is contained in:
Guillaume Gomez 2016-03-01 18:41:45 +05:01
Родитель 8b0d499bf2
Коммит fe3e62b8a4
5 изменённых файлов: 11 добавлений и 142 удалений

Просмотреть файл

@ -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());

Просмотреть файл

@ -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
})
}

Просмотреть файл

@ -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();

Просмотреть файл

@ -16,4 +16,3 @@ extern crate util;
#[cfg(test)] mod opts;
#[cfg(test)] mod str;
#[cfg(test)] mod thread;
#[cfg(test)] mod vec;

Просмотреть файл

@ -1,68 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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::fmt::Debug;
use util::vec::BinarySearchMethods;
#[cfg(test)]
fn test_find_all_elems<T: PartialEq + PartialOrd + Eq + Ord>(arr: &[T]) {
let mut i = 0;
while i < arr.len() {
assert!(test_match(&arr[i], arr.binary_search_(&arr[i])));
i += 1;
}
}
#[cfg(test)]
fn test_miss_all_elems<T: PartialEq + PartialOrd + Eq + Ord + Debug>(arr: &[T], misses: &[T]) {
let mut i = 0;
while i < misses.len() {
let res = arr.binary_search_(&misses[i]);
println!("{:?} == {:?} ?", misses[i], res);
assert!(!test_match(&misses[i], arr.binary_search_(&misses[i])));
i += 1;
}
}
#[cfg(test)]
fn test_match<T: PartialEq>(b: &T, a: Option<&T>) -> bool {
match a {
None => false,
Some(t) => t == b
}
}
#[test]
fn should_find_all_elements() {
let arr_odd = [1_i32, 2, 4, 6, 7, 8, 9];
let arr_even = [1_i32, 2, 5, 6, 7, 8, 9, 42];
let arr_double = [1_i32, 1, 2, 2, 6, 8, 22];
let arr_one = [234986325_i32];
let arr_two = [3044_i32, 8393];
let arr_three = [12_i32, 23, 34];
test_find_all_elems(&arr_odd);
test_find_all_elems(&arr_even);
test_find_all_elems(&arr_double);
test_find_all_elems(&arr_one);
test_find_all_elems(&arr_two);
test_find_all_elems(&arr_three);
}
#[test]
fn should_not_find_missing_elements() {
let arr_odd = [1_i32, 2, 4, 6, 7, 8, 9];
let arr_even = [1_i32, 2, 5, 6, 7, 8, 9, 42];
let arr_double = [1_i32, 1, 2, 2, 6, 8, 22];
let arr_one = [234986325_i32];
let arr_two = [3044_i32, 8393];
let arr_three = [12_i32, 23, 34];
test_miss_all_elems(&arr_odd, &[-22, 0, 3, 5, 34938, 10, 11, 12]);
test_miss_all_elems(&arr_even, &[-1, 0, 3, 34938, 10, 11, 12]);
test_miss_all_elems(&arr_double, &[-1, 0, 3, 4, 34938, 10, 11, 12, 234, 234, 33]);
test_miss_all_elems(&arr_one, &[-1, 0, 3, 34938, 10, 11, 12, 234, 234, 33]);
test_miss_all_elems(&arr_two, &[-1, 0, 3, 34938, 10, 11, 12, 234, 234, 33]);
test_miss_all_elems(&arr_three, &[-2, 0, 1, 2, 3, 34938, 10, 11, 234, 234, 33]);
}