#[cfg(feature = "standalone_collection_tools")]
pub mod hashmap_compat {
use std::collections::hash_map::RandomState;
use core::hash::Hash;
#[derive(Debug, Clone)]
pub struct HashMap<K, V>(hashbrown::HashMap<K, V, RandomState>);
impl<K, V> HashMap<K, V>
where
K: Hash + Eq,
{
#[must_use]
pub fn new() -> Self {
Self(hashbrown::HashMap::with_hasher(RandomState::new()))
}
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
self.0.insert(k, v)
}
pub fn get<Q>(&self, k: &Q) -> Option<&V>
where
K: core::borrow::Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.get(k)
}
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where
K: core::borrow::Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.get_mut(k)
}
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where
K: core::borrow::Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.remove(k)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
pub fn clear(&mut self) {
self.0.clear();
}
#[must_use]
pub fn iter(&self) -> hashbrown::hash_map::Iter<'_, K, V> {
self.0.iter()
}
pub fn iter_mut(&mut self) -> hashbrown::hash_map::IterMut<'_, K, V> {
self.0.iter_mut()
}
#[must_use]
pub fn keys(&self) -> hashbrown::hash_map::Keys<'_, K, V> {
self.0.keys()
}
#[must_use]
pub fn values(&self) -> hashbrown::hash_map::Values<'_, K, V> {
self.0.values()
}
}
impl<'a, K, V> IntoIterator for &'a HashMap<K, V>
where
K: Hash + Eq,
{
type Item = (&'a K, &'a V);
type IntoIter = hashbrown::hash_map::Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut HashMap<K, V>
where
K: Hash + Eq,
{
type Item = (&'a K, &'a mut V);
type IntoIter = hashbrown::hash_map::IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
#[derive(Debug, Clone)]
pub struct HashSet<T>(hashbrown::HashSet<T, RandomState>);
impl<T> HashSet<T>
where
T: Hash + Eq,
{
#[must_use]
pub fn new() -> Self {
Self(hashbrown::HashSet::with_hasher(RandomState::new()))
}
pub fn insert(&mut self, value: T) -> bool {
self.0.insert(value)
}
pub fn contains<Q>(&self, value: &Q) -> bool
where
T: core::borrow::Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.contains(value)
}
pub fn remove<Q>(&mut self, value: &Q) -> bool
where
T: core::borrow::Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.0.remove(value)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
pub fn clear(&mut self) {
self.0.clear();
}
#[must_use]
pub fn iter(&self) -> hashbrown::hash_set::Iter<'_, T> {
self.0.iter()
}
}
impl<'a, T> IntoIterator for &'a HashSet<T>
where
T: Hash + Eq,
{
type Item = &'a T;
type IntoIter = hashbrown::hash_set::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<K, V> Default for HashMap<K, V>
where
K: Hash + Eq,
{
fn default() -> Self {
Self::new()
}
}
impl<K, V, const N: usize> From<[(K, V); N]> for HashMap<K, V>
where
K: Hash + Eq,
{
fn from(arr: [(K, V); N]) -> Self {
let mut map = Self::new();
for (k, v) in arr {
map.insert(k, v);
}
map
}
}
impl<K, V> core::iter::FromIterator<(K, V)> for HashMap<K, V>
where
K: Hash + Eq,
{
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut map = Self::new();
for (k, v) in iter {
map.insert(k, v);
}
map
}
}
impl<K, V> IntoIterator for HashMap<K, V> {
type Item = (K, V);
type IntoIter = hashbrown::hash_map::IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<K, V> PartialEq for HashMap<K, V>
where
K: Eq + Hash,
V: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<K, V> Eq for HashMap<K, V>
where
K: Eq + Hash,
V: Eq,
{}
impl<T> Default for HashSet<T>
where
T: Hash + Eq,
{
fn default() -> Self {
Self::new()
}
}
impl<T, const N: usize> From<[T; N]> for HashSet<T>
where
T: Hash + Eq,
{
fn from(arr: [T; N]) -> Self {
let mut set = Self::new();
for item in arr {
set.insert(item);
}
set
}
}
impl<T> core::iter::FromIterator<T> for HashSet<T>
where
T: Hash + Eq,
{
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut set = Self::new();
for item in iter {
set.insert(item);
}
set
}
}
impl<T> IntoIterator for HashSet<T> {
type Item = T;
type IntoIter = hashbrown::hash_set::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<T> PartialEq for HashSet<T>
where
T: Eq + Hash,
{
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T> Eq for HashSet<T>
where
T: Eq + Hash,
{}
}
#[cfg(feature = "standalone_collection_tools")]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
pub use hashmap_compat::{HashMap, HashSet};
#[cfg(not(feature = "standalone_collection_tools"))]
#[ doc( inline ) ]
#[ allow( unused_imports ) ]
#[ allow( clippy::pub_use ) ]
pub use std::collections::{HashMap, HashSet};
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
pub use std::vec::Vec;
#[allow(unused_imports)]
pub mod btree_map {
pub use std::collections::BTreeMap;
pub use std::collections::btree_map::{IntoIter, Iter, IterMut, Keys, Values, ValuesMut, Entry, OccupiedEntry, VacantEntry};
}
#[allow(unused_imports)]
pub mod btree_set {
pub use std::collections::BTreeSet;
pub use std::collections::btree_set::{IntoIter, Iter, Difference, Intersection, SymmetricDifference, Union};
}
#[allow(unused_imports)]
pub mod binary_heap {
pub use std::collections::BinaryHeap;
pub use std::collections::binary_heap::{IntoIter, Iter, Drain};
}
#[allow(unused_imports)]
pub mod hash_map {
pub use super::HashMap;
pub use hashbrown::hash_map::{IntoIter, Iter, IterMut, Keys, Values, ValuesMut, Entry, OccupiedEntry, VacantEntry};
}
#[allow(unused_imports)]
pub mod hash_set {
pub use super::HashSet;
pub use hashbrown::hash_set::{IntoIter, Iter, Difference, Intersection, SymmetricDifference, Union};
}
#[allow(unused_imports)]
pub mod linked_list {
pub use std::collections::LinkedList;
pub use std::collections::linked_list::{IntoIter, Iter, IterMut};
}
#[allow(unused_imports)]
pub mod vec_deque {
pub use std::collections::VecDeque;
pub use std::collections::vec_deque::{IntoIter, Iter, IterMut, Drain};
}
#[allow(unused_imports)]
pub mod vector {
pub use std::vec::Vec;
}
pub mod collection {
pub mod exposed {
#[macro_export]
macro_rules! heap {
( $( $x:expr ),* ) => {
{
let mut heap = std::collections::BinaryHeap::new();
$(
heap.push($x);
)*
heap
}
};
}
#[macro_export]
macro_rules! bmap {
( $( $key:expr => $value:expr ),* ) => {
{
let mut map = std::collections::BTreeMap::new();
$(
map.insert($key, $value);
)*
map
}
};
}
#[macro_export]
macro_rules! vector_from {
( $( $x:expr ),* ) => {
{
let mut v = std::vec::Vec::new();
$(
v.push($x);
)*
v
}
};
}
#[macro_export]
macro_rules! hset {
( $( $x:expr ),* ) => {
{
let mut set = $crate::HashSet::new();
$(
set.insert($x);
)*
set
}
};
}
#[macro_export]
macro_rules! bset {
( $( $x:expr ),* ) => {
{
let mut set = std::collections::BTreeSet::new();
$(
set.insert($x);
)*
set
}
};
}
#[macro_export]
macro_rules! hmap {
( $( $key:expr => $value:expr ),* ) => {
{
let mut map = $crate::HashMap::new();
$(
map.insert($key, $value);
)*
map
}
};
}
#[macro_export]
macro_rules! into_hmap {
( $( $key:expr => $value:expr ),* ) => {
{
let mut map = $crate::HashMap::new();
$(
map.insert($key, $value);
)*
map
}
};
}
#[macro_export]
macro_rules! llist {
( $( $x:expr ),* ) => {
{
let mut list = std::collections::LinkedList::new();
$(
list.push_back($x);
)*
list
}
};
}
#[macro_export]
macro_rules! deque {
( $( $x:expr ),* ) => {
{
let mut deque = std::collections::VecDeque::new();
$(
deque.push_back($x);
)*
deque
}
};
}
#[macro_export]
macro_rules! into_heap {
( $( $x:expr ),* ) => {
{
let mut heap = std::collections::BinaryHeap::new();
$(
heap.push($x);
)*
heap
}
};
}
#[macro_export]
macro_rules! into_vecd {
( $( $x:expr ),* ) => {
{
let mut deque = std::collections::VecDeque::new();
$(
deque.push_back($x);
)*
deque
}
};
}
#[macro_export]
macro_rules! into_llist {
( $( $x:expr ),* ) => {
{
let mut list = std::collections::LinkedList::new();
$(
list.push_back($x);
)*
list
}
};
}
#[macro_export]
macro_rules! dlist {
( $( $x:expr ),* ) => {
{
let mut deque = std::collections::VecDeque::new();
$(
deque.push_back($x);
)*
deque
}
};
}
#[macro_export]
macro_rules! into_hset {
( $( $x:expr ),* ) => {
{
let mut set = $crate::HashSet::new();
$(
set.insert($x);
)*
set
}
};
}
#[macro_export]
macro_rules! into_dlist {
( $( $x:expr ),* ) => {
{
let mut vec = std::vec::Vec::new();
$(
vec.push($x);
)*
vec
}
};
}
#[allow(unused_imports)]
pub use crate::{heap, bmap, vector_from, hset, bset, hmap, llist, deque, dlist, into_heap, into_vecd, into_llist, into_dlist, into_hset, into_hmap};
}
}
pub use crate::{heap, bmap, hset, vector_from, bset, hmap, llist, deque, dlist, into_heap, into_vecd, into_llist, into_dlist, into_hset, into_hmap};