Skip to content

Commit

Permalink
Replace cmp_two_gets with is_ascending
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 22, 2021
1 parent 0d577e6 commit d6aa5bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
9 changes: 1 addition & 8 deletions utils/zerovec/src/map/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub trait ZeroMapKV<'a>: Sized {
/// Compare this type with a `Self::GetType`. This must produce the same result as
/// if `g` were converted to `Self`
fn cmp_get(&self, g: &Self::GetType) -> Ordering;
/// Compare two `Self::GetType`s, as if they were both converted to `Self`
fn cmp_two_gets(g1: &Self::GetType, g2: &Self::GetType) -> Ordering;
/// Obtain a version of this type suitable for serialization
///
/// This uses a callback because it's not possible to return owned-or-borrowed
Expand All @@ -57,9 +55,6 @@ macro_rules! impl_sized_kv {
self.cmp(&$ty::from_unaligned(g))
}

fn cmp_two_gets(g1: &Self::GetType, g2: &Self::GetType) -> Ordering {
$ty::from_unaligned(g1).cmp(&$ty::from_unaligned(g2))
}
fn with_ser<R>(g: &Self::GetType, f: impl FnOnce(&Self) -> R) -> R {
f(&Self::from_unaligned(g))
}
Expand Down Expand Up @@ -88,9 +83,7 @@ impl<'a> ZeroMapKV<'a> for String {
fn cmp_get(&self, g: &str) -> Ordering {
(&**self).cmp(g)
}
fn cmp_two_gets(g1: &str, g2: &str) -> Ordering {
g1.cmp(g2)
}

fn with_ser<R>(g: &str, f: impl FnOnce(&str) -> R) -> R {
f(g)
}
Expand Down
12 changes: 2 additions & 10 deletions utils/zerovec/src/map/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use super::{ZeroMap, ZeroMapKV, ZeroVecLike};
use serde::de::{self, MapAccess, Visitor};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::Ordering;
use std::fmt;
use std::marker::PhantomData;

Expand Down Expand Up @@ -108,15 +107,8 @@ where
"Mismatched key and value sizes in ZeroMap",
));
}
if keys.len() > 0 {
let mut prev = keys.get(0).unwrap();
for i in 1..keys.len() {
let cur = keys.get(i).unwrap();
if K::cmp_two_gets(&cur, &prev) != Ordering::Greater {
return Err(de::Error::custom("ZeroMap deserializing keys out of order"));
}
prev = cur;
}
if !keys.is_ascending() {
return Err(de::Error::custom("ZeroMap deserializing keys out of order"));
}
Ok(Self { keys, values })
}
Expand Down
20 changes: 20 additions & 0 deletions utils/zerovec/src/map/vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::ule::*;
use crate::VarZeroVec;
use crate::ZeroVec;
use std::cmp::Ordering;
use std::mem;

/// Trait abstracting over [`ZeroVec`] and [`VarZeroVec`], for use in [`ZeroMap`]. You
Expand Down Expand Up @@ -38,6 +39,8 @@ pub trait ZeroVecLike<'a, T> {
fn clear(&mut self);
/// Reserve space for `addl` additional elements
fn reserve(&mut self, addl: usize);
/// Check if this vector is in ascending order according to `T`s `Ord` impl
fn is_ascending(&self) -> bool;
}

impl<'a, T> ZeroVecLike<'a, T> for ZeroVec<'a, T>
Expand Down Expand Up @@ -80,6 +83,11 @@ where
fn reserve(&mut self, addl: usize) {
self.make_mut().reserve(addl)
}
fn is_ascending(&self) -> bool {
self.as_slice()
.windows(2)
.all(|w| T::from_unaligned(&w[1]).cmp(&T::from_unaligned(&w[0])) == Ordering::Greater)
}
}

impl<'a, T> ZeroVecLike<'a, T> for VarZeroVec<'a, T>
Expand Down Expand Up @@ -123,4 +131,16 @@ where
fn reserve(&mut self, addl: usize) {
self.make_mut().reserve(addl)
}
fn is_ascending(&self) -> bool {
if self.len() > 0 {
let mut prev = self.get(0).unwrap();
for element in self.iter().skip(1) {
if element.cmp(prev) != Ordering::Greater {
return false;
}
prev = element;
}
}
true
}
}

0 comments on commit d6aa5bb

Please sign in to comment.