#![allow(unsafe_code)]
use alloc::vec;
use core::{
borrow::Borrow,
fmt::{self, Debug, Formatter},
hash::{BuildHasher, Hash, Hasher},
iter::{FromIterator, FusedIterator},
marker::PhantomData,
};
use dlv_list::{
Index, IntoIter as VecListIntoIter, Iter as VecListIter, IterMut as VecListIterMut, VecList,
};
use hashbrown::{
hash_map::{RawEntryMut, RawOccupiedEntryMut},
HashMap,
};
#[cfg(feature = "std")]
pub type RandomState = std::collections::hash_map::RandomState;
#[cfg(not(feature = "std"))]
#[derive(Debug)]
pub struct RandomState(core::convert::Infallible);
#[cfg(not(feature = "std"))]
impl RandomState {
#[cfg_attr(mutants, mutants::skip)]
#[must_use]
pub fn new() -> RandomState {
panic!("RandomState is not available without std")
}
}
#[cfg(not(feature = "std"))]
impl Default for RandomState {
#[cfg_attr(mutants, mutants::skip)]
fn default() -> RandomState {
RandomState::new()
}
}
#[cfg(not(feature = "std"))]
impl BuildHasher for RandomState {
type Hasher = DummyHasher;
#[cfg_attr(mutants, mutants::skip)]
fn build_hasher(&self) -> Self::Hasher {
match self.0 {}
}
}
#[derive(Clone)]
pub struct ListOrderedMultimap<Key, Value, State = RandomState> {
pub(crate) build_hasher: State,
pub(crate) keys: VecList<Key>,
pub(crate) map: HashMap<Index<Key>, MapEntry<Key, Value>, DummyState>,
pub(crate) values: VecList<ValueEntry<Key, Value>>,
}
#[cfg(feature = "std")]
impl<Key, Value> ListOrderedMultimap<Key, Value, RandomState> {
#[must_use]
pub fn new() -> ListOrderedMultimap<Key, Value, RandomState> {
ListOrderedMultimap {
build_hasher: RandomState::new(),
keys: VecList::new(),
map: HashMap::with_hasher(DummyState),
values: VecList::new(),
}
}
#[must_use]
pub fn with_capacity(
key_capacity: usize,
value_capacity: usize,
) -> ListOrderedMultimap<Key, Value, RandomState> {
ListOrderedMultimap {
build_hasher: RandomState::new(),
keys: VecList::with_capacity(key_capacity),
map: HashMap::with_capacity_and_hasher(key_capacity, DummyState),
values: VecList::with_capacity(value_capacity),
}
}
}
impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>
where
State: BuildHasher,
{
#[must_use]
pub fn with_capacity_and_hasher(
key_capacity: usize,
value_capacity: usize,
state: State,
) -> ListOrderedMultimap<Key, Value, State> {
ListOrderedMultimap {
build_hasher: state,
keys: VecList::with_capacity(key_capacity),
map: HashMap::with_capacity_and_hasher(key_capacity, DummyState),
values: VecList::with_capacity(value_capacity),
}
}
#[must_use]
pub fn with_hasher(state: State) -> ListOrderedMultimap<Key, Value, State> {
ListOrderedMultimap {
build_hasher: state,
keys: VecList::new(),
map: HashMap::with_hasher(DummyState),
values: VecList::new(),
}
}
}
impl<Key, Value, State> ListOrderedMultimap<Key, Value, State> {
#[must_use]
pub fn back(&self) -> Option<(&Key, &Value)> {
self.iter().next_back()
}
#[must_use]
pub fn back_mut(&mut self) -> Option<(&Key, &mut Value)> {
self.iter_mut().next_back()
}
pub fn clear(&mut self) {
self.keys.clear();
self.map.clear();
self.values.clear();
}
#[must_use]
pub fn front(&self) -> Option<(&Key, &Value)> {
self.iter().next()
}
#[must_use]
pub fn front_mut(&mut self) -> Option<(&Key, &mut Value)> {
self.iter_mut().next()
}
#[must_use]
pub fn hasher(&self) -> &State {
&self.build_hasher
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.keys.is_empty()
}
#[must_use]
pub fn iter(&self) -> Iter<'_, Key, Value> {
Iter {
keys: &self.keys,
iter: self.values.iter(),
}
}
#[must_use]
pub fn iter_mut(&mut self) -> IterMut<'_, Key, Value> {
IterMut {
keys: &self.keys,
iter: self.values.iter_mut(),
}
}
#[must_use]
pub fn keys(&self) -> Keys<'_, Key> {
Keys(self.keys.iter())
}
#[must_use]
pub fn keys_capacity(&self) -> usize {
self.keys.capacity()
}
#[must_use]
pub fn keys_len(&self) -> usize {
self.keys.len()
}
#[must_use]
pub fn pairs(&self) -> KeyValues<'_, Key, Value, State> {
KeyValues {
build_hasher: &self.build_hasher,
keys: &self.keys,
iter: self.keys.iter(),
map: &self.map,
values: &self.values,
}
}
#[must_use]
pub fn pairs_mut(&mut self) -> KeyValuesMut<'_, Key, Value, State> {
KeyValuesMut {
build_hasher: &self.build_hasher,
keys: &self.keys,
iter: self.keys.iter(),
map: &self.map,
values: &mut self.values,
}
}
pub fn reserve_values(&mut self, additional_capacity: usize) {
self.values.reserve(additional_capacity);
}
#[must_use]
pub fn values(&self) -> Values<'_, Key, Value> {
Values(self.values.iter())
}
#[must_use]
pub fn values_mut(&mut self) -> ValuesMut<'_, Key, Value> {
ValuesMut(self.values.iter_mut())
}
#[must_use]
pub fn values_capacity(&self) -> usize {
self.values.capacity()
}
#[must_use]
pub fn values_len(&self) -> usize {
self.values.len()
}
}
impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
pub fn append(&mut self, key: Key, value: Value) -> bool {
let hash = hash_key(&self.build_hasher, &key);
let entry = raw_entry_mut(&self.keys, &mut self.map, hash, &key);
let build_hasher = &self.build_hasher;
match entry {
RawEntryMut::Occupied(mut entry) => {
let key_index = entry.key();
let mut value_entry = ValueEntry::new(*key_index, value);
let map_entry = entry.get_mut();
value_entry.previous_index = Some(map_entry.tail_index);
let index = self.values.push_back(value_entry);
self
.values
.get_mut(map_entry.tail_index)
.unwrap()
.next_index = Some(index);
map_entry.append(index);
true
}
RawEntryMut::Vacant(entry) => {
let key_index = self.keys.push_back(key);
let value_entry = ValueEntry::new(key_index, value);
let index = self.values.push_back(value_entry);
let keys = &self.keys;
let _ = entry.insert_with_hasher(hash, key_index, MapEntry::new(index), |&key_index| {
let key = keys.get(key_index).unwrap();
hash_key(build_hasher, key)
});
false
}
}
}
#[must_use]
pub fn contains_key<KeyQuery>(&self, key: &KeyQuery) -> bool
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
raw_entry(&self.keys, &self.map, hash, key).is_some()
}
#[must_use]
pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value, State> {
let hash = hash_key(&self.build_hasher, &key);
if !self.contains_key(&key) {
Entry::Vacant(VacantEntry {
build_hasher: &self.build_hasher,
hash,
key,
keys: &mut self.keys,
map: &mut self.map,
values: &mut self.values,
})
} else {
match raw_entry_mut(&self.keys, &mut self.map, hash, &key) {
RawEntryMut::Occupied(entry) => Entry::Occupied(OccupiedEntry {
entry,
keys: &mut self.keys,
values: &mut self.values,
}),
_ => panic!("expected occupied entry"),
}
}
}
#[must_use]
pub fn entry_len<KeyQuery>(&self, key: &KeyQuery) -> usize
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
match raw_entry(&self.keys, &self.map, hash, key) {
Some((_, map_entry)) => map_entry.length,
None => 0,
}
}
#[must_use]
pub fn get<KeyQuery>(&self, key: &KeyQuery) -> Option<&Value>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
let (_, map_entry) = raw_entry(&self.keys, &self.map, hash, key)?;
self
.values
.get(map_entry.head_index)
.map(|entry| &entry.value)
}
#[must_use]
pub fn get_all<KeyQuery>(&self, key: &KeyQuery) -> EntryValues<'_, Key, Value>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
match raw_entry(&self.keys, &self.map, hash, key) {
Some((_, map_entry)) => EntryValues::from_map_entry(&self.values, map_entry),
None => EntryValues::empty(&self.values),
}
}
#[must_use]
pub fn get_all_mut<KeyQuery>(&mut self, key: &KeyQuery) -> EntryValuesMut<'_, Key, Value>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
match raw_entry(&self.keys, &self.map, hash, key) {
Some((_, map_entry)) => EntryValuesMut::from_map_entry(&mut self.values, map_entry),
None => EntryValuesMut::empty(&mut self.values),
}
}
#[must_use]
pub fn get_mut<KeyQuery>(&mut self, key: &KeyQuery) -> Option<&mut Value>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
let (_, map_entry) = raw_entry(&self.keys, &self.map, hash, key)?;
self
.values
.get_mut(map_entry.head_index)
.map(|entry| &mut entry.value)
}
pub fn insert(&mut self, key: Key, value: Value) -> Option<Value> {
self.insert_all(key, value).next()
}
pub fn insert_all(&mut self, key: Key, value: Value) -> EntryValuesDrain<'_, Key, Value> {
let hash = hash_key(&self.build_hasher, &key);
let entry = raw_entry_mut(&self.keys, &mut self.map, hash, &key);
let build_hasher = &self.build_hasher;
match entry {
RawEntryMut::Occupied(mut entry) => {
let key_index = entry.key();
let value_entry = ValueEntry::new(*key_index, value);
let index = self.values.push_back(value_entry);
let map_entry = entry.get_mut();
let iter = EntryValuesDrain::from_map_entry(&mut self.values, map_entry);
map_entry.reset(index);
iter
}
RawEntryMut::Vacant(entry) => {
let key_index = self.keys.push_back(key);
let value_entry = ValueEntry::new(key_index, value);
let index = self.values.push_back(value_entry);
let keys = &self.keys;
let _ = entry.insert_with_hasher(hash, key_index, MapEntry::new(index), |&key_index| {
let key = keys.get(key_index).unwrap();
hash_key(build_hasher, key)
});
EntryValuesDrain::empty(&mut self.values)
}
}
}
#[cfg(feature = "std")]
pub fn pack_to(&mut self, keys_minimum_capacity: usize, values_minimum_capacity: usize)
where
State: Default,
{
assert!(
keys_minimum_capacity >= self.keys_len(),
"cannot pack multimap keys lower than current length"
);
assert!(
values_minimum_capacity >= self.values_len(),
"cannot pack multimap values lower than current length"
);
let key_map = self.keys.pack_to(keys_minimum_capacity);
let value_map = self.values.pack_to(values_minimum_capacity);
let mut map = HashMap::with_capacity_and_hasher(keys_minimum_capacity, DummyState);
let build_hasher = &self.build_hasher;
for value_entry in self.values.iter_mut() {
value_entry.key_index = key_map[&value_entry.key_index];
value_entry.next_index = value_entry.next_index.map(|index| value_map[&index]);
value_entry.previous_index = value_entry.previous_index.map(|index| value_map[&index]);
}
for (key_index, mut map_entry) in self.map.drain() {
map_entry.head_index = value_map[&map_entry.head_index];
map_entry.tail_index = value_map[&map_entry.tail_index];
let key_index = key_map[&key_index];
let key = self.keys.get(key_index).unwrap();
let hash = hash_key(&self.build_hasher, key);
match map.raw_entry_mut().from_hash(hash, |_| false) {
RawEntryMut::Vacant(entry) => {
let keys = &self.keys;
let _ = entry.insert_with_hasher(hash, key_index, map_entry, |&key_index| {
let key = keys.get(key_index).unwrap();
hash_key(build_hasher, key)
});
}
_ => panic!("expected vacant entry"),
}
}
self.map = map;
}
#[cfg(feature = "std")]
pub fn pack_to_fit(&mut self)
where
State: Default,
{
self.pack_to(self.keys_len(), self.values_len());
}
pub fn pop_back(&mut self) -> Option<(KeyWrapper<'_, Key>, Value)> {
let value_entry = self.values.pop_back()?;
let key_wrapper = match value_entry.previous_index {
Some(previous_index) => {
let key = self.keys.get(value_entry.key_index).unwrap();
let hash = hash_key(&self.build_hasher, &key);
let mut entry = match raw_entry_mut(&self.keys, &mut self.map, hash, key) {
RawEntryMut::Occupied(entry) => entry,
_ => panic!("expected occupied entry in internal map"),
};
let map_entry = entry.get_mut();
map_entry.length -= 1;
map_entry.tail_index = previous_index;
let previous_value_entry = self.values.get_mut(previous_index).unwrap();
previous_value_entry.next_index = None;
KeyWrapper::Borrowed(key)
}
None => {
let key = self.keys.remove(value_entry.key_index).unwrap();
let hash = hash_key(&self.build_hasher, &key);
match raw_entry_mut_empty(&self.keys, &mut self.map, hash) {
RawEntryMut::Occupied(entry) => {
let _ = entry.remove();
}
_ => panic!("expectd occupied entry in internal map"),
}
KeyWrapper::Owned(key)
}
};
Some((key_wrapper, value_entry.value))
}
pub fn pop_front(&mut self) -> Option<(KeyWrapper<'_, Key>, Value)> {
let value_entry = self.values.pop_front()?;
let key_wrapper = match value_entry.next_index {
Some(next_index) => {
let key = self.keys.get(value_entry.key_index).unwrap();
let hash = hash_key(&self.build_hasher, &key);
let mut entry = match raw_entry_mut(&self.keys, &mut self.map, hash, key) {
RawEntryMut::Occupied(entry) => entry,
_ => panic!("expected occupied entry in internal map"),
};
let map_entry = entry.get_mut();
map_entry.length -= 1;
map_entry.head_index = next_index;
let next_value_entry = self.values.get_mut(next_index).unwrap();
next_value_entry.previous_index = None;
KeyWrapper::Borrowed(key)
}
None => {
let key = self.keys.remove(value_entry.key_index).unwrap();
let hash = hash_key(&self.build_hasher, &key);
match raw_entry_mut_empty(&self.keys, &mut self.map, hash) {
RawEntryMut::Occupied(entry) => {
let _ = entry.remove();
}
_ => panic!("expectd occupied entry in internal map"),
}
KeyWrapper::Owned(key)
}
};
Some((key_wrapper, value_entry.value))
}
pub fn remove<KeyQuery>(&mut self, key: &KeyQuery) -> Option<Value>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
self.remove_entry(key).map(|(_, value)| value)
}
pub fn remove_all<KeyQuery>(&mut self, key: &KeyQuery) -> EntryValuesDrain<'_, Key, Value>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
let entry = raw_entry_mut(&self.keys, &mut self.map, hash, key);
match entry {
RawEntryMut::Occupied(entry) => {
let (key_index, map_entry) = entry.remove_entry();
let _ = self.keys.remove(key_index).unwrap();
EntryValuesDrain::from_map_entry(&mut self.values, &map_entry)
}
RawEntryMut::Vacant(_) => EntryValuesDrain::empty(&mut self.values),
}
}
pub fn remove_entry<KeyQuery>(&mut self, key: &KeyQuery) -> Option<(Key, Value)>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let (key, mut iter) = self.remove_entry_all(key)?;
Some((key, iter.next().unwrap()))
}
pub fn remove_entry_all<KeyQuery>(
&mut self,
key: &KeyQuery,
) -> Option<(Key, EntryValuesDrain<'_, Key, Value>)>
where
Key: Borrow<KeyQuery>,
KeyQuery: ?Sized + Eq + Hash,
{
let hash = hash_key(&self.build_hasher, &key);
let entry = raw_entry_mut(&self.keys, &mut self.map, hash, key);
match entry {
RawEntryMut::Occupied(entry) => {
let (key_index, map_entry) = entry.remove_entry();
let key = self.keys.remove(key_index).unwrap();
let iter = EntryValuesDrain::from_map_entry(&mut self.values, &map_entry);
Some((key, iter))
}
_ => None,
}
}
pub fn reserve_keys(&mut self, additional_capacity: usize) {
if self.keys.capacity() - self.keys.len() >= additional_capacity {
return;
}
let capacity = self.map.capacity() + additional_capacity;
let mut map = HashMap::with_capacity_and_hasher(capacity, DummyState);
for (key_index, map_entry) in self.map.drain() {
let key = self.keys.get(key_index).unwrap();
let hash = hash_key(&self.build_hasher, key);
let entry = match raw_entry_mut(&self.keys, &mut map, hash, key) {
RawEntryMut::Vacant(entry) => entry,
_ => panic!("expected vacant entry"),
};
let _ = entry.insert_hashed_nocheck(hash, key_index, map_entry);
}
self.keys.reserve(additional_capacity);
self.map = map;
}
pub fn retain<Function>(&mut self, function: Function)
where
Function: FnMut(&Key, &mut Value) -> bool,
{
ListOrderedMultimap::retain_helper(
&self.build_hasher,
&mut self.keys,
&mut self.map,
&mut self.values,
function,
);
}
fn retain_helper<'map, Function>(
build_hasher: &'map State,
keys: &'map mut VecList<Key>,
map: &'map mut HashMap<Index<Key>, MapEntry<Key, Value>, DummyState>,
values: &'map mut VecList<ValueEntry<Key, Value>>,
mut function: Function,
) where
Function: FnMut(&Key, &mut Value) -> bool,
{
let mut post_updates = vec![];
values.retain(|value_entry| {
let key = keys.get(value_entry.key_index).unwrap();
if function(key, &mut value_entry.value) {
true
} else {
let hash = hash_key(build_hasher, key);
let mut entry = match raw_entry_mut(keys, map, hash, key) {
RawEntryMut::Occupied(entry) => entry,
_ => panic!("expected occupied entry in internal map"),
};
if value_entry.previous_index.is_none() && value_entry.next_index.is_none() {
let _ = entry.remove();
let _ = keys.remove(value_entry.key_index);
} else {
let map_entry = entry.get_mut();
map_entry.length -= 1;
if let Some(previous_index) = value_entry.previous_index {
post_updates.push((previous_index, None, Some(value_entry.next_index)));
} else {
map_entry.head_index = value_entry.next_index.unwrap();
}
if let Some(next_index) = value_entry.next_index {
post_updates.push((next_index, Some(value_entry.previous_index), None));
} else {
map_entry.tail_index = value_entry.previous_index.unwrap();
}
}
false
}
});
for (index, new_previous_index, new_next_index) in post_updates {
let value_entry = values.get_mut(index).unwrap();
if let Some(new_previous_index) = new_previous_index {
value_entry.previous_index = new_previous_index;
}
if let Some(new_next_index) = new_next_index {
value_entry.next_index = new_next_index;
}
}
}
}
impl<Key, Value, State> Debug for ListOrderedMultimap<Key, Value, State>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.debug_map().entries(self.iter()).finish()
}
}
#[cfg(feature = "std")]
impl<Key, Value> Default for ListOrderedMultimap<Key, Value, RandomState> {
fn default() -> Self {
Self::new()
}
}
impl<Key, Value, State> Eq for ListOrderedMultimap<Key, Value, State>
where
Key: Eq,
Value: PartialEq,
{
}
impl<Key, Value, State> Extend<(Key, Value)> for ListOrderedMultimap<Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
fn extend<Iter>(&mut self, iter: Iter)
where
Iter: IntoIterator<Item = (Key, Value)>,
{
let iter = iter.into_iter();
self.reserve_values(iter.size_hint().0);
for (key, value) in iter {
let _ = self.append(key, value);
}
}
}
impl<'a, Key, Value, State> Extend<(&'a Key, &'a Value)> for ListOrderedMultimap<Key, Value, State>
where
Key: Copy + Eq + Hash,
Value: Copy,
State: BuildHasher,
{
fn extend<Iter>(&mut self, iter: Iter)
where
Iter: IntoIterator<Item = (&'a Key, &'a Value)>,
{
self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
}
}
impl<Key, Value, State> FromIterator<(Key, Value)> for ListOrderedMultimap<Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher + Default,
{
fn from_iter<Iter>(iter: Iter) -> Self
where
Iter: IntoIterator<Item = (Key, Value)>,
{
let mut map = ListOrderedMultimap::with_hasher(State::default());
map.extend(iter);
map
}
}
impl<Key, Value, State> IntoIterator for ListOrderedMultimap<Key, Value, State>
where
Key: Clone,
{
type IntoIter = IntoIter<Key, Value>;
type Item = (Key, Value);
fn into_iter(self) -> Self::IntoIter {
IntoIter {
keys: self.keys,
iter: self.values.into_iter(),
}
}
}
impl<'map, Key, Value, State> IntoIterator for &'map ListOrderedMultimap<Key, Value, State> {
type IntoIter = Iter<'map, Key, Value>;
type Item = (&'map Key, &'map Value);
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'map, Key, Value, State> IntoIterator for &'map mut ListOrderedMultimap<Key, Value, State> {
type IntoIter = IterMut<'map, Key, Value>;
type Item = (&'map Key, &'map mut Value);
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<Key, Value, State> PartialEq for ListOrderedMultimap<Key, Value, State>
where
Key: PartialEq,
Value: PartialEq,
{
fn eq(&self, other: &ListOrderedMultimap<Key, Value, State>) -> bool {
if self.keys_len() != other.keys_len() || self.values_len() != other.values_len() {
return false;
}
self.iter().eq(other.iter())
}
}
#[allow(single_use_lifetimes)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KeyWrapper<'map, Key> {
Borrowed(&'map Key),
Owned(Key),
}
impl<Key> KeyWrapper<'_, Key> {
#[must_use]
pub fn into_owned(self) -> Key
where
Key: Clone,
{
match self {
KeyWrapper::Borrowed(key) => key.clone(),
KeyWrapper::Owned(key) => key,
}
}
#[must_use]
pub fn is_borrowed(&self) -> bool {
matches!(self, KeyWrapper::Borrowed(_))
}
#[must_use]
pub fn is_owned(&self) -> bool {
matches!(self, KeyWrapper::Owned(_))
}
}
#[derive(Clone)]
pub(crate) struct MapEntry<Key, Value> {
head_index: Index<ValueEntry<Key, Value>>,
length: usize,
tail_index: Index<ValueEntry<Key, Value>>,
}
impl<Key, Value> MapEntry<Key, Value> {
pub fn append(&mut self, index: Index<ValueEntry<Key, Value>>) {
self.length += 1;
self.tail_index = index;
}
#[must_use]
pub fn new(index: Index<ValueEntry<Key, Value>>) -> Self {
MapEntry {
head_index: index,
length: 1,
tail_index: index,
}
}
pub fn reset(&mut self, index: Index<ValueEntry<Key, Value>>) {
self.head_index = index;
self.length = 1;
self.tail_index = index;
}
}
#[derive(Clone)]
pub(crate) struct ValueEntry<Key, Value> {
key_index: Index<Key>,
next_index: Option<Index<ValueEntry<Key, Value>>>,
previous_index: Option<Index<ValueEntry<Key, Value>>>,
value: Value,
}
impl<Key, Value> ValueEntry<Key, Value> {
#[must_use]
pub fn new(key_index: Index<Key>, value: Value) -> Self {
ValueEntry {
key_index,
next_index: None,
previous_index: None,
value,
}
}
}
pub enum Entry<'map, Key, Value, State = RandomState> {
Occupied(OccupiedEntry<'map, Key, Value>),
Vacant(VacantEntry<'map, Key, Value, State>),
}
impl<'map, Key, Value, State> Entry<'map, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
pub fn and_modify<Function>(self, function: Function) -> Self
where
Function: FnOnce(&mut Value),
{
match self {
Entry::Occupied(mut entry) => {
function(entry.get_mut());
Entry::Occupied(entry)
}
Entry::Vacant(entry) => Entry::Vacant(entry),
}
}
pub fn or_insert(self, value: Value) -> &'map mut Value {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(value),
}
}
pub fn or_insert_entry(self, value: Value) -> OccupiedEntry<'map, Key, Value> {
match self {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => entry.insert_entry(value),
}
}
pub fn or_insert_with<Function>(self, function: Function) -> &'map mut Value
where
Function: FnOnce() -> Value,
{
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(function()),
}
}
pub fn or_insert_with_entry<Function>(self, function: Function) -> OccupiedEntry<'map, Key, Value>
where
Function: FnOnce() -> Value,
{
match self {
Entry::Occupied(entry) => entry,
Entry::Vacant(entry) => entry.insert_entry(function()),
}
}
}
impl<Key, Value, State> Debug for Entry<'_, Key, Value, State>
where
Key: Debug,
State: BuildHasher,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Entry::Occupied(entry) => entry.fmt(formatter),
Entry::Vacant(entry) => entry.fmt(formatter),
}
}
}
pub struct OccupiedEntry<'map, Key, Value> {
entry: RawOccupiedEntryMut<'map, Index<Key>, MapEntry<Key, Value>, DummyState>,
keys: &'map mut VecList<Key>,
values: &'map mut VecList<ValueEntry<Key, Value>>,
}
#[allow(clippy::len_without_is_empty)]
impl<'map, Key, Value> OccupiedEntry<'map, Key, Value> {
pub fn append(&mut self, value: Value) {
let key_index = *self.entry.key();
let map_entry = self.entry.get_mut();
let mut value_entry = ValueEntry::new(key_index, value);
value_entry.previous_index = Some(map_entry.tail_index);
let index = self.values.push_back(value_entry);
self
.values
.get_mut(map_entry.tail_index)
.unwrap()
.next_index = Some(index);
map_entry.length += 1;
map_entry.tail_index = index;
}
#[must_use]
pub fn get(&self) -> &Value {
let index = self.entry.get().head_index;
&self.values.get(index).unwrap().value
}
#[must_use]
pub fn get_mut(&mut self) -> &mut Value {
let index = self.entry.get().head_index;
&mut self.values.get_mut(index).unwrap().value
}
pub fn insert(&mut self, value: Value) -> Value {
let key_index = *self.entry.key();
let map_entry = self.entry.get_mut();
let first_index = map_entry.head_index;
let mut entry = self.values.remove(first_index).unwrap();
let first_value = entry.value;
while let Some(next_index) = entry.next_index {
entry = self.values.remove(next_index).unwrap();
}
let value_entry = ValueEntry::new(key_index, value);
let index = self.values.push_back(value_entry);
map_entry.head_index = index;
map_entry.length = 1;
map_entry.tail_index = index;
first_value
}
pub fn insert_all(&mut self, value: Value) -> EntryValuesDrain<'_, Key, Value> {
let key_index = *self.entry.key();
let map_entry = self.entry.get_mut();
let value_entry = ValueEntry::new(key_index, value);
let index = self.values.push_back(value_entry);
let iter = EntryValuesDrain::from_map_entry(self.values, map_entry);
map_entry.reset(index);
iter
}
#[must_use]
pub fn into_mut(mut self) -> &'map mut Value {
let index = self.entry.get_mut().head_index;
&mut self.values.get_mut(index).unwrap().value
}
#[must_use]
pub fn iter(&self) -> EntryValues<'_, Key, Value> {
let map_entry = self.entry.get();
EntryValues::from_map_entry(self.values, map_entry)
}
#[must_use]
pub fn iter_mut(&mut self) -> EntryValuesMut<'_, Key, Value> {
let map_entry = self.entry.get_mut();
EntryValuesMut::from_map_entry(self.values, map_entry)
}
#[must_use]
pub fn key(&self) -> &Key {
let key_index = self.entry.key();
self.keys.get(*key_index).unwrap()
}
#[must_use]
pub fn len(&self) -> usize {
self.entry.get().length
}
pub fn remove(self) -> Value {
self.remove_entry().1
}
pub fn remove_all(self) -> EntryValuesDrain<'map, Key, Value> {
self.remove_entry_all().1
}
pub fn remove_entry(self) -> (Key, Value) {
let (key_index, map_entry) = self.entry.remove_entry();
let key = self.keys.remove(key_index).unwrap();
let first_index = map_entry.head_index;
let mut entry = self.values.remove(first_index).unwrap();
let first_value = entry.value;
while let Some(next_index) = entry.next_index {
entry = self.values.remove(next_index).unwrap();
}
(key, first_value)
}
pub fn remove_entry_all(self) -> (Key, EntryValuesDrain<'map, Key, Value>) {
let (key_index, map_entry) = self.entry.remove_entry();
let key = self.keys.remove(key_index).unwrap();
let iter = EntryValuesDrain {
head_index: Some(map_entry.head_index),
remaining: map_entry.length,
tail_index: Some(map_entry.tail_index),
values: self.values,
};
(key, iter)
}
}
impl<Key, Value> Debug for OccupiedEntry<'_, Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("OccupiedEntry")
.field("key", self.key())
.field("values", &self.iter())
.finish()
}
}
pub struct VacantEntry<'map, Key, Value, State = RandomState> {
build_hasher: &'map State,
hash: u64,
key: Key,
keys: &'map mut VecList<Key>,
map: &'map mut HashMap<Index<Key>, MapEntry<Key, Value>, DummyState>,
values: &'map mut VecList<ValueEntry<Key, Value>>,
}
impl<'map, Key, Value, State> VacantEntry<'map, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
pub fn insert(self, value: Value) -> &'map mut Value {
let build_hasher = self.build_hasher;
let entry = match raw_entry_mut(self.keys, self.map, self.hash, &self.key) {
RawEntryMut::Vacant(entry) => entry,
_ => panic!("expected vacant entry"),
};
let key_index = self.keys.push_back(self.key);
let value_entry = ValueEntry::new(key_index, value);
let index = self.values.push_back(value_entry);
let map_entry = MapEntry::new(index);
let keys = &self.keys;
let _ = entry.insert_with_hasher(self.hash, key_index, map_entry, |&key_index| {
let key = keys.get(key_index).unwrap();
hash_key(build_hasher, key)
});
&mut self.values.get_mut(index).unwrap().value
}
pub fn insert_entry(self, value: Value) -> OccupiedEntry<'map, Key, Value> {
let build_hasher = self.build_hasher;
let entry = match raw_entry_mut(self.keys, self.map, self.hash, &self.key) {
RawEntryMut::Vacant(entry) => entry,
_ => panic!("expected vacant entry"),
};
let key_index = self.keys.push_back(self.key);
let value_entry = ValueEntry::new(key_index, value);
let index = self.values.push_back(value_entry);
let map_entry = MapEntry::new(index);
let keys = &self.keys;
let _ = entry.insert_with_hasher(self.hash, key_index, map_entry, |&key_index| {
let key = keys.get(key_index).unwrap();
hash_key(build_hasher, key)
});
let key = self.keys.get(key_index).unwrap();
let entry = match raw_entry_mut(self.keys, self.map, self.hash, key) {
RawEntryMut::Occupied(entry) => entry,
_ => panic!("expected occupied entry"),
};
OccupiedEntry {
entry,
keys: self.keys,
values: self.values,
}
}
#[must_use]
pub fn into_key(self) -> Key {
self.key
}
#[must_use]
pub fn key(&self) -> &Key {
&self.key
}
}
impl<Key, Value, State> Debug for VacantEntry<'_, Key, Value, State>
where
Key: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter
.debug_tuple("VacantEntry")
.field(&self.key)
.finish()
}
}
pub struct EntryValues<'map, Key, Value> {
head_index: Option<Index<ValueEntry<Key, Value>>>,
remaining: usize,
tail_index: Option<Index<ValueEntry<Key, Value>>>,
values: &'map VecList<ValueEntry<Key, Value>>,
}
impl<'map, Key, Value> EntryValues<'map, Key, Value> {
#[must_use]
fn empty(values: &'map VecList<ValueEntry<Key, Value>>) -> Self {
EntryValues {
head_index: None,
remaining: 0,
tail_index: None,
values,
}
}
#[must_use]
fn from_map_entry(
values: &'map VecList<ValueEntry<Key, Value>>,
map_entry: &MapEntry<Key, Value>,
) -> Self {
EntryValues {
head_index: Some(map_entry.head_index),
remaining: map_entry.length,
tail_index: Some(map_entry.tail_index),
values,
}
}
}
impl<'map, Key, Value> Clone for EntryValues<'map, Key, Value> {
fn clone(&self) -> EntryValues<'map, Key, Value> {
EntryValues {
head_index: self.head_index,
remaining: self.remaining,
tail_index: self.tail_index,
values: self.values,
}
}
}
impl<Key, Value> Debug for EntryValues<'_, Key, Value>
where
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("EntryValues(")?;
formatter.debug_list().entries(self.clone()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for EntryValues<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
None
} else {
self.tail_index.map(|index| {
let entry = self.values.get(index).unwrap();
self.tail_index = entry.previous_index;
self.remaining -= 1;
&entry.value
})
}
}
}
impl<Key, Value> ExactSizeIterator for EntryValues<'_, Key, Value> {}
impl<Key, Value> FusedIterator for EntryValues<'_, Key, Value> {}
impl<'map, Key, Value> Iterator for EntryValues<'map, Key, Value> {
type Item = &'map Value;
fn next(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
None
} else {
self.head_index.map(|index| {
let entry = self.values.get(index).unwrap();
self.head_index = entry.next_index;
self.remaining -= 1;
&entry.value
})
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
pub struct EntryValuesDrain<'map, Key, Value> {
head_index: Option<Index<ValueEntry<Key, Value>>>,
remaining: usize,
tail_index: Option<Index<ValueEntry<Key, Value>>>,
values: &'map mut VecList<ValueEntry<Key, Value>>,
}
impl<'map, Key, Value> EntryValuesDrain<'map, Key, Value> {
fn empty(values: &'map mut VecList<ValueEntry<Key, Value>>) -> Self {
EntryValuesDrain {
head_index: None,
remaining: 0,
tail_index: None,
values,
}
}
fn from_map_entry(
values: &'map mut VecList<ValueEntry<Key, Value>>,
map_entry: &MapEntry<Key, Value>,
) -> Self {
EntryValuesDrain {
head_index: Some(map_entry.head_index),
remaining: map_entry.length,
tail_index: Some(map_entry.tail_index),
values,
}
}
#[must_use]
pub fn iter(&self) -> EntryValues<'_, Key, Value> {
EntryValues {
head_index: self.head_index,
remaining: self.remaining,
tail_index: self.tail_index,
values: self.values,
}
}
}
impl<Key, Value> Debug for EntryValuesDrain<'_, Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("EntryValuesDrain(")?;
formatter.debug_list().entries(self.iter()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for EntryValuesDrain<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
None
} else {
self.tail_index.map(|index| {
let entry = self.values.remove(index).unwrap();
self.tail_index = entry.previous_index;
self.remaining -= 1;
entry.value
})
}
}
}
impl<Key, Value> Drop for EntryValuesDrain<'_, Key, Value> {
fn drop(&mut self) {
for _ in self {}
}
}
impl<Key, Value> ExactSizeIterator for EntryValuesDrain<'_, Key, Value> {}
impl<Key, Value> FusedIterator for EntryValuesDrain<'_, Key, Value> {}
impl<Key, Value> Iterator for EntryValuesDrain<'_, Key, Value> {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
None
} else {
self.head_index.map(|index| {
let entry = self.values.remove(index).unwrap();
self.head_index = entry.next_index;
self.remaining -= 1;
entry.value
})
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
pub struct EntryValuesMut<'map, Key, Value> {
head_index: Option<Index<ValueEntry<Key, Value>>>,
phantom: PhantomData<&'map mut VecList<ValueEntry<Key, Value>>>,
remaining: usize,
tail_index: Option<Index<ValueEntry<Key, Value>>>,
values: *mut VecList<ValueEntry<Key, Value>>,
}
impl<'map, Key, Value> EntryValuesMut<'map, Key, Value> {
#[must_use]
fn empty(values: &'map mut VecList<ValueEntry<Key, Value>>) -> Self {
EntryValuesMut {
head_index: None,
phantom: PhantomData,
remaining: 0,
tail_index: None,
values,
}
}
#[must_use]
fn from_map_entry(
values: &'map mut VecList<ValueEntry<Key, Value>>,
map_entry: &MapEntry<Key, Value>,
) -> Self {
EntryValuesMut {
head_index: Some(map_entry.head_index),
phantom: PhantomData,
remaining: map_entry.length,
tail_index: Some(map_entry.tail_index),
values,
}
}
#[must_use]
pub fn iter(&self) -> EntryValues<'_, Key, Value> {
EntryValues {
head_index: self.head_index,
remaining: self.remaining,
tail_index: self.tail_index,
values: unsafe { &*self.values },
}
}
}
impl<Key, Value> Debug for EntryValuesMut<'_, Key, Value>
where
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("EntryValuesMut(")?;
formatter.debug_list().entries(self.iter()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for EntryValuesMut<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
None
} else {
self.tail_index.map(|index| {
let entry = unsafe { (*self.values).get_mut(index) }.unwrap();
self.tail_index = entry.previous_index;
self.remaining -= 1;
&mut entry.value
})
}
}
}
impl<Key, Value> ExactSizeIterator for EntryValuesMut<'_, Key, Value> {}
impl<Key, Value> FusedIterator for EntryValuesMut<'_, Key, Value> {}
impl<'map, Key, Value> Iterator for EntryValuesMut<'map, Key, Value> {
type Item = &'map mut Value;
fn next(&mut self) -> Option<Self::Item> {
if self.remaining == 0 {
None
} else {
self.head_index.map(|index| {
let entry = unsafe { (*self.values).get_mut(index) }.unwrap();
self.head_index = entry.next_index;
self.remaining -= 1;
&mut entry.value
})
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
unsafe impl<Key, Value> Send for EntryValuesMut<'_, Key, Value>
where
Key: Send,
Value: Send,
{
}
unsafe impl<Key, Value> Sync for EntryValuesMut<'_, Key, Value>
where
Key: Sync,
Value: Sync,
{
}
pub struct IntoIter<Key, Value> {
keys: VecList<Key>,
iter: VecListIntoIter<ValueEntry<Key, Value>>,
}
impl<Key, Value> IntoIter<Key, Value> {
#[must_use]
pub fn iter(&self) -> Iter<'_, Key, Value> {
Iter {
keys: &self.keys,
iter: self.iter.iter(),
}
}
}
impl<Key, Value> Debug for IntoIter<Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("IntoIter(")?;
formatter.debug_list().entries(self.iter()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for IntoIter<Key, Value>
where
Key: Clone,
{
fn next_back(&mut self) -> Option<Self::Item> {
let value_entry = self.iter.next_back()?;
let key = self.keys.get(value_entry.key_index).cloned().unwrap();
Some((key, value_entry.value))
}
}
impl<Key, Value> ExactSizeIterator for IntoIter<Key, Value> where Key: Clone {}
impl<Key, Value> FusedIterator for IntoIter<Key, Value> where Key: Clone {}
impl<Key, Value> Iterator for IntoIter<Key, Value>
where
Key: Clone,
{
type Item = (Key, Value);
fn next(&mut self) -> Option<Self::Item> {
let value_entry = self.iter.next()?;
let key = self.keys.get(value_entry.key_index).cloned().unwrap();
Some((key, value_entry.value))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
pub struct Iter<'map, Key, Value> {
keys: &'map VecList<Key>,
iter: VecListIter<'map, ValueEntry<Key, Value>>,
}
impl<'map, Key, Value> Clone for Iter<'map, Key, Value> {
fn clone(&self) -> Iter<'map, Key, Value> {
Iter {
keys: self.keys,
iter: self.iter.clone(),
}
}
}
impl<Key, Value> Debug for Iter<'_, Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("Iter(")?;
formatter.debug_list().entries(self.clone()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for Iter<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
let value_entry = self.iter.next_back()?;
let key = self.keys.get(value_entry.key_index).unwrap();
Some((key, &value_entry.value))
}
}
impl<Key, Value> ExactSizeIterator for Iter<'_, Key, Value> {}
impl<Key, Value> FusedIterator for Iter<'_, Key, Value> {}
impl<'map, Key, Value> Iterator for Iter<'map, Key, Value> {
type Item = (&'map Key, &'map Value);
fn next(&mut self) -> Option<Self::Item> {
let value_entry = self.iter.next()?;
let key = self.keys.get(value_entry.key_index).unwrap();
Some((key, &value_entry.value))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
pub struct IterMut<'map, Key, Value> {
keys: &'map VecList<Key>,
iter: VecListIterMut<'map, ValueEntry<Key, Value>>,
}
impl<Key, Value> IterMut<'_, Key, Value> {
#[must_use]
pub fn iter(&self) -> Iter<'_, Key, Value> {
Iter {
keys: self.keys,
iter: self.iter.iter(),
}
}
}
impl<Key, Value> Debug for IterMut<'_, Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("IterMut(")?;
formatter.debug_list().entries(self.iter()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for IterMut<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
let value_entry = self.iter.next_back()?;
let key = self.keys.get(value_entry.key_index).unwrap();
Some((key, &mut value_entry.value))
}
}
impl<Key, Value> ExactSizeIterator for IterMut<'_, Key, Value> {}
impl<Key, Value> FusedIterator for IterMut<'_, Key, Value> {}
impl<'map, Key, Value> Iterator for IterMut<'map, Key, Value> {
type Item = (&'map Key, &'map mut Value);
fn next(&mut self) -> Option<Self::Item> {
let value_entry = self.iter.next()?;
let key = self.keys.get(value_entry.key_index).unwrap();
Some((key, &mut value_entry.value))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
pub struct KeyValues<'map, Key, Value, State = RandomState> {
build_hasher: &'map State,
keys: &'map VecList<Key>,
iter: VecListIter<'map, Key>,
map: &'map HashMap<Index<Key>, MapEntry<Key, Value>, DummyState>,
values: &'map VecList<ValueEntry<Key, Value>>,
}
impl<'map, Key, Value, State> Clone for KeyValues<'map, Key, Value, State> {
fn clone(&self) -> KeyValues<'map, Key, Value, State> {
KeyValues {
build_hasher: self.build_hasher,
keys: self.keys,
iter: self.iter.clone(),
map: self.map,
values: self.values,
}
}
}
impl<Key, Value, State> Debug for KeyValues<'_, Key, Value, State>
where
Key: Debug + Eq + Hash,
State: BuildHasher,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("KeyValues(")?;
formatter.debug_list().entries(self.clone()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value, State> DoubleEndedIterator for KeyValues<'_, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
fn next_back(&mut self) -> Option<Self::Item> {
let key = self.iter.next_back()?;
let hash = hash_key(self.build_hasher, key);
let (_, map_entry) = raw_entry(self.keys, self.map, hash, key).unwrap();
let iter = EntryValues::from_map_entry(self.values, map_entry);
Some((key, iter))
}
}
impl<Key, Value, State> ExactSizeIterator for KeyValues<'_, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
}
impl<Key, Value, State> FusedIterator for KeyValues<'_, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
}
impl<'map, Key, Value, State> Iterator for KeyValues<'map, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
type Item = (&'map Key, EntryValues<'map, Key, Value>);
fn next(&mut self) -> Option<Self::Item> {
let key = self.iter.next()?;
let hash = hash_key(self.build_hasher, key);
let (_, map_entry) = raw_entry(self.keys, self.map, hash, key).unwrap();
let iter = EntryValues::from_map_entry(self.values, map_entry);
Some((key, iter))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
pub struct KeyValuesMut<'map, Key, Value, State = RandomState> {
build_hasher: &'map State,
keys: &'map VecList<Key>,
iter: VecListIter<'map, Key>,
map: &'map HashMap<Index<Key>, MapEntry<Key, Value>, DummyState>,
values: *mut VecList<ValueEntry<Key, Value>>,
}
impl<Key, Value, State> KeyValuesMut<'_, Key, Value, State> {
#[must_use]
pub fn iter(&self) -> KeyValues<'_, Key, Value, State> {
KeyValues {
build_hasher: self.build_hasher,
keys: self.keys,
iter: self.iter.clone(),
map: self.map,
values: unsafe { &*self.values },
}
}
}
impl<Key, Value, State> Debug for KeyValuesMut<'_, Key, Value, State>
where
Key: Debug + Eq + Hash,
State: BuildHasher,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("KeyValuesMut(")?;
formatter.debug_list().entries(self.iter()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value, State> DoubleEndedIterator for KeyValuesMut<'_, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
fn next_back(&mut self) -> Option<Self::Item> {
let key = self.iter.next_back()?;
let hash = hash_key(self.build_hasher, key);
let (_, map_entry) = raw_entry(self.keys, self.map, hash, key).unwrap();
let iter = EntryValuesMut::from_map_entry(unsafe { &mut *self.values }, map_entry);
Some((key, iter))
}
}
impl<Key, Value, State> ExactSizeIterator for KeyValuesMut<'_, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
}
impl<Key, Value, State> FusedIterator for KeyValuesMut<'_, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
}
impl<'map, Key, Value, State> Iterator for KeyValuesMut<'map, Key, Value, State>
where
Key: Eq + Hash,
State: BuildHasher,
{
type Item = (&'map Key, EntryValuesMut<'map, Key, Value>);
fn next(&mut self) -> Option<Self::Item> {
let key = self.iter.next()?;
let hash = hash_key(self.build_hasher, key);
let (_, map_entry) = raw_entry(self.keys, self.map, hash, key).unwrap();
let iter = EntryValuesMut::from_map_entry(unsafe { &mut *self.values }, map_entry);
Some((key, iter))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
unsafe impl<Key, Value> Send for KeyValuesMut<'_, Key, Value>
where
Key: Send,
Value: Send,
{
}
unsafe impl<Key, Value> Sync for KeyValuesMut<'_, Key, Value>
where
Key: Sync,
Value: Sync,
{
}
pub struct Keys<'map, Key>(VecListIter<'map, Key>);
impl<'map, Key> Clone for Keys<'map, Key> {
fn clone(&self) -> Keys<'map, Key> {
Keys(self.0.clone())
}
}
impl<Key> Debug for Keys<'_, Key>
where
Key: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("Keys(")?;
formatter.debug_list().entries(self.clone()).finish()?;
formatter.write_str(")")
}
}
impl<Key> DoubleEndedIterator for Keys<'_, Key> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}
impl<Key> ExactSizeIterator for Keys<'_, Key> {}
impl<Key> FusedIterator for Keys<'_, Key> {}
impl<'map, Key> Iterator for Keys<'map, Key> {
type Item = &'map Key;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct Values<'map, Key, Value>(VecListIter<'map, ValueEntry<Key, Value>>);
impl<'map, Key, Value> Clone for Values<'map, Key, Value> {
fn clone(&self) -> Values<'map, Key, Value> {
Values(self.0.clone())
}
}
impl<Key, Value> Debug for Values<'_, Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("Values(")?;
formatter.debug_list().entries(self.clone()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for Values<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(|entry| &entry.value)
}
}
impl<Key, Value> ExactSizeIterator for Values<'_, Key, Value> {}
impl<Key, Value> FusedIterator for Values<'_, Key, Value> {}
impl<'map, Key, Value> Iterator for Values<'map, Key, Value> {
type Item = &'map Value;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|entry| &entry.value)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
pub struct ValuesMut<'map, Key, Value>(VecListIterMut<'map, ValueEntry<Key, Value>>);
impl<Key, Value> ValuesMut<'_, Key, Value> {
#[must_use]
pub fn iter(&self) -> Values<'_, Key, Value> {
Values(self.0.iter())
}
}
impl<Key, Value> Debug for ValuesMut<'_, Key, Value>
where
Key: Debug,
Value: Debug,
{
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str("ValuesMut(")?;
formatter.debug_list().entries(self.iter()).finish()?;
formatter.write_str(")")
}
}
impl<Key, Value> DoubleEndedIterator for ValuesMut<'_, Key, Value> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back().map(|entry| &mut entry.value)
}
}
impl<Key, Value> ExactSizeIterator for ValuesMut<'_, Key, Value> {}
impl<Key, Value> FusedIterator for ValuesMut<'_, Key, Value> {}
impl<'map, Key, Value> Iterator for ValuesMut<'map, Key, Value> {
type Item = &'map mut Value;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|entry| &mut entry.value)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
#[derive(Clone, Debug)]
pub(crate) struct DummyState;
impl BuildHasher for DummyState {
type Hasher = DummyHasher;
fn build_hasher(&self) -> Self::Hasher {
DummyHasher
}
}
#[derive(Clone, Debug)]
pub struct DummyHasher;
impl Hasher for DummyHasher {
fn finish(&self) -> u64 {
unimplemented!();
}
fn write(&mut self, _: &[u8]) {
unimplemented!();
}
}
#[must_use]
fn hash_key<KeyQuery, State>(state: &State, key: &KeyQuery) -> u64
where
KeyQuery: ?Sized + Eq + Hash,
State: BuildHasher,
{
let mut hasher = state.build_hasher();
key.hash(&mut hasher);
hasher.finish()
}
#[must_use]
fn raw_entry<'map, Key, KeyQuery, Value, State>(
keys: &VecList<Key>,
map: &'map HashMap<Index<Key>, MapEntry<Key, Value>, State>,
hash: u64,
key: &KeyQuery,
) -> Option<(&'map Index<Key>, &'map MapEntry<Key, Value>)>
where
Key: Borrow<KeyQuery> + Eq + Hash,
KeyQuery: ?Sized + Eq + Hash,
State: BuildHasher,
{
map.raw_entry().from_hash(hash, |&key_index| {
let existing_key = keys.get(key_index).unwrap();
key == existing_key.borrow()
})
}
#[must_use]
fn raw_entry_mut<'map, Key, KeyQuery, Value, State>(
keys: &VecList<Key>,
map: &'map mut HashMap<Index<Key>, MapEntry<Key, Value>, State>,
hash: u64,
key: &KeyQuery,
) -> RawEntryMut<'map, Index<Key>, MapEntry<Key, Value>, State>
where
Key: Borrow<KeyQuery> + Eq + Hash,
KeyQuery: ?Sized + Eq + Hash,
State: BuildHasher,
{
map.raw_entry_mut().from_hash(hash, |&key_index| {
let existing_key = keys.get(key_index).unwrap();
key == existing_key.borrow()
})
}
#[must_use]
fn raw_entry_mut_empty<'map, Key, KeyQuery, Value, State>(
keys: &VecList<Key>,
map: &'map mut HashMap<Index<Key>, MapEntry<Key, Value>, State>,
hash: u64,
) -> RawEntryMut<'map, Index<Key>, MapEntry<Key, Value>, State>
where
Key: Borrow<KeyQuery> + Eq + Hash,
KeyQuery: ?Sized + Eq + Hash,
State: BuildHasher,
{
map
.raw_entry_mut()
.from_hash(hash, |&key_index| keys.get(key_index).is_none())
}
#[allow(unused_results)]
#[cfg(all(test, feature = "std"))]
mod test {
use coverage_helper::test;
use super::*;
#[test]
fn test_bounds() {
fn check_bounds<Type: Send + Sync>() {}
check_bounds::<EntryValues<'static, (), ()>>();
check_bounds::<EntryValuesDrain<'static, (), ()>>();
check_bounds::<EntryValuesMut<'static, (), ()>>();
check_bounds::<IntoIter<(), ()>>();
check_bounds::<Iter<'static, (), ()>>();
check_bounds::<IterMut<'static, (), ()>>();
check_bounds::<KeyValues<'static, (), ()>>();
check_bounds::<KeyValuesMut<'static, (), ()>>();
check_bounds::<ListOrderedMultimap<(), ()>>();
check_bounds::<Values<'static, (), ()>>();
check_bounds::<ValuesMut<'static, (), ()>>();
}
#[test]
fn test_collision() {
struct TestBuildHasher;
impl BuildHasher for TestBuildHasher {
type Hasher = TestHasher;
fn build_hasher(&self) -> Self::Hasher {
TestHasher
}
}
struct TestHasher;
impl Hasher for TestHasher {
fn finish(&self) -> u64 {
0
}
fn write(&mut self, _: &[u8]) {}
}
let mut map = ListOrderedMultimap::with_hasher(TestBuildHasher);
let state = map.hasher();
assert_eq!(hash_key(state, "key1"), hash_key(state, "key2"));
map.insert("key1", "value1");
assert_eq!(map.get(&"key1"), Some(&"value1"));
map.insert("key2", "value2");
assert_eq!(map.get(&"key2"), Some(&"value2"));
}
#[test]
fn test_no_collision() {
let state = RandomState::new();
let hash_1 = hash_key(&state, "key1");
let hash_2 = hash_key(&state, "key2");
assert!(hash_1 != hash_2);
}
#[test]
fn test_entry_and_modify() {
let mut map = ListOrderedMultimap::new();
map
.entry("key")
.and_modify(|_| panic!("entry should be vacant"));
map.insert("key", "value1");
map.entry("key").and_modify(|value| *value = "value2");
assert_eq!(map.get(&"key"), Some(&"value2"));
}
#[test]
fn test_entry_or_insert() {
let mut map = ListOrderedMultimap::new();
let value = map.entry("key").or_insert("value1");
assert_eq!(value, &"value1");
let value = map.entry("key").or_insert("value2");
assert_eq!(value, &"value1");
}
#[test]
fn test_entry_or_insert_entry() {
let mut map = ListOrderedMultimap::new();
let entry = map.entry("key").or_insert_entry("value1");
assert_eq!(entry.get(), &"value1");
let entry = map.entry("key").or_insert_entry("value2");
assert_eq!(entry.get(), &"value1");
}
#[test]
fn test_entry_or_insert_with() {
let mut map = ListOrderedMultimap::new();
let value = map.entry("key").or_insert_with(|| "value1");
assert_eq!(value, &"value1");
let value = map.entry("key").or_insert_with(|| "value2");
assert_eq!(value, &"value1");
}
#[test]
fn test_entry_or_insert_with_entry() {
let mut map = ListOrderedMultimap::new();
let entry = map.entry("key").or_insert_with_entry(|| "value1");
assert_eq!(entry.get(), &"value1");
let entry = map.entry("key").or_insert_with_entry(|| "value2");
assert_eq!(entry.get(), &"value1");
}
#[test]
fn test_entry_debug() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let entry = map.entry("key");
assert_eq!(format!("{entry:?}"), r#"VacantEntry("key")"#);
}
#[test]
fn test_entry_values_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let iter = map.get_all(&"key");
assert_eq!(
format!("{iter:?}"),
r#"EntryValues(["value1", "value2", "value3", "value4"])"#
);
}
#[test]
fn test_entry_values_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next_back(), Some(&"value4"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next_back(), Some(&"value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_drain_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let iter = map.remove_all(&"key");
assert_eq!(
format!("{iter:?}"),
r#"EntryValuesDrain(["value1", "value2", "value3", "value4"])"#
);
}
#[test]
fn test_entry_values_drain_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let mut iter = map.remove_all(&"key");
assert_eq!(iter.next(), Some("value1"));
assert_eq!(iter.next_back(), Some("value4"));
assert_eq!(iter.next(), Some("value2"));
assert_eq!(iter.next_back(), Some("value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_drain_empty() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.remove_all(&"key");
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_drain_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.remove_all(&"key");
assert_eq!(iter.next(), Some("value"));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_drain_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let mut iter = map.remove_all(&"key");
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_entry_values_empty() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.get_all(&"key");
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), Some(&"value"));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_mut_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let iter = map.get_all_mut(&"key");
assert_eq!(
format!("{iter:?}"),
r#"EntryValuesMut(["value1", "value2", "value3", "value4"])"#
);
}
#[test]
fn test_entry_values_mut_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let mut iter = map.get_all_mut(&"key");
assert_eq!(iter.next(), Some(&mut "value1"));
assert_eq!(iter.next_back(), Some(&mut "value4"));
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next_back(), Some(&mut "value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_mut_empty() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.get_all_mut(&"key");
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_mut_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.get_all_mut(&"key");
assert_eq!(iter.next(), Some(&mut "value"));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_entry_values_mut_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let mut iter = map.get_all_mut(&"key");
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_entry_values_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let mut iter = map.get_all(&"key");
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_iter_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.iter();
assert_eq!(
format!("{iter:?}"),
r#"Iter([("key1", "value1"), ("key2", "value2"), ("key2", "value3"), ("key1", "value4")])"#
);
}
#[test]
fn test_iter_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &"value1")));
assert_eq!(iter.next_back(), Some((&"key1", &"value4")));
assert_eq!(iter.next(), Some((&"key2", &"value2")));
assert_eq!(iter.next_back(), Some((&"key2", &"value3")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_iter_empty() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.iter();
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_iter_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key", &"value")));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_iter_mut_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.iter_mut();
assert_eq!(
format!("{iter:?}"),
r#"IterMut([("key1", "value1"), ("key2", "value2"), ("key2", "value3"), ("key1", "value4")])"#
);
}
#[test]
fn test_iter_mut_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.iter_mut();
assert_eq!(iter.next(), Some((&"key1", &mut "value1")));
assert_eq!(iter.next_back(), Some((&"key1", &mut "value4")));
assert_eq!(iter.next(), Some((&"key2", &mut "value2")));
assert_eq!(iter.next_back(), Some((&"key2", &mut "value3")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_iter_mut_empty() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.iter_mut();
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_iter_mut_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.iter_mut();
assert_eq!(iter.next(), Some((&"key", &mut "value")));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_iter_mut_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.iter_mut();
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_iter_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.iter();
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_into_iter_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.into_iter();
assert_eq!(
format!("{iter:?}"),
r#"IntoIter([("key1", "value1"), ("key2", "value2"), ("key2", "value3"), ("key1", "value4")])"#
);
}
#[test]
fn test_into_iter_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.into_iter();
assert_eq!(iter.next(), Some(("key1", "value1")));
assert_eq!(iter.next_back(), Some(("key1", "value4")));
assert_eq!(iter.next(), Some(("key2", "value2")));
assert_eq!(iter.next_back(), Some(("key2", "value3")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_into_iter_empty() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.into_iter();
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_into_iter_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.into_iter();
assert_eq!(iter.next(), Some(("key", "value")));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_into_iter_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.into_iter();
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_key_values_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.pairs();
assert_eq!(
format!("{iter:?}"),
r#"KeyValues([("key1", EntryValues(["value1", "value4"])), ("key2", EntryValues(["value2", "value3"]))])"#
);
}
#[test]
fn test_key_values_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.pairs();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key1");
assert_eq!(values.next(), Some(&"value1"));
assert_eq!(values.next(), Some(&"value4"));
assert_eq!(values.next(), None);
let (key, mut values) = iter.next_back().unwrap();
assert_eq!(key, &"key2");
assert_eq!(values.next(), Some(&"value2"));
assert_eq!(values.next(), Some(&"value3"));
assert_eq!(values.next(), None);
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
}
#[test]
fn test_key_values_empty() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.pairs();
assert!(iter.next_back().is_none());
assert!(iter.next().is_none());
}
#[test]
fn test_key_values_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.pairs();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key");
assert_eq!(values.next(), Some(&"value"));
assert_eq!(values.next(), None);
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
assert!(iter.next().is_none());
}
#[test]
fn test_key_values_mut_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.pairs_mut();
assert_eq!(
format!("{iter:?}"),
r#"KeyValuesMut([("key1", EntryValues(["value1", "value4"])), ("key2", EntryValues(["value2", "value3"]))])"#
);
}
#[test]
fn test_key_values_mut_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.pairs_mut();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key1");
assert_eq!(values.next(), Some(&mut "value1"));
assert_eq!(values.next(), Some(&mut "value4"));
assert_eq!(values.next(), None);
let (key, mut values) = iter.next_back().unwrap();
assert_eq!(key, &"key2");
assert_eq!(values.next(), Some(&mut "value2"));
assert_eq!(values.next(), Some(&mut "value3"));
assert_eq!(values.next(), None);
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
}
#[test]
fn test_key_values_mut_empty() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.pairs_mut();
assert!(iter.next_back().is_none());
assert!(iter.next().is_none());
}
#[test]
fn test_key_values_mut_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.pairs_mut();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key");
assert_eq!(values.next(), Some(&mut "value"));
assert_eq!(values.next(), None);
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
assert!(iter.next().is_none());
assert!(iter.next_back().is_none());
assert!(iter.next().is_none());
}
#[test]
fn test_key_values_mut_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.pairs_mut();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_key_values_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.pairs();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_keys_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.keys();
assert_eq!(format!("{iter:?}"), r#"Keys(["key1", "key2"])"#);
}
#[test]
fn test_keys_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.keys();
assert_eq!(iter.next(), Some(&"key1"));
assert_eq!(iter.next_back(), Some(&"key2"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_keys_empty() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.keys();
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_keys_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.keys();
assert_eq!(iter.next(), Some(&"key"));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_keys_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.keys();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_list_ordered_multimap_append() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.entry_len(&"key"), 0);
let already_exists = map.append("key", "value1");
assert!(!already_exists);
assert_eq!(map.entry_len(&"key"), 1);
let already_exists = map.append("key", "value2");
assert!(already_exists);
assert_eq!(map.entry_len(&"key"), 2);
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_back() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.back(), None);
map.insert("key1", "value1");
assert_eq!(map.back(), Some((&"key1", &"value1")));
map.append("key2", "value2");
assert_eq!(map.back(), Some((&"key2", &"value2")));
map.remove(&"key2");
assert_eq!(map.back(), Some((&"key1", &"value1")));
map.remove(&"key1");
assert_eq!(map.back(), None);
}
#[test]
fn test_list_ordered_multimap_back_mut() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.back(), None);
map.insert("key1", "value1");
assert_eq!(map.back(), Some((&"key1", &"value1")));
map.append("key2", "value2");
assert_eq!(map.back(), Some((&"key2", &"value2")));
map.remove(&"key2");
assert_eq!(map.back(), Some((&"key1", &"value1")));
map.remove(&"key1");
assert_eq!(map.back(), None);
}
#[test]
fn test_list_ordered_multimap_clear() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
map.insert("key2", "value");
map.clear();
assert!(map.is_empty());
assert_eq!(map.get(&"key"), None);
assert_eq!(map.get(&"key2"), None);
}
#[test]
fn test_list_ordered_multimap_contains_key() {
let mut map = ListOrderedMultimap::new();
assert!(!map.contains_key(&"key"));
map.insert("key", "value");
assert!(map.contains_key(&"key"));
}
#[test]
fn test_list_ordered_multimap_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
assert_eq!(
format!("{map:?}"),
r#"{"key1": "value1", "key2": "value2", "key2": "value3", "key1": "value4"}"#
);
}
#[test]
fn test_list_ordered_multimap_entry() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.get(&"key1"), None);
let value = map.entry("key").or_insert("value1");
assert_eq!(value, &"value1");
assert_eq!(map.get(&"key"), Some(&"value1"));
let value = map.entry("key").or_insert("value2");
assert_eq!(value, &"value1");
assert_eq!(map.get(&"key"), Some(&"value1"));
}
#[test]
fn test_list_ordered_multimap_entry_len() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.entry_len(&"key1"), 0);
map.insert("key", "value");
assert_eq!(map.entry_len(&"key"), 1);
map.insert("key", "value");
assert_eq!(map.entry_len(&"key"), 1);
map.append("key", "value");
assert_eq!(map.entry_len(&"key"), 2);
map.insert("key", "value");
assert_eq!(map.entry_len(&"key"), 1);
map.remove(&"key");
assert_eq!(map.entry_len(&"key"), 0);
}
#[test]
fn test_list_ordered_multimap_equality() {
let mut map_1 = ListOrderedMultimap::new();
map_1.insert("key1", "value1");
map_1.insert("key2", "value2");
map_1.append("key2", "value3");
map_1.append("key1", "value4");
let mut map_2 = map_1.clone();
map_2.pop_back();
assert_ne!(map_1, map_2);
map_2.append("key1", "value4");
assert_eq!(map_1, map_2);
}
#[test]
fn test_list_ordered_multimap_extend() {
let mut map = ListOrderedMultimap::new();
map.extend(vec![
("key1", "value1"),
("key2", "value2"),
("key2", "value3"),
]);
let mut iter = map.get_all(&"key1");
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), None);
let mut iter = map.get_all(&"key2");
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), Some(&"value3"));
assert_eq!(iter.next(), None);
let mut map = ListOrderedMultimap::new();
map.extend(vec![(&1, &1), (&2, &1), (&2, &2)]);
let mut iter = map.get_all(&1);
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), None);
let mut iter = map.get_all(&2);
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_from_iterator() {
let map: ListOrderedMultimap<_, _, RandomState> = ListOrderedMultimap::from_iter(vec![
("key1", "value1"),
("key2", "value2"),
("key2", "value3"),
]);
let mut iter = map.get_all(&"key1");
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), None);
let mut iter = map.get_all(&"key2");
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), Some(&"value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_get() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.get(&"key"), None);
map.insert("key", "value");
assert_eq!(map.get(&"key"), Some(&"value"));
}
#[test]
fn test_list_ordered_multimap_get_all() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), None);
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), Some(&"value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_get_all_mut() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), None);
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
let mut iter = map.get_all_mut(&"key");
assert_eq!(iter.next(), Some(&mut "value1"));
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next(), Some(&mut "value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_get_mut() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.get_mut(&"key"), None);
map.insert("key", "value");
assert_eq!(map.get_mut(&"key"), Some(&mut "value"));
}
#[test]
fn test_list_ordered_multimap_insert() {
let mut map = ListOrderedMultimap::new();
assert!(!map.contains_key(&"key"));
assert_eq!(map.get(&"key"), None);
let value = map.insert("key", "value1");
assert_eq!(value, None);
assert!(map.contains_key(&"key"));
assert_eq!(map.get(&"key"), Some(&"value1"));
let value = map.insert("key", "value2");
assert_eq!(value, Some("value1"));
assert!(map.contains_key(&"key"));
assert_eq!(map.get(&"key"), Some(&"value2"));
}
#[test]
fn test_list_ordered_multimap_insert_all() {
let mut map = ListOrderedMultimap::new();
assert!(!map.contains_key(&"key"));
assert_eq!(map.get(&"key"), None);
{
let mut iter = map.insert_all("key", "value1");
assert_eq!(iter.next(), None);
}
assert!(map.contains_key(&"key"));
assert_eq!(map.get(&"key"), Some(&"value1"));
{
let mut iter = map.insert_all("key", "value2");
assert_eq!(iter.next(), Some("value1"));
assert_eq!(iter.next(), None);
}
assert!(map.contains_key(&"key"));
assert_eq!(map.get(&"key"), Some(&"value2"));
}
#[test]
fn test_list_ordered_multimap_is_empty() {
let mut map = ListOrderedMultimap::new();
assert!(map.is_empty());
map.insert("key", "value");
assert!(!map.is_empty());
map.remove(&"key");
assert!(map.is_empty());
}
#[test]
fn test_list_ordered_multimap_iter() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.iter();
assert_eq!(iter.next(), None);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &"value1")));
assert_eq!(iter.next(), Some((&"key2", &"value2")));
assert_eq!(iter.next(), Some((&"key2", &"value3")));
assert_eq!(iter.next(), Some((&"key1", &"value4")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_iter_mut() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.iter_mut();
assert_eq!(iter.next(), None);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.iter_mut();
assert_eq!(iter.next(), Some((&"key1", &mut "value1")));
assert_eq!(iter.next(), Some((&"key2", &mut "value2")));
assert_eq!(iter.next(), Some((&"key2", &mut "value3")));
assert_eq!(iter.next(), Some((&"key1", &mut "value4")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_keys() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.keys();
assert_eq!(iter.next(), None);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.insert("key1", "value3");
map.insert("key3", "value4");
let mut iter = map.keys();
assert_eq!(iter.next(), Some(&"key1"));
assert_eq!(iter.next(), Some(&"key2"));
assert_eq!(iter.next(), Some(&"key3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_keys_capacity() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.keys_capacity(), 0);
map.insert("key", "value");
assert!(map.keys_capacity() > 0);
}
#[test]
fn test_list_ordered_multimap_keys_len() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.keys_len(), 0);
map.insert("key1", "value1");
assert_eq!(map.keys_len(), 1);
map.insert("key2", "value2");
assert_eq!(map.keys_len(), 2);
map.append("key1", "value3");
assert_eq!(map.keys_len(), 2);
map.remove(&"key1");
assert_eq!(map.keys_len(), 1);
map.remove(&"key2");
assert_eq!(map.keys_len(), 0);
}
#[test]
fn test_list_ordered_multimap_new() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
assert_eq!(map.keys_capacity(), 0);
assert_eq!(map.keys_len(), 0);
assert_eq!(map.values_capacity(), 0);
assert_eq!(map.values_len(), 0);
}
#[test]
fn test_list_ordered_multimap_pack_to() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::with_capacity(5, 5);
map.pack_to_fit();
assert_eq!(map.keys_capacity(), 0);
assert_eq!(map.values_capacity(), 0);
let mut map = ListOrderedMultimap::with_capacity(10, 10);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
map.pack_to(5, 5);
assert_eq!(map.get(&"key1"), Some(&"value1"));
assert_eq!(map.get(&"key2"), Some(&"value2"));
assert_eq!(map.keys_capacity(), 5);
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_capacity(), 5);
assert_eq!(map.values_len(), 4);
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &"value1")));
assert_eq!(iter.next(), Some((&"key2", &"value2")));
assert_eq!(iter.next(), Some((&"key2", &"value3")));
assert_eq!(iter.next(), Some((&"key1", &"value4")));
assert_eq!(iter.next(), None);
}
#[should_panic]
#[test]
fn test_list_ordered_multimap_pack_to_panic_key_capacity() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
map.pack_to(1, 5);
}
#[should_panic]
#[test]
fn test_list_ordered_multimap_pack_to_panic_value_capacity() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
map.pack_to(5, 1);
}
#[test]
fn test_list_ordered_multimap_pack_to_fit() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::with_capacity(5, 5);
map.pack_to_fit();
assert_eq!(map.keys_capacity(), 0);
assert_eq!(map.values_capacity(), 0);
let mut map = ListOrderedMultimap::with_capacity(5, 5);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
map.pack_to_fit();
assert_eq!(map.keys_capacity(), 2);
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_capacity(), 4);
assert_eq!(map.values_len(), 4);
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &"value1")));
assert_eq!(iter.next(), Some((&"key2", &"value2")));
assert_eq!(iter.next(), Some((&"key2", &"value3")));
assert_eq!(iter.next(), Some((&"key1", &"value4")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_pairs() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.pairs();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key1");
assert_eq!(values.next(), Some(&"value1"));
assert_eq!(values.next(), Some(&"value4"));
assert_eq!(values.next(), None);
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key2");
assert_eq!(values.next(), Some(&"value2"));
assert_eq!(values.next(), Some(&"value3"));
assert_eq!(values.next(), None);
assert!(iter.next().is_none());
}
#[test]
fn test_list_ordered_multimap_pairs_mut() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.pairs_mut();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key1");
assert_eq!(values.next(), Some(&mut "value1"));
assert_eq!(values.next(), Some(&mut "value4"));
assert_eq!(values.next(), None);
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key2");
assert_eq!(values.next(), Some(&mut "value2"));
assert_eq!(values.next(), Some(&mut "value3"));
assert_eq!(values.next(), None);
assert!(iter.next().is_none());
}
#[test]
fn test_list_ordered_multimap_pop_back() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let (key, value) = map.pop_back().unwrap();
assert_eq!(key, KeyWrapper::Borrowed(&"key1"));
assert_eq!(&value, &"value4");
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_len(), 3);
let (key, value) = map.pop_back().unwrap();
assert_eq!(key, KeyWrapper::Borrowed(&"key2"));
assert_eq!(&value, &"value3");
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_len(), 2);
let (key, value) = map.pop_back().unwrap();
assert_eq!(key, KeyWrapper::Owned("key2"));
assert_eq!(&value, &"value2");
assert_eq!(map.keys_len(), 1);
assert_eq!(map.values_len(), 1);
let (key, value) = map.pop_back().unwrap();
assert_eq!(key, KeyWrapper::Owned("key1"));
assert_eq!(&value, &"value1");
assert_eq!(map.keys_len(), 0);
assert_eq!(map.values_len(), 0);
assert!(map.pop_back().is_none());
}
#[test]
fn test_list_ordered_multimap_pop_front() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let (key, value) = map.pop_front().unwrap();
assert_eq!(key, KeyWrapper::Borrowed(&"key1"));
assert_eq!(&value, &"value1");
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_len(), 3);
let (key, value) = map.pop_front().unwrap();
assert_eq!(key, KeyWrapper::Borrowed(&"key2"));
assert_eq!(&value, &"value2");
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_len(), 2);
let (key, value) = map.pop_front().unwrap();
assert_eq!(key, KeyWrapper::Owned("key2"));
assert_eq!(&value, &"value3");
assert_eq!(map.keys_len(), 1);
assert_eq!(map.values_len(), 1);
let (key, value) = map.pop_front().unwrap();
assert_eq!(key, KeyWrapper::Owned("key1"));
assert_eq!(&value, &"value4");
assert_eq!(map.keys_len(), 0);
assert_eq!(map.values_len(), 0);
assert!(map.pop_front().is_none());
}
#[test]
fn test_list_ordered_multimap_remove() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.remove(&"key"), None);
map.insert("key", "value1");
map.append("key", "value2");
assert_eq!(map.remove(&"key"), Some("value1"));
assert_eq!(map.remove(&"key"), None);
}
#[test]
fn test_list_ordered_multimap_remove_all() {
let mut map = ListOrderedMultimap::new();
{
let mut iter = map.remove_all(&"key");
assert_eq!(iter.next(), None);
}
map.insert("key", "value1");
map.append("key", "value2");
{
let mut iter = map.remove_all(&"key");
assert_eq!(iter.next(), Some("value1"));
assert_eq!(iter.next(), Some("value2"));
assert_eq!(iter.next(), None);
}
let mut iter = map.remove_all(&"key");
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_remove_entry() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.remove_entry(&"key"), None);
map.insert("key", "value1");
map.append("key", "value2");
assert_eq!(map.remove_entry(&"key"), Some(("key", "value1")));
assert_eq!(map.remove_entry(&"key"), None);
}
#[test]
fn test_list_ordered_multimap_remove_entry_all() {
let mut map = ListOrderedMultimap::new();
{
let entry = map.remove_entry_all(&"key");
assert!(entry.is_none());
}
map.insert("key", "value1");
map.append("key", "value2");
{
let (key, mut iter) = map.remove_entry_all(&"key").unwrap();
assert_eq!(key, "key");
assert_eq!(iter.next(), Some("value1"));
assert_eq!(iter.next(), Some("value2"));
assert_eq!(iter.next(), None);
}
let entry = map.remove_entry_all(&"key");
assert!(entry.is_none());
}
#[test]
fn test_list_ordered_multimap_reserve_keys() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
assert_eq!(map.keys_capacity(), 0);
map.reserve_keys(5);
assert!(map.keys_capacity() >= 5);
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::with_capacity(5, 5);
assert_eq!(map.keys_capacity(), 5);
map.reserve_keys(2);
assert_eq!(map.keys_capacity(), 5);
}
#[test]
fn test_list_ordered_multimap_reserve_values() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
assert_eq!(map.values_capacity(), 0);
map.reserve_values(5);
assert!(map.values_capacity() >= 5);
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::with_capacity(5, 5);
assert_eq!(map.values_capacity(), 5);
map.reserve_values(2);
assert_eq!(map.values_capacity(), 5);
}
#[test]
fn test_list_ordered_multimap_retain() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", 1);
map.insert("key2", 5);
map.append("key1", -1);
map.insert("key3", -10);
map.insert("key4", 1);
map.append("key4", -1);
map.append("key4", 1);
map.retain(|_, &mut value| value >= 0);
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &1)));
assert_eq!(iter.next(), Some((&"key2", &5)));
assert_eq!(iter.next(), Some((&"key4", &1)));
assert_eq!(iter.next(), Some((&"key4", &1)));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_values() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.iter();
assert_eq!(iter.next(), None);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.values();
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), Some(&"value3"));
assert_eq!(iter.next(), Some(&"value4"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_values_mut() {
let mut map = ListOrderedMultimap::new();
let mut iter = map.iter();
assert_eq!(iter.next(), None);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.values_mut();
assert_eq!(iter.next(), Some(&mut "value1"));
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next(), Some(&mut "value3"));
assert_eq!(iter.next(), Some(&mut "value4"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_list_ordered_multimap_values_capacity() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.values_capacity(), 0);
map.insert("key", "value");
assert!(map.values_capacity() > 0);
}
#[test]
fn test_list_ordered_multimap_values_len() {
let mut map = ListOrderedMultimap::new();
assert_eq!(map.values_len(), 0);
map.insert("key1", "value1");
assert_eq!(map.values_len(), 1);
map.insert("key2", "value2");
assert_eq!(map.values_len(), 2);
map.append("key1", "value3");
assert_eq!(map.values_len(), 3);
map.remove(&"key1");
assert_eq!(map.values_len(), 1);
map.remove(&"key2");
assert_eq!(map.values_len(), 0);
}
#[test]
fn test_list_ordered_multimap_with_capacity() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::with_capacity(1, 2);
assert!(map.keys_capacity() >= 1);
assert_eq!(map.keys_len(), 0);
assert!(map.values_capacity() >= 2);
assert_eq!(map.values_len(), 0);
}
#[test]
fn test_list_ordered_multimap_with_capacity_and_hasher() {
let state = RandomState::new();
let map: ListOrderedMultimap<&str, &str> =
ListOrderedMultimap::with_capacity_and_hasher(1, 2, state);
assert!(map.keys_capacity() >= 1);
assert_eq!(map.keys_len(), 0);
assert!(map.values_capacity() >= 2);
assert_eq!(map.values_len(), 0);
}
#[test]
fn test_occupied_entry_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
map.append("key", "value3");
map.append("key", "value4");
let entry = match map.entry("key") {
Entry::Occupied(entry) => entry,
_ => panic!("expected occupied entry"),
};
assert_eq!(
format!("{entry:?}"),
"OccupiedEntry { \
key: \"key\", \
values: EntryValues([\"value1\", \"value2\", \"value3\", \"value4\"]) \
}"
);
}
#[test]
fn test_vacant_entry_debug() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let entry = match map.entry("key") {
Entry::Vacant(entry) => entry,
_ => panic!("expected vacant entry"),
};
assert_eq!(format!("{entry:?}"), r#"VacantEntry("key")"#);
}
#[test]
fn test_values_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.values();
assert_eq!(
format!("{iter:?}"),
r#"Values(["value1", "value2", "value3", "value4"])"#
);
}
#[test]
fn test_values_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.values();
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next_back(), Some(&"value4"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next_back(), Some(&"value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_values_empty() {
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.values();
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_values_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.values();
assert_eq!(iter.next(), Some(&"value"));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_values_mut_debug() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let iter = map.values_mut();
assert_eq!(
format!("{iter:?}"),
r#"ValuesMut(["value1", "value2", "value3", "value4"])"#
);
}
#[test]
fn test_values_mut_double_ended() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.values_mut();
assert_eq!(iter.next(), Some(&mut "value1"));
assert_eq!(iter.next_back(), Some(&mut "value4"));
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next_back(), Some(&mut "value3"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_values_mut_empty() {
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let mut iter = map.values_mut();
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_values_mut_fused() {
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
let mut iter = map.values_mut();
assert_eq!(iter.next(), Some(&mut "value"));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
assert_eq!(iter.next(), None);
}
#[test]
fn test_values_mut_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.values_mut();
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[test]
fn test_values_size_hint() {
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.append("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
let mut iter = map.values();
assert_eq!(iter.size_hint(), (4, Some(4)));
iter.next();
assert_eq!(iter.size_hint(), (3, Some(3)));
iter.next();
assert_eq!(iter.size_hint(), (2, Some(2)));
iter.next();
assert_eq!(iter.size_hint(), (1, Some(1)));
iter.next();
assert_eq!(iter.size_hint(), (0, Some(0)));
}
#[should_panic]
#[test]
fn test_dummy_hasher_finish() {
let hasher = DummyHasher;
hasher.finish();
}
#[should_panic]
#[test]
fn test_dummy_hasher_write() {
let mut hasher = DummyHasher;
hasher.write(&[]);
}
}