Use `Ord` instead of `PartialOrd` for the doc id
Technically not required, but being stricter for the doc prevents the (very unlikely) case of ending up with non-Ord stuff being used as a doc (thinking of NaN) which would break sort stability.
- Id
- 2894ca9ab37826b7a02ba4e6a351ca6d378d5d69
- Author
- Caio
- Commit time
- 2020-01-21T18:41:34+01:00
Modified tique/src/conditional_collector/topk.rs
heap: BinaryHeap<Reverse<Scored<S, D>>>,
}
-impl<T: PartialOrd, D: PartialOrd> AscendingTopK<T, D> {
+impl<T: PartialOrd, D: Ord> AscendingTopK<T, D> {
pub(crate) fn new(limit: usize) -> Self {
Self {
limit,
}
}
-impl<T: PartialOrd, D: PartialOrd> DescendingTopK<T, D> {
+impl<T: PartialOrd, D: Ord> DescendingTopK<T, D> {
pub(crate) fn new(limit: usize) -> Self {
Self {
limit,
pub doc: D,
}
-impl<S: PartialOrd, D: PartialOrd> Scored<S, D> {
+impl<S: PartialOrd, D: Ord> Scored<S, D> {
pub(crate) fn new(score: S, doc: D) -> Self {
Self { score, doc }
}
}
-impl<S: PartialOrd, D: PartialOrd> PartialOrd for Scored<S, D> {
+impl<S: PartialOrd, D: Ord> PartialOrd for Scored<S, D> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
-impl<S: PartialOrd, D: PartialOrd> Ord for Scored<S, D> {
+impl<S: PartialOrd, D: Ord> Ord for Scored<S, D> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
// Highest score first
match self.score.partial_cmp(&other.score) {
Some(Ordering::Equal) | None => {
// Break even by lowest id
- other.doc.partial_cmp(&self.doc).unwrap_or(Ordering::Equal)
+ other.doc.cmp(&self.doc)
}
Some(rest) => rest,
}
}
}
-impl<S: PartialOrd, D: PartialOrd> PartialEq for Scored<S, D> {
+impl<S: PartialOrd, D: Ord> PartialEq for Scored<S, D> {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
-impl<S: PartialOrd, D: PartialOrd> Eq for Scored<S, D> {}
+impl<S: PartialOrd, D: Ord> Eq for Scored<S, D> {}
#[cfg(test)]
mod tests {