Change HashMap to BTreeMap.

This commit is contained in:
Stephen Chung
2021-03-23 12:13:53 +08:00
parent 7a0032fc89
commit f70225ca1d
19 changed files with 139 additions and 214 deletions

View File

@@ -6,15 +6,13 @@ use crate::stdlib::{
borrow::Borrow,
boxed::Box,
cmp::Ordering,
collections::HashMap,
fmt,
fmt::{Debug, Display},
hash::{BuildHasher, Hash, Hasher},
iter::FromIterator,
ops::{Add, AddAssign, Deref, DerefMut, Sub, SubAssign},
ops::{Add, AddAssign, Deref, Sub, SubAssign},
str::FromStr,
string::{String, ToString},
vec::Vec,
};
use crate::Shared;
@@ -106,62 +104,6 @@ pub(crate) fn combine_hashes(a: u64, b: u64) -> u64 {
a ^ b
}
/// _(INTERNALS)_ A type that wraps a [`HashMap`] and implements [`Hash`].
/// Exported under the `internals` feature only.
#[derive(Clone, Default)]
pub struct HashableHashMap<K, T, H: BuildHasher>(HashMap<K, T, H>);
impl<K, T, H: BuildHasher> From<HashMap<K, T, H>> for HashableHashMap<K, T, H> {
#[inline(always)]
fn from(value: HashMap<K, T, H>) -> Self {
Self(value)
}
}
impl<K, T, H: BuildHasher> AsRef<HashMap<K, T, H>> for HashableHashMap<K, T, H> {
#[inline(always)]
fn as_ref(&self) -> &HashMap<K, T, H> {
&self.0
}
}
impl<K, T, H: BuildHasher> AsMut<HashMap<K, T, H>> for HashableHashMap<K, T, H> {
#[inline(always)]
fn as_mut(&mut self) -> &mut HashMap<K, T, H> {
&mut self.0
}
}
impl<K, T, H: BuildHasher> Deref for HashableHashMap<K, T, H> {
type Target = HashMap<K, T, H>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<K, T, H: BuildHasher> DerefMut for HashableHashMap<K, T, H> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<K: Debug, T: Debug, H: BuildHasher> Debug for HashableHashMap<K, T, H> {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<K: Hash + Ord, T: Hash, H: BuildHasher> Hash for HashableHashMap<K, T, H> {
#[inline(always)]
fn hash<B: Hasher>(&self, state: &mut B) {
let mut keys: Vec<_> = self.0.keys().collect();
keys.sort();
keys.into_iter().for_each(|key| {
key.hash(state);
self.0.get(&key).unwrap().hash(state);
});
}
}
/// The system immutable string type.
///
/// An [`ImmutableString`] wraps an [`Rc`][std::rc::Rc]`<`[`String`]`>`