Vendor things
This commit is contained in:
parent
5deceec006
commit
977e3c17e5
19434 changed files with 10682014 additions and 0 deletions
12
third-party/vendor/hashlink/src/lib.rs
vendored
Normal file
12
third-party/vendor/hashlink/src/lib.rs
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#![no_std]
|
||||
extern crate alloc;
|
||||
|
||||
pub mod linked_hash_map;
|
||||
pub mod linked_hash_set;
|
||||
pub mod lru_cache;
|
||||
#[cfg(feature = "serde_impl")]
|
||||
pub mod serde;
|
||||
|
||||
pub use linked_hash_map::LinkedHashMap;
|
||||
pub use linked_hash_set::LinkedHashSet;
|
||||
pub use lru_cache::LruCache;
|
||||
2180
third-party/vendor/hashlink/src/linked_hash_map.rs
vendored
Normal file
2180
third-party/vendor/hashlink/src/linked_hash_map.rs
vendored
Normal file
File diff suppressed because it is too large
Load diff
766
third-party/vendor/hashlink/src/linked_hash_set.rs
vendored
Normal file
766
third-party/vendor/hashlink/src/linked_hash_set.rs
vendored
Normal file
|
|
@ -0,0 +1,766 @@
|
|||
use core::{
|
||||
borrow::Borrow,
|
||||
fmt,
|
||||
hash::{BuildHasher, Hash, Hasher},
|
||||
iter::{Chain, FromIterator},
|
||||
ops::{BitAnd, BitOr, BitXor, Sub},
|
||||
};
|
||||
|
||||
use hashbrown::hash_map::DefaultHashBuilder;
|
||||
|
||||
use crate::linked_hash_map::{self, LinkedHashMap, TryReserveError};
|
||||
|
||||
pub struct LinkedHashSet<T, S = DefaultHashBuilder> {
|
||||
map: LinkedHashMap<T, (), S>,
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq> LinkedHashSet<T, DefaultHashBuilder> {
|
||||
#[inline]
|
||||
pub fn new() -> LinkedHashSet<T, DefaultHashBuilder> {
|
||||
LinkedHashSet {
|
||||
map: LinkedHashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_capacity(capacity: usize) -> LinkedHashSet<T, DefaultHashBuilder> {
|
||||
LinkedHashSet {
|
||||
map: LinkedHashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> LinkedHashSet<T, S> {
|
||||
#[inline]
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.map.capacity()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn iter(&self) -> Iter<'_, T> {
|
||||
Iter {
|
||||
iter: self.map.keys(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.map.is_empty()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn drain(&mut self) -> Drain<T> {
|
||||
Drain {
|
||||
iter: self.map.drain(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
self.map.clear()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn retain<F>(&mut self, mut f: F)
|
||||
where
|
||||
F: FnMut(&T) -> bool,
|
||||
{
|
||||
self.map.retain(|k, _| f(k));
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
pub fn with_hasher(hasher: S) -> LinkedHashSet<T, S> {
|
||||
LinkedHashSet {
|
||||
map: LinkedHashMap::with_hasher(hasher),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> LinkedHashSet<T, S> {
|
||||
LinkedHashSet {
|
||||
map: LinkedHashMap::with_capacity_and_hasher(capacity, hasher),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn hasher(&self) -> &S {
|
||||
self.map.hasher()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn reserve(&mut self, additional: usize) {
|
||||
self.map.reserve(additional)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
|
||||
self.map.try_reserve(additional)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shrink_to_fit(&mut self) {
|
||||
self.map.shrink_to_fit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn difference<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Difference<'a, T, S> {
|
||||
Difference {
|
||||
iter: self.iter(),
|
||||
other,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn symmetric_difference<'a>(
|
||||
&'a self,
|
||||
other: &'a LinkedHashSet<T, S>,
|
||||
) -> SymmetricDifference<'a, T, S> {
|
||||
SymmetricDifference {
|
||||
iter: self.difference(other).chain(other.difference(self)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn intersection<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Intersection<'a, T, S> {
|
||||
Intersection {
|
||||
iter: self.iter(),
|
||||
other,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn union<'a>(&'a self, other: &'a LinkedHashSet<T, S>) -> Union<'a, T, S> {
|
||||
Union {
|
||||
iter: self.iter().chain(other.difference(self)),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
self.map.contains_key(value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
self.map.raw_entry().from_key(value).map(|p| p.0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_or_insert(&mut self, value: T) -> &T {
|
||||
self.map
|
||||
.raw_entry_mut()
|
||||
.from_key(&value)
|
||||
.or_insert(value, ())
|
||||
.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
F: FnOnce(&Q) -> T,
|
||||
{
|
||||
self.map
|
||||
.raw_entry_mut()
|
||||
.from_key(value)
|
||||
.or_insert_with(|| (f(value), ()))
|
||||
.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_disjoint(&self, other: &LinkedHashSet<T, S>) -> bool {
|
||||
self.iter().all(|v| !other.contains(v))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_subset(&self, other: &LinkedHashSet<T, S>) -> bool {
|
||||
self.iter().all(|v| other.contains(v))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_superset(&self, other: &LinkedHashSet<T, S>) -> bool {
|
||||
other.is_subset(self)
|
||||
}
|
||||
|
||||
/// Inserts the given value into the set.
|
||||
///
|
||||
/// If the set did not have this value present, inserts it at the *back* of the internal linked
|
||||
/// list and returns true, otherwise it moves the existing value to the *back* of the internal
|
||||
/// linked list and returns false.
|
||||
#[inline]
|
||||
pub fn insert(&mut self, value: T) -> bool {
|
||||
self.map.insert(value, ()).is_none()
|
||||
}
|
||||
|
||||
/// Adds the given value to the set, replacing the existing value.
|
||||
///
|
||||
/// If a previous value existed, returns the replaced value. In this case, the value's position
|
||||
/// in the internal linked list is *not* changed.
|
||||
#[inline]
|
||||
pub fn replace(&mut self, value: T) -> Option<T> {
|
||||
match self.map.entry(value) {
|
||||
linked_hash_map::Entry::Occupied(occupied) => Some(occupied.replace_key()),
|
||||
linked_hash_map::Entry::Vacant(vacant) => {
|
||||
vacant.insert(());
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
self.map.remove(value).is_some()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
match self.map.raw_entry_mut().from_key(value) {
|
||||
linked_hash_map::RawEntryMut::Occupied(occupied) => Some(occupied.remove_entry().0),
|
||||
linked_hash_map::RawEntryMut::Vacant(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn front(&self) -> Option<&T> {
|
||||
self.map.front().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
self.map.pop_front().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn back(&self) -> Option<&T> {
|
||||
self.map.back().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pop_back(&mut self) -> Option<T> {
|
||||
self.map.pop_back().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_front<Q: ?Sized>(&mut self, value: &Q) -> bool
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
match self.map.raw_entry_mut().from_key(value) {
|
||||
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
|
||||
occupied.to_front();
|
||||
true
|
||||
}
|
||||
linked_hash_map::RawEntryMut::Vacant(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_back<Q: ?Sized>(&mut self, value: &Q) -> bool
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Hash + Eq,
|
||||
{
|
||||
match self.map.raw_entry_mut().from_key(value) {
|
||||
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
|
||||
occupied.to_back();
|
||||
true
|
||||
}
|
||||
linked_hash_map::RawEntryMut::Vacant(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn retain_with_order<F>(&mut self, mut f: F)
|
||||
where
|
||||
F: FnMut(&T) -> bool,
|
||||
{
|
||||
self.map.retain_with_order(|k, _| f(k));
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + Clone, S: BuildHasher + Clone> Clone for LinkedHashSet<T, S> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
let map = self.map.clone();
|
||||
Self { map }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> PartialEq for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.len() == other.len() && self.iter().eq(other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Hash for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
for e in self {
|
||||
e.hash(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Eq for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
}
|
||||
|
||||
impl<T, S> fmt::Debug for LinkedHashSet<T, S>
|
||||
where
|
||||
T: fmt::Debug,
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_set().entries(self.iter()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> FromIterator<T> for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
#[inline]
|
||||
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> LinkedHashSet<T, S> {
|
||||
let mut set = LinkedHashSet::with_hasher(Default::default());
|
||||
set.extend(iter);
|
||||
set
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Extend<T> for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
||||
self.map.extend(iter.into_iter().map(|k| (k, ())));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Extend<&'a T> for LinkedHashSet<T, S>
|
||||
where
|
||||
T: 'a + Eq + Hash + Copy,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
|
||||
self.extend(iter.into_iter().cloned());
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Default for LinkedHashSet<T, S>
|
||||
where
|
||||
S: Default,
|
||||
{
|
||||
#[inline]
|
||||
fn default() -> LinkedHashSet<T, S> {
|
||||
LinkedHashSet {
|
||||
map: LinkedHashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T, S> BitOr<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash + Clone,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
type Output = LinkedHashSet<T, S>;
|
||||
|
||||
#[inline]
|
||||
fn bitor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
|
||||
self.union(rhs).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T, S> BitAnd<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash + Clone,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
type Output = LinkedHashSet<T, S>;
|
||||
|
||||
#[inline]
|
||||
fn bitand(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
|
||||
self.intersection(rhs).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T, S> BitXor<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash + Clone,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
type Output = LinkedHashSet<T, S>;
|
||||
|
||||
#[inline]
|
||||
fn bitxor(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
|
||||
self.symmetric_difference(rhs).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, T, S> Sub<&'b LinkedHashSet<T, S>> for &'a LinkedHashSet<T, S>
|
||||
where
|
||||
T: Eq + Hash + Clone,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
type Output = LinkedHashSet<T, S>;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, rhs: &LinkedHashSet<T, S>) -> LinkedHashSet<T, S> {
|
||||
self.difference(rhs).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Iter<'a, K> {
|
||||
iter: linked_hash_map::Keys<'a, K, ()>,
|
||||
}
|
||||
|
||||
pub struct IntoIter<K> {
|
||||
iter: linked_hash_map::IntoIter<K, ()>,
|
||||
}
|
||||
|
||||
pub struct Drain<'a, K: 'a> {
|
||||
iter: linked_hash_map::Drain<'a, K, ()>,
|
||||
}
|
||||
|
||||
pub struct Intersection<'a, T, S> {
|
||||
iter: Iter<'a, T>,
|
||||
other: &'a LinkedHashSet<T, S>,
|
||||
}
|
||||
|
||||
pub struct Difference<'a, T, S> {
|
||||
iter: Iter<'a, T>,
|
||||
other: &'a LinkedHashSet<T, S>,
|
||||
}
|
||||
|
||||
pub struct SymmetricDifference<'a, T, S> {
|
||||
iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
|
||||
}
|
||||
|
||||
pub struct Union<'a, T, S> {
|
||||
iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
|
||||
}
|
||||
|
||||
impl<'a, T, S> IntoIterator for &'a LinkedHashSet<T, S> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = Iter<'a, T>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> Iter<'a, T> {
|
||||
self.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> IntoIterator for LinkedHashSet<T, S> {
|
||||
type Item = T;
|
||||
type IntoIter = IntoIter<T>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> IntoIter<T> {
|
||||
IntoIter {
|
||||
iter: self.map.into_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K> Clone for Iter<'a, K> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Iter<'a, K> {
|
||||
Iter {
|
||||
iter: self.iter.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, K> Iterator for Iter<'a, K> {
|
||||
type Item = &'a K;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a K> {
|
||||
self.iter.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K> ExactSizeIterator for Iter<'a, K> {}
|
||||
|
||||
impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a T> {
|
||||
self.iter.next_back()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K: fmt::Debug> fmt::Debug for Iter<'a, K> {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_list().entries(self.clone()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K> Iterator for IntoIter<K> {
|
||||
type Item = K;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<K> {
|
||||
self.iter.next().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K> ExactSizeIterator for IntoIter<K> {}
|
||||
|
||||
impl<K> DoubleEndedIterator for IntoIter<K> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<K> {
|
||||
self.iter.next_back().map(|(k, _)| k)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K> Iterator for Drain<'a, K> {
|
||||
type Item = K;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<K> {
|
||||
self.iter.next().map(|(k, _)| k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K> DoubleEndedIterator for Drain<'a, K> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<K> {
|
||||
self.iter.next_back().map(|(k, _)| k)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K> ExactSizeIterator for Drain<'a, K> {}
|
||||
|
||||
impl<'a, T, S> Clone for Intersection<'a, T, S> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Intersection<'a, T, S> {
|
||||
Intersection {
|
||||
iter: self.iter.clone(),
|
||||
..*self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Iterator for Intersection<'a, T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
type Item = &'a T;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
loop {
|
||||
match self.iter.next() {
|
||||
None => return None,
|
||||
Some(elt) => {
|
||||
if self.other.contains(elt) {
|
||||
return Some(elt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let (_, upper) = self.iter.size_hint();
|
||||
(0, upper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> fmt::Debug for Intersection<'a, T, S>
|
||||
where
|
||||
T: fmt::Debug + Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_list().entries(self.clone()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Clone for Difference<'a, T, S> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Difference<'a, T, S> {
|
||||
Difference {
|
||||
iter: self.iter.clone(),
|
||||
..*self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Iterator for Difference<'a, T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
type Item = &'a T;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
loop {
|
||||
match self.iter.next() {
|
||||
None => return None,
|
||||
Some(elt) => {
|
||||
if !self.other.contains(elt) {
|
||||
return Some(elt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let (_, upper) = self.iter.size_hint();
|
||||
(0, upper)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> fmt::Debug for Difference<'a, T, S>
|
||||
where
|
||||
T: fmt::Debug + Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_list().entries(self.clone()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Clone for SymmetricDifference<'a, T, S> {
|
||||
#[inline]
|
||||
fn clone(&self) -> SymmetricDifference<'a, T, S> {
|
||||
SymmetricDifference {
|
||||
iter: self.iter.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
type Item = &'a T;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
self.iter.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> fmt::Debug for SymmetricDifference<'a, T, S>
|
||||
where
|
||||
T: fmt::Debug + Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_list().entries(self.clone()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Clone for Union<'a, T, S> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Union<'a, T, S> {
|
||||
Union {
|
||||
iter: self.iter.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> fmt::Debug for Union<'a, T, S>
|
||||
where
|
||||
T: fmt::Debug + Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_list().entries(self.clone()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> Iterator for Union<'a, T, S>
|
||||
where
|
||||
T: Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
type Item = &'a T;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
self.iter.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.iter.size_hint()
|
||||
}
|
||||
}
|
||||
292
third-party/vendor/hashlink/src/lru_cache.rs
vendored
Normal file
292
third-party/vendor/hashlink/src/lru_cache.rs
vendored
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
use core::{
|
||||
borrow::Borrow,
|
||||
fmt,
|
||||
hash::{BuildHasher, Hash},
|
||||
usize,
|
||||
};
|
||||
|
||||
use hashbrown::hash_map;
|
||||
|
||||
use crate::linked_hash_map::{self, LinkedHashMap};
|
||||
|
||||
pub use crate::linked_hash_map::{
|
||||
Drain, Entry, IntoIter, Iter, IterMut, OccupiedEntry, RawEntryBuilder, RawEntryBuilderMut,
|
||||
RawOccupiedEntryMut, RawVacantEntryMut, VacantEntry,
|
||||
};
|
||||
|
||||
pub struct LruCache<K, V, S = hash_map::DefaultHashBuilder> {
|
||||
map: LinkedHashMap<K, V, S>,
|
||||
max_size: usize,
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash, V> LruCache<K, V> {
|
||||
#[inline]
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
LruCache {
|
||||
map: LinkedHashMap::new(),
|
||||
max_size: capacity,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new unbounded `LruCache` that does not automatically evict entries.
|
||||
///
|
||||
/// A simple convenience method that is equivalent to `LruCache::new(usize::MAX)`
|
||||
#[inline]
|
||||
pub fn new_unbounded() -> Self {
|
||||
LruCache::new(usize::MAX)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> LruCache<K, V, S> {
|
||||
#[inline]
|
||||
pub fn with_hasher(capacity: usize, hash_builder: S) -> Self {
|
||||
LruCache {
|
||||
map: LinkedHashMap::with_hasher(hash_builder),
|
||||
max_size: capacity,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.max_size
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.map.is_empty()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
self.map.clear();
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn iter(&self) -> Iter<K, V> {
|
||||
self.map.iter()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn iter_mut(&mut self) -> IterMut<K, V> {
|
||||
self.map.iter_mut()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn drain(&mut self) -> Drain<K, V> {
|
||||
self.map.drain()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash, V, S> LruCache<K, V, S>
|
||||
where
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
pub fn contains_key<Q>(&mut self, key: &Q) -> bool
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.get_mut(key).is_some()
|
||||
}
|
||||
|
||||
/// Insert a new value into the `LruCache`.
|
||||
///
|
||||
/// If necessary, will remove the value at the front of the LRU list to make room.
|
||||
#[inline]
|
||||
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
|
||||
let old_val = self.map.insert(k, v);
|
||||
if self.len() > self.capacity() {
|
||||
self.remove_lru();
|
||||
}
|
||||
old_val
|
||||
}
|
||||
|
||||
/// Get the value for the given key, *without* marking the value as recently used and moving it
|
||||
/// to the back of the LRU list.
|
||||
#[inline]
|
||||
pub fn peek<Q>(&self, k: &Q) -> Option<&V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.map.get(k)
|
||||
}
|
||||
|
||||
/// Get the value for the given key mutably, *without* marking the value as recently used and
|
||||
/// moving it to the back of the LRU list.
|
||||
#[inline]
|
||||
pub fn peek_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.map.get_mut(k)
|
||||
}
|
||||
|
||||
/// Retrieve the given key, marking it as recently used and moving it to the back of the LRU
|
||||
/// list.
|
||||
#[inline]
|
||||
pub fn get<Q>(&mut self, k: &Q) -> Option<&V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.get_mut(k).map(|v| &*v)
|
||||
}
|
||||
|
||||
/// Retrieve the given key, marking it as recently used and moving it to the back of the LRU
|
||||
/// list.
|
||||
#[inline]
|
||||
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
match self.map.raw_entry_mut().from_key(k) {
|
||||
linked_hash_map::RawEntryMut::Occupied(mut occupied) => {
|
||||
occupied.to_back();
|
||||
Some(occupied.into_mut())
|
||||
}
|
||||
linked_hash_map::RawEntryMut::Vacant(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// If the returned entry is vacant, it will always have room to insert a single value. By
|
||||
/// using the entry API, you can exceed the configured capacity by 1.
|
||||
///
|
||||
/// The returned entry is not automatically moved to the back of the LRU list. By calling
|
||||
/// `Entry::to_back` / `Entry::to_front` you can manually control the position of this entry in
|
||||
/// the LRU list.
|
||||
#[inline]
|
||||
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S> {
|
||||
if self.len() > self.capacity() {
|
||||
self.remove_lru();
|
||||
}
|
||||
self.map.entry(key)
|
||||
}
|
||||
|
||||
/// The constructed raw entry is never automatically moved to the back of the LRU list. By
|
||||
/// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
|
||||
/// entry in the LRU list.
|
||||
#[inline]
|
||||
pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S> {
|
||||
self.map.raw_entry()
|
||||
}
|
||||
|
||||
/// If the constructed raw entry is vacant, it will always have room to insert a single value.
|
||||
/// By using the raw entry API, you can exceed the configured capacity by 1.
|
||||
///
|
||||
/// The constructed raw entry is never automatically moved to the back of the LRU list. By
|
||||
/// calling `Entry::to_back` / `Entry::to_front` you can manually control the position of this
|
||||
/// entry in the LRU list.
|
||||
#[inline]
|
||||
pub fn raw_entry_mut(&mut self) -> RawEntryBuilderMut<'_, K, V, S> {
|
||||
if self.len() > self.capacity() {
|
||||
self.remove_lru();
|
||||
}
|
||||
self.map.raw_entry_mut()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.map.remove(k)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.map.remove_entry(k)
|
||||
}
|
||||
|
||||
/// Set the new cache capacity for the `LruCache`.
|
||||
///
|
||||
/// If there are more entries in the `LruCache` than the new capacity will allow, they are
|
||||
/// removed.
|
||||
#[inline]
|
||||
pub fn set_capacity(&mut self, capacity: usize) {
|
||||
for _ in capacity..self.len() {
|
||||
self.remove_lru();
|
||||
}
|
||||
self.max_size = capacity;
|
||||
}
|
||||
|
||||
/// Remove the least recently used entry and return it.
|
||||
///
|
||||
/// If the `LruCache` is empty this will return None.
|
||||
#[inline]
|
||||
pub fn remove_lru(&mut self) -> Option<(K, V)> {
|
||||
self.map.pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Hash + Eq + Clone, V: Clone, S: BuildHasher + Clone> Clone for LruCache<K, V, S> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
LruCache {
|
||||
map: self.map.clone(),
|
||||
max_size: self.max_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for LruCache<K, V, S> {
|
||||
#[inline]
|
||||
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
|
||||
for (k, v) in iter {
|
||||
self.insert(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> IntoIterator for LruCache<K, V, S> {
|
||||
type Item = (K, V);
|
||||
type IntoIter = IntoIter<K, V>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> IntoIter<K, V> {
|
||||
self.map.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K, V, S> IntoIterator for &'a LruCache<K, V, S> {
|
||||
type Item = (&'a K, &'a V);
|
||||
type IntoIter = Iter<'a, K, V>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> Iter<'a, K, V> {
|
||||
self.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K, V, S> IntoIterator for &'a mut LruCache<K, V, S> {
|
||||
type Item = (&'a K, &'a mut V);
|
||||
type IntoIter = IterMut<'a, K, V>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> IterMut<'a, K, V> {
|
||||
self.iter_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> fmt::Debug for LruCache<K, V, S>
|
||||
where
|
||||
K: fmt::Debug,
|
||||
V: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_map().entries(self.iter().rev()).finish()
|
||||
}
|
||||
}
|
||||
161
third-party/vendor/hashlink/src/serde.rs
vendored
Normal file
161
third-party/vendor/hashlink/src/serde.rs
vendored
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
use core::{
|
||||
fmt::{self, Formatter},
|
||||
hash::{BuildHasher, Hash},
|
||||
marker::PhantomData,
|
||||
};
|
||||
|
||||
use serde::{
|
||||
de::{MapAccess, SeqAccess, Visitor},
|
||||
ser::{SerializeMap, SerializeSeq},
|
||||
Deserialize, Deserializer, Serialize, Serializer,
|
||||
};
|
||||
|
||||
use crate::{LinkedHashMap, LinkedHashSet};
|
||||
|
||||
// LinkedHashMap impls
|
||||
|
||||
impl<K, V, S> Serialize for LinkedHashMap<K, V, S>
|
||||
where
|
||||
K: Serialize + Eq + Hash,
|
||||
V: Serialize,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<T: Serializer>(&self, serializer: T) -> Result<T::Ok, T::Error> {
|
||||
let mut map_serializer = serializer.serialize_map(Some(self.len()))?;
|
||||
for (k, v) in self {
|
||||
map_serializer.serialize_key(k)?;
|
||||
map_serializer.serialize_value(v)?;
|
||||
}
|
||||
map_serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, K, V, S> Deserialize<'de> for LinkedHashMap<K, V, S>
|
||||
where
|
||||
K: Deserialize<'de> + Eq + Hash,
|
||||
V: Deserialize<'de>,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
#[derive(Debug)]
|
||||
pub struct LinkedHashMapVisitor<K, V, S> {
|
||||
marker: PhantomData<LinkedHashMap<K, V, S>>,
|
||||
}
|
||||
|
||||
impl<K, V, S> LinkedHashMapVisitor<K, V, S> {
|
||||
fn new() -> Self {
|
||||
LinkedHashMapVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Default for LinkedHashMapVisitor<K, V, S> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, K, V, S> Visitor<'de> for LinkedHashMapVisitor<K, V, S>
|
||||
where
|
||||
K: Deserialize<'de> + Eq + Hash,
|
||||
V: Deserialize<'de>,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
type Value = LinkedHashMap<K, V, S>;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
||||
write!(formatter, "a map")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Self::Value, M::Error> {
|
||||
let mut values = LinkedHashMap::with_capacity_and_hasher(
|
||||
map.size_hint().unwrap_or(0),
|
||||
S::default(),
|
||||
);
|
||||
|
||||
while let Some((k, v)) = map.next_entry()? {
|
||||
values.insert(k, v);
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_map(LinkedHashMapVisitor::default())
|
||||
}
|
||||
}
|
||||
|
||||
// LinkedHashSet impls
|
||||
|
||||
impl<T, S> Serialize for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Serialize + Eq + Hash,
|
||||
S: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<U: Serializer>(&self, serializer: U) -> Result<U::Ok, U::Error> {
|
||||
let mut seq_serializer = serializer.serialize_seq(Some(self.len()))?;
|
||||
for v in self {
|
||||
seq_serializer.serialize_element(v)?;
|
||||
}
|
||||
seq_serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T, S> Deserialize<'de> for LinkedHashSet<T, S>
|
||||
where
|
||||
T: Deserialize<'de> + Eq + Hash,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
#[derive(Debug)]
|
||||
pub struct LinkedHashSetVisitor<T, S> {
|
||||
marker: PhantomData<LinkedHashSet<T, S>>,
|
||||
}
|
||||
|
||||
impl<T, S> LinkedHashSetVisitor<T, S> {
|
||||
fn new() -> Self {
|
||||
LinkedHashSetVisitor {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Default for LinkedHashSetVisitor<T, S> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T, S> Visitor<'de> for LinkedHashSetVisitor<T, S>
|
||||
where
|
||||
T: Deserialize<'de> + Eq + Hash,
|
||||
S: BuildHasher + Default,
|
||||
{
|
||||
type Value = LinkedHashSet<T, S>;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
|
||||
write!(formatter, "a sequence")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<SA: SeqAccess<'de>>(self, mut seq: SA) -> Result<Self::Value, SA::Error> {
|
||||
let mut values = LinkedHashSet::with_capacity_and_hasher(
|
||||
seq.size_hint().unwrap_or(0),
|
||||
S::default(),
|
||||
);
|
||||
|
||||
while let Some(v) = seq.next_element()? {
|
||||
values.insert(v);
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_seq(LinkedHashSetVisitor::default())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue