use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::Hash;
use std::iter;
use std::iter::{Product, Sum};
use crate::Iterable;
pub trait Map<Key, Value> {
type This<K, V>;
#[inline]
fn add(self, key: Key, value: Value) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
self.into_iter().chain(iter::once((key, value))).collect()
}
#[inline]
fn add_multi(self, entries: impl IntoIterator<Item = (Key, Value)>) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
self.into_iter().chain(entries).collect()
}
fn all(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> bool;
fn any(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> bool;
fn collect<B>(&self) -> B
where
Key: Clone,
Value: Clone,
B: FromIterator<(Key, Value)>;
#[inline]
fn collect_to<B>(self) -> B
where
B: FromIterator<(Key, Value)>,
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().collect()
}
fn count_by(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> usize;
fn count_unique(&self) -> usize
where
Value: Eq + Hash;
#[inline]
fn delete(self, key: &Key) -> Self
where
Key: PartialEq,
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
self.into_iter().filter_map(|(k, v)| if &k != key { Some((k, v)) } else { None }).collect()
}
#[inline]
fn delete_multi<'a>(self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> Self
where
Key: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
let removed: HashSet<&Key> = HashSet::from_iter(keys.iterator());
self.into_iter().filter(|(k, _)| !removed.contains(k)).collect()
}
fn disjoint<'a>(&'a self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Key: Eq + Hash + 'a;
#[inline]
fn fill_with(mut value: impl FnMut() -> (Key, Value), size: usize) -> Self
where
Key: Clone,
Value: Clone,
Self: FromIterator<(Key, Value)>,
{
iter::repeat(value()).take(size).collect()
}
#[inline]
fn filter(self, mut predicate: impl FnMut((&Key, &Value)) -> bool) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
self.into_iter().filter(|(k, v)| predicate((k, v))).collect()
}
fn filter_ref(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> Self
where
Key: Clone,
Value: Clone;
#[inline]
fn filter_keys(self, mut predicate: impl FnMut(&Key) -> bool) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
self.into_iter().filter(|(k, _)| predicate(k)).collect()
}
#[inline]
fn filter_values(self, mut predicate: impl FnMut(&Value) -> bool) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
self.into_iter().filter(|(_, v)| predicate(v)).collect()
}
#[inline]
fn filter_map<L, W>(self, function: impl FnMut((Key, Value)) -> Option<(L, W)>) -> Self::This<L, W>
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
Self::This<L, W>: FromIterator<(L, W)>,
{
self.into_iter().filter_map(function).collect()
}
fn filter_map_ref<L, W>(&self, function: impl FnMut((&Key, &Value)) -> Option<(L, W)>) -> Self::This<L, W>
where
Self::This<L, W>: FromIterator<(L, W)>;
fn find(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> Option<(&Key, &Value)>;
fn find_map_ref<B>(&self, function: impl FnMut((&Key, &Value)) -> Option<B>) -> Option<B>;
#[inline]
fn find_map<B>(self, function: impl FnMut((Key, Value)) -> Option<B>) -> Option<B>
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().find_map(function)
}
#[inline]
fn flat_map<L, W, R>(self, function: impl FnMut((Key, Value)) -> R) -> Self::This<L, W>
where
R: IntoIterator<Item = (L, W)>,
Self: IntoIterator<Item = (Key, Value)> + Sized,
Self::This<L, W>: FromIterator<(L, W)>,
{
self.into_iter().flat_map(function).collect()
}
fn flat_map_ref<L, W, R>(&self, function: impl FnMut((&Key, &Value)) -> R) -> Self::This<L, W>
where
R: IntoIterator<Item = (L, W)>,
Self::This<L, W>: FromIterator<(L, W)>;
fn fold<B>(self, initial_value: B, function: impl FnMut(B, (Key, Value)) -> B) -> B
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().fold(initial_value, function)
}
fn fold_ref<B>(&self, initial_value: B, function: impl FnMut(B, (&Key, &Value)) -> B) -> B;
fn for_each(&self, function: impl FnMut((&Key, &Value)));
#[inline]
fn intersect<'a>(self, entries: &'a impl Iterable<Item<'a> = &'a (Key, Value)>) -> Self
where
Key: Eq + Hash + 'a,
Value: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
let retained: HashSet<(&Key, &Value)> = HashSet::from_iter(entries.iterator().map(|(k, v)| (k, v)));
self.into_iter().filter(|(k, v)| retained.contains(&(k, v))).collect()
}
#[inline]
fn into_vec(self) -> Vec<(Key, Value)>
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().collect()
}
#[inline]
fn map<L, W>(self, function: impl FnMut((Key, Value)) -> (L, W)) -> Self::This<L, W>
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
Self::This<L, W>: FromIterator<(L, W)>,
{
self.into_iter().map(function).collect()
}
fn map_ref<L, W>(&self, function: impl FnMut((&Key, &Value)) -> (L, W)) -> Self::This<L, W>
where
Self::This<L, W>: FromIterator<(L, W)>;
#[inline]
fn map_keys<L>(self, mut function: impl FnMut(&Key) -> L) -> Self::This<L, Value>
where
L: Eq + Hash,
Self: IntoIterator<Item = (Key, Value)> + Sized,
Self::This<L, Value>: FromIterator<(L, Value)>,
{
self.into_iter().map(|(k, v)| (function(&k), v)).collect()
}
#[inline]
fn map_values<W>(self, mut function: impl FnMut(&Value) -> W) -> Self::This<Key, W>
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
Self::This<Key, W>: FromIterator<(Key, W)>,
{
self.into_iter().map(|(k, v)| (k, function(&v))).collect()
}
fn max_by(&self, compare: impl FnMut((&Key, &Value), (&Key, &Value)) -> Ordering) -> Option<(&Key, &Value)>;
fn max_by_key<K>(&self, to_key: impl FnMut((&Key, &Value)) -> K) -> Option<(&Key, &Value)>
where
K: Ord;
#[inline]
fn max_of(&self) -> Option<(&Key, &Value)>
where
Key: Ord,
Value: Ord,
{
self.max_by(|x, y| x.cmp(&y))
}
fn min_by(&self, compare: impl FnMut((&Key, &Value), (&Key, &Value)) -> Ordering) -> Option<(&Key, &Value)>;
fn min_by_key<K>(&self, to_key: impl FnMut((&Key, &Value)) -> K) -> Option<(&Key, &Value)>
where
K: Ord;
#[inline]
fn min_of(&self) -> Option<(&Key, &Value)>
where
Key: Ord,
Value: Ord,
{
self.min_by(|(k1, v1), (k2, v2)| (k1, v1).cmp(&(k2, v2)))
}
fn minmax_by(
&self, compare: impl FnMut((&Key, &Value), (&Key, &Value)) -> Ordering,
) -> Option<((&Key, &Value), (&Key, &Value))>;
fn minmax_by_key<K>(&self, to_key: impl FnMut((&Key, &Value)) -> K) -> Option<((&Key, &Value), (&Key, &Value))>
where
K: Ord;
#[inline]
fn minmax_of(&self) -> Option<((&Key, &Value), (&Key, &Value))>
where
Key: Ord,
Value: Ord,
{
self.minmax_by(|(x1, x2), (y1, y2)| (x1, x2).cmp(&(y1, y2)))
}
#[inline]
fn partition(self, mut predicate: impl FnMut((&Key, &Value)) -> bool) -> (Self, Self)
where
Self: IntoIterator<Item = (Key, Value)> + Default + Extend<(Key, Value)>,
{
self.into_iter().partition(|(k, v)| predicate((k, v)))
}
fn partition_map<L1, W1, L2, W2>(
self, mut function: impl FnMut((Key, Value)) -> Result<(L1, W1), (L2, W2)>,
) -> (Self::This<L1, W1>, Self::This<L2, W2>)
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
Self::This<L1, W1>: Default + Extend<(L1, W1)>,
Self::This<L2, W2>: Default + Extend<(L2, W2)>,
{
let mut result_left: Self::This<L1, W1> = Self::This::default();
let mut result_right: Self::This<L2, W2> = Self::This::default();
for item in self.into_iter() {
match function(item) {
Ok(value) => result_left.extend(iter::once(value)),
Err(value) => result_right.extend(iter::once(value)),
}
}
(result_left, result_right)
}
fn partition_map_ref<L1, W1, L2, W2>(
&self, function: impl FnMut((&Key, &Value)) -> Result<(L1, W1), (L2, W2)>,
) -> (Self::This<L1, W1>, Self::This<L2, W2>)
where
Self::This<L1, W1>: Default + Extend<(L1, W1)>,
Self::This<L2, W2>: Default + Extend<(L2, W2)>;
#[inline]
fn product_keys(self) -> Key
where
Key: Product,
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().map(|(k, _)| k).product()
}
#[inline]
fn product_values(self) -> Value
where
Value: Product,
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().map(|(_, v)| v).product()
}
fn reduce(self, mut function: impl FnMut((Key, Value), (Key, Value)) -> (Key, Value)) -> Option<(Key, Value)>
where
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
let mut iterator = self.into_iter();
iterator.next().and_then(|value1| {
iterator.next().map(|value2| iterator.fold(function(value1, value2), |(k, v), x| function((k, v), x)))
})
}
fn reduce_ref(&self, function: impl FnMut((&Key, &Value), (&Key, &Value)) -> (Key, Value)) -> Option<(Key, Value)>;
fn subset<'a>(&'a self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Key: Eq + Hash + 'a;
#[inline]
fn substitute(self, value: &Key, replacement_key: Key, replacement_value: Value) -> Self
where
Key: PartialEq,
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
let mut replaced = Some((replacement_key, replacement_value));
self
.into_iter()
.map(|(key, val)| if &key == value { replaced.take().unwrap_or((key, val)) } else { (key, val) })
.collect()
}
#[inline]
fn substitute_multi<'a>(
self, keys: &'a impl Iterable<Item<'a> = &'a Key>, replacements: impl IntoIterator<Item = (Key, Value)>,
) -> Self
where
Key: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
let keys_iterator = keys.iterator();
let mut replaced = HashMap::<&Key, LinkedList<(Key, Value)>>::with_capacity(keys_iterator.size_hint().0);
for (item, replacement) in keys_iterator.zip(replacements.into_iter()) {
replaced.entry(item).or_default().push_back(replacement);
}
self
.into_iter()
.map(
|(k, v)| if let Some(entries) = replaced.get_mut(&k) { entries.pop_front().unwrap_or((k, v)) } else { (k, v) },
)
.collect()
}
fn superset<'a>(&'a self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Key: Eq + Hash + 'a;
#[inline]
fn sum_keys(self) -> Key
where
Key: Sum,
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().map(|(k, _)| k).sum()
}
#[inline]
fn sum_values(self) -> Value
where
Value: Sum,
Self: IntoIterator<Item = (Key, Value)> + Sized,
{
self.into_iter().map(|(_, v)| v).sum()
}
fn to_bmap(self) -> BTreeMap<Key, Value>
where
Key: Ord + Clone,
Value: Clone;
fn to_bset(self) -> BTreeSet<(Key, Value)>
where
Key: Ord + Clone,
Value: Ord + Clone;
fn to_deque(self) -> VecDeque<(Key, Value)>
where
Key: Clone,
Value: Clone;
fn to_heap(self) -> BinaryHeap<(Key, Value)>
where
Key: Ord + Clone,
Value: Ord + Clone;
fn to_keys(&self) -> Vec<Key>
where
Key: Clone;
fn to_list(self) -> LinkedList<(Key, Value)>
where
Key: Clone,
Value: Clone;
fn to_map(self) -> HashMap<Key, Value>
where
Key: Eq + Hash + Clone,
Value: Clone;
fn to_set(self) -> HashSet<(Key, Value)>
where
Key: Eq + Hash + Clone,
Value: Eq + Hash + Clone;
fn to_values(&self) -> Vec<Value>
where
Value: Clone;
fn to_vec(self) -> Vec<(Key, Value)>
where
Key: Clone,
Value: Clone;
#[inline]
fn unit(key: Key, value: Value) -> Self
where
Self: FromIterator<(Key, Value)>,
{
iter::once((key, value)).collect()
}
}
pub(crate) fn minmax_by_pairs<'a, K: 'a, V: 'a>(
mut iterator: impl Iterator<Item = (&'a K, &'a V)>, mut compare: impl FnMut((&K, &V), (&K, &V)) -> Ordering,
) -> Option<((&'a K, &'a V), (&'a K, &'a V))> {
iterator.next().map(|item| {
let mut min = item;
let mut max = min;
for item in iterator {
if compare(item, min) == Ordering::Less {
min = item;
}
if compare(item, max) == Ordering::Greater {
max = item;
}
}
(min, max)
})
}
#[inline]
pub(crate) fn minmax_by_key_pairs<'a, K: 'a, V: 'a, E: Ord>(
iterator: impl Iterator<Item = (&'a K, &'a V)>, mut to_key: impl FnMut((&K, &V)) -> E,
) -> Option<((&'a K, &'a V), (&'a K, &'a V))> {
minmax_by_pairs(iterator, |x, y| to_key(x).cmp(&to_key(y)))
}
pub(crate) fn partition_map_pairs<'a, K: 'a, V: 'a, L1, W1, L2, W2, Left, Right>(
iterator: impl Iterator<Item = (&'a K, &'a V)>, mut function: impl FnMut((&K, &V)) -> Result<(L1, W1), (L2, W2)>,
) -> (Left, Right)
where
Left: Default + Extend<(L1, W1)>,
Right: Default + Extend<(L2, W2)>,
{
let mut result_left = Left::default();
let mut result_right = Right::default();
for item in iterator {
match function(item) {
Ok(value) => result_left.extend(iter::once(value)),
Err(value) => result_right.extend(iter::once(value)),
}
}
(result_left, result_right)
}
#[inline]
pub(crate) fn reduce_pairs<'a, K: 'a, V: 'a>(
mut iterator: impl Iterator<Item = (&'a K, &'a V)>, mut function: impl FnMut((&K, &V), (&K, &V)) -> (K, V),
) -> Option<(K, V)> {
iterator.next().and_then(|value1| {
iterator.next().map(|value2| iterator.fold(function(value1, value2), |r, x| function((&r.0, &r.1), x)))
})
}