use {
super::lib::{
fmt,
vec::{Drain as VecDrain, IntoIter as VecIntoIter},
},
core::{
borrow::Borrow,
iter::{
DoubleEndedIterator, ExactSizeIterator, FromIterator, FusedIterator, IntoIterator,
Iterator, Zip,
},
marker::PhantomData,
ops::{Index, IndexMut},
slice::{Iter as SliceIter, IterMut as SliceIterMut},
},
};
#[macro_export]
macro_rules! vecmap {
(@single $($x:tt)*) => (());
(@count $($rest:expr),*) => (<[()]>::len(&[ $($crate::vecmap!(@single $rest)),* ]));
( strict, data = { $( $key:expr => $value:expr, )+ } $( , )? ) => {{
$crate::vecmap!(strict, data = { $( $key => $value ),+ })
}};
( strict, data = { $( $key:expr => $value:expr ),* } $( , )? ) => {{
use $crate::{vec_map::VecMapExt, Peep};
let _cap = $crate::vecmap!(@count $($key),*);
$crate::vec_map::VecMap::with_capacity(_cap)
$(
.peep(|map| assert!(map.get(&$key).is_some()) )
.inserted($key, $value)
)*
}};
( $( $key:expr => $value:expr, )+ ) => {{
$crate::vecmap!( $( $key => $value ),+ )
}};
( $( $key:expr => $value:expr ),* ) => {{
use $crate::vec_map::VecMapExt;
let _cap = $crate::vecmap!(@count $($key),*);
$crate::vec_map::VecMap::with_capacity(_cap)
$(
.inserted($key, $value)
)*
}};
() => {
$crate::vec_map::VecMap::new()
};
}
pub struct VecMap<K, V> {
keys: Vec<K>,
values: Vec<V>,
}
impl<K, V> VecMap<K, V> {
#[inline]
pub fn new() -> VecMap<K, V> {
VecMap::default()
}
#[inline]
pub fn with_capacity(capacity: usize) -> VecMap<K, V> {
VecMap {
keys: Vec::with_capacity(capacity),
values: Vec::with_capacity(capacity),
}
}
#[inline]
pub fn capacity(&self) -> usize {
self.keys.capacity().min(self.values.capacity())
}
#[inline]
pub fn keys(&self) -> Keys<'_, K, V> {
Keys {
iter: self.keys.iter(),
_phantom: PhantomData,
}
}
#[inline]
pub fn values(&self) -> Values<'_, K, V> {
Values {
iter: self.values.iter(),
_phantom: PhantomData,
}
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut {
iter: self.values.iter_mut(),
_phantom: PhantomData,
}
}
#[inline]
pub fn iter(&self) -> Iter<'_, K, V> {
Iter {
iter: self.keys.iter().zip(self.values.iter()),
}
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut {
iter: self.keys.iter().zip(self.values.iter_mut()),
}
}
#[inline]
pub fn len(&self) -> usize {
self.keys.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn drain(&mut self) -> Drain<'_, K, V> {
Drain {
iter: self.keys.drain(..).zip(self.values.drain(..)),
}
}
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&K, &mut V) -> bool,
{
for i in (0..self.len()).rev() {
if !f(&self.keys[i], &mut self.values[i]) {
self.keys.swap_remove(i);
self.values.swap_remove(i);
}
}
}
#[inline]
pub fn clear(&mut self) {
self.keys.clear();
self.values.clear();
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.keys.reserve(additional);
self.values.reserve(additional);
}
#[inline]
pub fn shrink_to_fit(&mut self) {
self.keys.shrink_to_fit();
self.values.shrink_to_fit();
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.keys.truncate(len);
self.values.truncate(len);
}
}
impl<K, V> VecMap<K, V>
where
K: Eq,
{
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
match self.iter_mut().position(|(k, _)| &key == k) {
Some(index) => Entry::Occupied(OccupiedEntry { index, map: self }),
None => Entry::Vacant(VacantEntry { key, map: self }),
}
}
#[inline]
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&'_ V>
where
K: Borrow<Q>,
Q: Eq,
{
self.keys
.iter()
.position(|k| key.eq(k.borrow()))
.map(|p| &self.values[p])
}
#[inline]
pub fn get_key_value<Q: ?Sized>(&self, key: &Q) -> Option<(&'_ K, &'_ V)>
where
K: Borrow<Q>,
Q: PartialEq<K>,
{
self.keys
.iter()
.position(|k| key == k)
.map(|p| (&self.keys[p], &self.values[p]))
}
#[inline]
pub fn get_key_value_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<(&'_ K, &'_ mut V)>
where
K: Borrow<Q>,
Q: PartialEq<K>,
{
self.keys
.iter_mut()
.position(|k| key == k)
.map(move |p| (&self.keys[p], &mut self.values[p]))
}
#[inline]
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: PartialEq<K>,
{
self.keys.iter().any(|k| key == k)
}
#[inline]
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&'_ mut V>
where
K: Borrow<Q>,
Q: Eq,
{
self.keys
.iter()
.position(|k| key.eq(k.borrow()))
.map(move |p| &mut self.values[p])
}
#[inline]
pub fn insert(&mut self, key: K, mut value: V) -> Option<V> {
if let Some(position) = self.keys.iter().position(|k| &key == k) {
core::mem::swap(&mut value, &mut self.values[position]);
Some(value)
} else {
self.keys.push(key);
self.values.push(value);
None
}
}
#[inline]
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: PartialEq<K>,
{
if let Some(index) = self.keys.iter().position(|k| key == k) {
self.keys.swap_remove(index);
Some(self.values.swap_remove(index))
} else {
None
}
}
#[inline]
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: PartialEq<K>,
{
if let Some(index) = self.keys.iter().position(|k| key == k) {
Some((self.keys.swap_remove(index), self.values.swap_remove(index)))
} else {
None
}
}
}
impl<'a, K, V> IntoIterator for &'a VecMap<K, V> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut VecMap<K, V> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<K, V> IntoIterator for VecMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter {
iter: self.keys.into_iter().zip(self.values.into_iter()),
}
}
}
impl<K, V> FromIterator<(K, V)> for VecMap<K, V>
where
K: Eq,
{
#[inline]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let iter = iter.into_iter();
let mut map = Self::with_capacity(iter.size_hint().0);
iter.for_each(|(k, v)| {
map.insert(k, v);
});
map
}
}
impl<K, Q: ?Sized, V> Index<&Q> for VecMap<K, V>
where
K: Eq + Borrow<Q>,
Q: Eq,
{
type Output = V;
#[inline]
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
}
impl<K, Q: ?Sized, V> IndexMut<&Q> for VecMap<K, V>
where
K: Eq + Borrow<Q>,
Q: Eq,
{
#[inline]
fn index_mut(&mut self, key: &Q) -> &mut V {
self.get_mut(key).expect("no entry found for key")
}
}
impl<K, V> Clone for VecMap<K, V>
where
K: Clone,
V: Clone,
{
#[inline]
fn clone(&self) -> Self {
VecMap {
keys: self.keys.clone(),
values: self.values.clone(),
}
}
#[inline]
fn clone_from(&mut self, source: &Self) {
self.keys.clone_from(&source.keys);
self.values.clone_from(&source.values);
}
}
impl<K, V> fmt::Debug for VecMap<K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
impl<K, V> Default for VecMap<K, V> {
#[inline]
fn default() -> Self {
Self::with_capacity(0)
}
}
impl<K, V> PartialEq<VecMap<K, V>> for VecMap<K, V>
where
K: PartialEq,
V: PartialEq,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
self.keys.eq(&other.keys) && self.values.eq(&other.values)
}
}
impl<K, V> Eq for VecMap<K, V>
where
K: Eq,
V: Eq,
{
}
pub struct Keys<'a, K, V> {
iter: SliceIter<'a, K>,
_phantom: PhantomData<V>,
}
impl<'a, K, V> Clone for Keys<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
_phantom: PhantomData,
}
}
}
impl<'a, K, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
pub struct Values<'a, K, V> {
iter: SliceIter<'a, V>,
_phantom: PhantomData<K>,
}
impl<'a, K, V> Clone for Values<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
_phantom: PhantomData,
}
}
}
impl<K, V> fmt::Debug for Values<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K, V> FusedIterator for Values<'a, K, V> {}
pub struct ValuesMut<'a, K, V> {
iter: SliceIterMut<'a, V>,
_phantom: PhantomData<K>,
}
impl<K, V> fmt::Debug for ValuesMut<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for ValuesMut<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, K, V> ExactSizeIterator for ValuesMut<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K, V> FusedIterator for ValuesMut<'a, K, V> {}
pub struct Iter<'a, K, V> {
iter: Zip<SliceIter<'a, K>, SliceIter<'a, V>>,
}
impl<K, V> fmt::Debug for Iter<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K, V> FusedIterator for Iter<'a, K, V> {}
pub struct IterMut<'a, K, V> {
iter: Zip<SliceIter<'a, K>, SliceIterMut<'a, V>>,
}
impl<K, V> fmt::Debug for IterMut<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
pub struct IntoIter<K, V> {
iter: Zip<VecIntoIter<K>, VecIntoIter<V>>,
}
impl<K, V> fmt::Debug for IntoIter<K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<K, V> FusedIterator for IntoIter<K, V> {}
pub struct Drain<'a, K, V> {
iter: Zip<VecDrain<'a, K>, VecDrain<'a, V>>,
}
impl<K, V> fmt::Debug for Drain<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a, K, V> Iterator for Drain<'a, K, V> {
type Item = (K, V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for Drain<'a, K, V> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K, V> FusedIterator for Drain<'a, K, V> {}
pub enum Entry<'a, K, V> {
Occupied(OccupiedEntry<'a, K, V>),
Vacant(VacantEntry<'a, K, V>),
}
impl<'a, K: 'a, V: 'a> Entry<'a, K, V> {
#[inline]
pub fn key(&self) -> &K {
match self {
Entry::Occupied(entry) => &entry.map.keys[entry.index],
Entry::Vacant(entry) => entry.key(),
}
}
#[inline]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Entry::Occupied(mut entry) => {
f(entry.get_mut());
Entry::Occupied(entry)
}
Entry::Vacant(entry) => Entry::Vacant(entry),
}
}
}
impl<'a, K: 'a, V: 'a> Entry<'a, K, V>
where
K: Eq,
{
#[inline]
pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V> {
match self {
Entry::Occupied(mut entry) => {
entry.insert(value);
entry
}
Entry::Vacant(entry) => {
entry.map.insert(entry.key, value);
OccupiedEntry {
index: entry.map.values.len() - 1,
map: entry.map,
}
}
}
}
#[inline]
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default),
}
}
}
impl<'a, K: 'a, V: 'a> Entry<'a, K, V>
where
K: Eq,
V: Default,
{
#[inline]
pub fn or_default(self) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(Default::default()),
}
}
}
impl<K, V> fmt::Debug for Entry<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Entry::Occupied(ref e) => f.debug_tuple("Entry").field(e).finish(),
Entry::Vacant(ref e) => f.debug_tuple("Entry").field(e).finish(),
}
}
}
pub struct OccupiedEntry<'a, K, V> {
index: usize,
map: &'a mut VecMap<K, V>,
}
impl<'a, K: 'a, V: 'a> OccupiedEntry<'a, K, V> {
#[inline]
pub fn key(&self) -> &K {
&self.map.keys[self.index]
}
#[inline]
pub fn get(&self) -> &V {
&self.map.values[self.index]
}
#[inline]
pub fn get_mut(&mut self) -> &mut V {
&mut self.map.values[self.index]
}
#[inline]
pub fn into_mut(self) -> &'a mut V {
&mut self.map.values[self.index]
}
#[inline]
pub fn insert(&mut self, mut value: V) -> V {
core::mem::swap(&mut value, &mut self.map.values[self.index]);
value
}
}
impl<'a, K: 'a, V: 'a> OccupiedEntry<'a, K, V>
where
K: PartialEq,
{
#[inline]
pub fn remove(self) -> V {
self.map.keys.swap_remove(self.index);
self.map.values.swap_remove(self.index)
}
#[inline]
pub fn remove_entry(self) -> (K, V) {
(
self.map.keys.swap_remove(self.index),
self.map.values.swap_remove(self.index),
)
}
}
impl<K, V> fmt::Debug for OccupiedEntry<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntry")
.field("index", &self.index)
.field("value", &self.map.values[self.index])
.finish()
}
}
pub struct VacantEntry<'a, K, V> {
key: K,
map: &'a mut VecMap<K, V>,
}
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn into_key(self) -> K {
self.key
}
}
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V>
where
K: Eq,
{
#[inline]
pub fn insert(self, value: V) -> &'a mut V {
self.map.insert(self.key, value);
let index = self.map.values.len() - 1;
self.map
.values
.get_mut(index)
.expect("no entry found for key")
}
}
impl<K, V> fmt::Debug for VacantEntry<'_, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VacantEntry")
.field("key", &self.key)
.finish()
}
}
pub trait VecMapExt<K, V> {
fn cleared(self) -> Self;
fn inserted(self, k: K, v: V) -> Self
where
K: Eq;
fn removed<Q>(self, k: &Q) -> Self
where
K: Eq + Borrow<Q>,
Q: Eq + PartialEq<K>;
fn retained<F>(self, f: F) -> Self
where
K: Eq,
F: FnMut(&K, &mut V) -> bool;
fn shrinked_to_fit(self) -> Self
where
K: Eq;
}
impl<K, V> VecMapExt<K, V> for VecMap<K, V> {
fn cleared(mut self) -> Self {
self.clear();
self
}
fn inserted(mut self, k: K, v: V) -> Self
where
K: Eq,
{
let _ = self.insert(k, v);
self
}
fn removed<Q>(mut self, k: &Q) -> Self
where
K: Eq + Borrow<Q>,
Q: Eq + PartialEq<K>,
{
let _ = self.remove(k);
self
}
fn retained<F>(mut self, f: F) -> Self
where
K: Eq,
F: FnMut(&K, &mut V) -> bool,
{
self.retain(f);
self
}
fn shrinked_to_fit(mut self) -> Self
where
K: Eq,
{
self.shrink_to_fit();
self
}
}