Bug 1332633 - Part 1: Implement ComputeDistance trait.

Introduce ComputeDistance trait, which implement compute_distance and
compute_squared_distance.

For vector, compute_squared_distance is necessary because we use Euclidean
distance as the distance between two values. The easier way to implement
compute_squared_distance is to square the result from compute_distance, but
for some property values, they may have many components, e.g. (v1, v2, v3).
If we just square the result from compute_distance, the computation is
(sqrt(v1^2 + v2^2 + v3^2))^2. There are two redundant operators:
"square-root" and then "square". In order to avoid this, we should
implement compute_squared_distance separately for these types.

MozReview-Commit-ID: LmmrUXYlDb6
This commit is contained in:
Boris Chiou 2017-03-18 19:47:04 +08:00
parent 17dc598d99
commit 57f87007f2
7 changed files with 733 additions and 11 deletions

View file

@ -818,7 +818,7 @@ ${helpers.single_keyword("font-variant-caps",
}
pub mod computed_value {
use properties::animated_properties::Interpolate;
use properties::animated_properties::{ComputeDistance, Interpolate};
use std::fmt;
use style_traits::ToCss;
use values::CSSFloat;
@ -850,6 +850,17 @@ ${helpers.single_keyword("font-variant-caps",
}
}
}
impl ComputeDistance for T {
#[inline]
fn compute_distance(&self, other: &Self) -> Result<f64, ()> {
match (*self, *other) {
(T::Number(ref number), T::Number(ref other)) =>
number.compute_distance(other),
_ => Err(()),
}
}
}
}
#[inline]