#![deny(missing_docs)]
#[cfg(feature = "serde_impl")]
pub mod serde;
pub mod set;
use std::borrow::Borrow;
use std::fmt::{self, Debug};
use std::iter;
use std::mem;
use std::ops;
use std::slice;
use std::vec;
use self::Entry::{Occupied, Vacant};
pub struct LinearMap<K, V> {
storage: Vec<(K, V)>,
}
impl<K: Eq, V> LinearMap<K, V> {
pub fn new() -> Self {
LinearMap { storage: vec![] }
}
pub fn with_capacity(capacity: usize) -> Self {
LinearMap { storage: Vec::with_capacity(capacity) }
}
pub fn capacity(&self) -> usize {
self.storage.capacity()
}
pub fn reserve(&mut self, additional: usize) {
self.storage.reserve(additional);
}
pub fn reserve_exact(&mut self, additional: usize) {
self.storage.reserve_exact(additional);
}
pub fn shrink_to_fit(&mut self) {
self.storage.shrink_to_fit();
}
pub fn len(&self) -> usize {
self.storage.len()
}
pub fn is_empty(&self) -> bool {
self.storage.is_empty()
}
pub fn clear(&mut self) {
self.storage.clear();
}
pub fn retain<F>(&mut self, mut keep_fn: F)
where F: FnMut(&K, &mut V) -> bool {
let mut del = 0;
{
let v = &mut *self.storage;
for i in 0..v.len() {
if !keep_fn(&v[i].0, &mut v[i].1) {
del += 1;
} else if del > 0 {
v.swap(i - del, i);
}
}
}
if del > 0 {
let len = self.storage.len();
self.storage.truncate(len - del);
}
}
pub fn drain(&mut self) -> Drain<K, V> {
Drain { iter: self.storage.drain(..) }
}
pub fn iter(&self) -> Iter<K, V> {
Iter { iter: self.storage.iter() }
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut { iter: self.storage.iter_mut() }
}
pub fn keys(&self) -> Keys<K, V> {
Keys { iter: self.iter() }
}
pub fn values(&self) -> Values<K, V> {
Values { iter: self.iter() }
}
pub fn get<Q: ?Sized + Eq>(&self, key: &Q) -> Option<&V> where K: Borrow<Q> {
for (k, v) in self {
if key == k.borrow() {
return Some(v);
}
}
None
}
pub fn get_mut<Q: ?Sized + Eq>(&mut self, key: &Q) -> Option<&mut V> where K: Borrow<Q> {
for (k, v) in self {
if key == k.borrow() {
return Some(v);
}
}
None
}
pub fn contains_key<Q: ?Sized + Eq>(&self, key: &Q) -> bool where K: Borrow<Q> {
self.get(key).is_some()
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
match self.entry(key) {
Occupied(mut e) => Some(e.insert(value)),
Vacant(e) => { e.insert(value); None }
}
}
pub fn remove<Q: ?Sized + Eq>(&mut self, key: &Q) -> Option<V> where K: Borrow<Q> {
for i in 0..self.storage.len() {
if self.storage[i].0.borrow() == key {
return Some(self.storage.swap_remove(i).1);
}
}
None
}
pub fn entry(&mut self, key: K) -> Entry<K, V> {
match self.storage.iter().position(|&(ref k, _)| key == *k) {
None => Vacant(VacantEntry {
map: self,
key: key
}),
Some(index) => Occupied(OccupiedEntry {
map: self,
index: index
})
}
}
}
impl<K: Clone, V: Clone> Clone for LinearMap<K, V> {
fn clone(&self) -> Self {
LinearMap { storage: self.storage.clone() }
}
fn clone_from(&mut self, other: &Self) {
self.storage.clone_from(&other.storage);
}
}
impl<K: Eq + Debug, V: Debug> Debug for LinearMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self).finish()
}
}
impl<K: Eq, V> Default for LinearMap<K, V> {
fn default() -> Self {
Self::new()
}
}
impl<K: Eq, V> Extend<(K, V)> for LinearMap<K, V> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, key_values: I) {
for (key, value) in key_values { self.insert(key, value); }
}
}
impl<K: Eq, V> iter::FromIterator<(K, V)> for LinearMap<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(key_values: I) -> Self {
let mut map = Self::new();
map.extend(key_values);
map
}
}
impl<'a, K: Eq + Borrow<Q>, V, Q: ?Sized + Eq> ops::Index<&'a Q> for LinearMap<K, V> {
type Output = V;
fn index(&self, key: &'a Q) -> &V {
self.get(key).expect("key not found")
}
}
impl<K: Eq, V: PartialEq> PartialEq for LinearMap<K, V> {
fn eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}
for (key, value) in self {
if other.get(key) != Some(value) {
return false;
}
}
true
}
}
impl<K: Eq, V: Eq> Eq for LinearMap<K, V> {}
impl<K: Eq, V> Into<Vec<(K, V)>> for LinearMap<K, V> {
fn into(self) -> Vec<(K, V)> {
self.storage
}
}
#[macro_export]
macro_rules! linear_map {
($($key:expr => $value:expr,)+) => { linear_map!($($key => $value),+) };
($($key:expr => $value:expr),*) => {
{
let _cap = <[&str]>::len(&[$(stringify!($key)),*]);
let mut _map = $crate::LinearMap::with_capacity(_cap);
$(
_map.insert($key, $value);
)*
_map
}
};
}
pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
map: &'a mut LinearMap<K, V>,
index: usize,
}
pub struct VacantEntry<'a, K: 'a, V: 'a> {
map: &'a mut LinearMap<K, V>,
key: K,
}
pub enum Entry<'a, K: 'a, V: 'a> {
Occupied(OccupiedEntry<'a, K, V>),
Vacant(VacantEntry<'a, K, V>)
}
impl<'a, K, V> Entry<'a, K, V> {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default)
}
}
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default())
}
}
}
impl<'a, K, V> OccupiedEntry<'a, K, V> {
pub fn get(&self) -> &V {
&self.map.storage[self.index].1
}
pub fn get_mut(&mut self) -> &mut V {
&mut self.map.storage[self.index].1
}
pub fn into_mut(self) -> &'a mut V {
&mut self.map.storage[self.index].1
}
pub fn insert(&mut self, value: V) -> V {
mem::replace(self.get_mut(), value)
}
pub fn remove(self) -> V {
self.map.storage.swap_remove(self.index).1
}
}
impl<'a, K, V> VacantEntry<'a, K, V> {
pub fn insert(self, value: V) -> &'a mut V {
self.map.storage.push((self.key, value));
&mut self.map.storage.last_mut().unwrap().1
}
}
pub struct IntoIter<K, V> {
iter: vec::IntoIter<(K, V)>,
}
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
fn next_back(&mut self) -> Option<(K, V)> {
self.iter.next_back()
}
}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
fn len(&self) -> usize {
self.iter.len()
}
}
pub struct Drain<'a, K: 'a, V: 'a> {
iter: vec::Drain<'a, (K, V)>,
}
pub struct Iter<'a, K: 'a, V: 'a> {
iter: slice::Iter<'a, (K, V)>,
}
pub struct IterMut<'a, K: 'a, V: 'a> {
iter: slice::IterMut<'a, (K, V)>,
}
pub struct Keys<'a, K: 'a, V: 'a> {
iter: Iter<'a, K, V>,
}
pub struct Values<'a, K: 'a, V: 'a> {
iter: Iter<'a, K, V>,
}
macro_rules! impl_iter {($typ:ty, $item:ty, $map:expr) => {
impl<'a, K, V> Iterator for $typ {
type Item = $item;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map($map)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a, K, V> DoubleEndedIterator for $typ {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map($map)
}
}
impl<'a, K, V> ExactSizeIterator for $typ {
fn len(&self) -> usize {
self.iter.len()
}
}
}}
impl_iter!{Drain<'a,K,V>, (K,V), |e| e }
impl_iter!{Iter<'a,K,V>, (&'a K, &'a V), |e| (&e.0, &e.1) }
impl_iter!{IterMut<'a,K,V>, (&'a K, &'a mut V), |e| (&e.0, &mut e.1) }
impl_iter!{Keys<'a,K,V>, &'a K, |e| e.0 }
impl_iter!{Values<'a,K,V>, &'a V, |e| e.1 }
impl<'a, K, V> Clone for Iter<'a, K, V> {
fn clone(&self) -> Self {
Iter { iter: self.iter.clone() }
}
}
impl<'a, K, V> Clone for Keys<'a, K, V> {
fn clone(&self) -> Self {
Keys { iter: self.iter.clone() }
}
}
impl<'a, K, V> Clone for Values<'a, K, V> {
fn clone(&self) -> Self {
Values { iter: self.iter.clone() }
}
}
impl<K: Eq, V> IntoIterator for LinearMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> IntoIter<K, V> {
IntoIter { iter: self.storage.into_iter() }
}
}
impl<'a, K: Eq, V> IntoIterator for &'a LinearMap<K, V> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Iter<'a, K, V> {
self.iter()
}
}
impl<'a, K: Eq, V> IntoIterator for &'a mut LinearMap<K, V> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> IterMut<'a, K, V> {
self.iter_mut()
}
}
#[allow(dead_code)]
fn assert_covariance() {
fn a<'a, K, V>(x: LinearMap<&'static K, &'static V>) -> LinearMap<&'a K, &'a V> { x }
fn b<'a, K, V>(x: IntoIter<&'static K, &'static V>) -> IntoIter<&'a K, &'a V> { x }
fn c<'i, 'a, K, V>(x: Iter<'i, &'static K, &'static V>) -> Iter<'i, &'a K, &'a V> { x }
fn d<'i, 'a, K, V>(x: Keys<'i, &'static K, &'static V>) -> Keys<'i, &'a K, &'a V> { x }
fn e<'i, 'a, K, V>(x: Values<'i, &'static K, &'static V>) -> Values<'i, &'a K, &'a V> { x }
}