use crate::{Key, KeyValue, Value};
use indexmap::map::{
Drain, Entry, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys, Values, ValuesMut,
};
use indexmap::{Equivalent, IndexMap};
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hash};
use std::iter::FromIterator;
use std::ops::{Index, IndexMut, RangeBounds};
#[derive(Clone, Debug)]
pub struct OrderMap<K, V, S = RandomState>(IndexMap<K, V, S>);
impl<K, V> OrderMap<K, V> {
#[inline]
pub fn new() -> Self {
Self(IndexMap::new())
}
#[inline]
pub fn with_capacity(n: usize) -> Self {
Self(IndexMap::with_capacity(n))
}
}
impl<K, V, S> OrderMap<K, V, S> {
#[inline]
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self {
Self(IndexMap::with_capacity_and_hasher(n, hash_builder))
}
pub const fn with_hasher(hash_builder: S) -> Self {
Self(IndexMap::with_hasher(hash_builder))
}
pub fn capacity(&self) -> usize {
self.0.capacity()
}
pub fn hasher(&self) -> &S {
self.0.hasher()
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn iter(&self) -> Iter<'_, K, V> {
self.0.iter()
}
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
self.0.iter_mut()
}
pub fn keys(&self) -> Keys<'_, K, V> {
self.0.keys()
}
pub fn into_keys(self) -> IntoKeys<K, V> {
self.0.into_keys()
}
pub fn values(&self) -> Values<'_, K, V> {
self.0.values()
}
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
self.0.values_mut()
}
pub fn into_values(self) -> IntoValues<K, V> {
self.0.into_values()
}
pub fn clear(&mut self) {
self.0.clear();
}
pub fn truncate(&mut self, len: usize) {
self.0.truncate(len);
}
pub fn drain<R>(&mut self, range: R) -> Drain<'_, K, V>
where
R: RangeBounds<usize>,
{
self.0.drain(range)
}
pub fn split_off(&mut self, at: usize) -> Self
where
S: Clone,
{
Self(self.0.split_off(at))
}
}
impl<K, V, S> OrderMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
pub fn reserve(&mut self, additional: usize) {
self.0.reserve(additional)
}
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit()
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.0.insert(key, value)
}
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) {
self.0.insert_full(key, value)
}
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
self.0.entry(key)
}
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
where
Q: Hash + Equivalent<K>,
{
self.0.contains_key(key)
}
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V>
where
Q: Hash + Equivalent<K>,
{
self.0.get(key)
}
pub fn get_key_value<Q: ?Sized>(&self, key: &Q) -> Option<(&K, &V)>
where
Q: Hash + Equivalent<K>,
{
self.0.get_key_value(key)
}
pub fn get_full<Q: ?Sized>(&self, key: &Q) -> Option<(usize, &K, &V)>
where
Q: Hash + Equivalent<K>,
{
self.0.get_full(key)
}
pub fn get_index_of<Q: ?Sized>(&self, key: &Q) -> Option<usize>
where
Q: Hash + Equivalent<K>,
{
self.0.get_index_of(key)
}
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
where
Q: Hash + Equivalent<K>,
{
self.0.get_mut(key)
}
pub fn get_full_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)>
where
Q: Hash + Equivalent<K>,
{
self.0.get_full_mut(key)
}
pub fn shift_remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where
Q: Hash + Equivalent<K>,
{
self.0.shift_remove(key)
}
pub fn shift_remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
Q: Hash + Equivalent<K>,
{
self.0.shift_remove_entry(key)
}
pub fn shift_remove_full<Q: ?Sized>(&mut self, key: &Q) -> Option<(usize, K, V)>
where
Q: Hash + Equivalent<K>,
{
self.0.shift_remove_full(key)
}
pub fn pop(&mut self) -> Option<(K, V)> {
self.0.pop()
}
pub fn retain<F>(&mut self, keep: F)
where
F: FnMut(&K, &mut V) -> bool,
{
self.0.retain(keep);
}
}
impl<K, V, S> OrderMap<K, V, S> {
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
self.0.get_index(index)
}
pub fn get_index_mut(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
self.0.get_index_mut(index)
}
pub fn first(&self) -> Option<(&K, &V)> {
self.0.first()
}
pub fn first_mut(&mut self) -> Option<(&K, &mut V)> {
self.0.first_mut()
}
pub fn last(&self) -> Option<(&K, &V)> {
self.0.last()
}
pub fn last_mut(&mut self) -> Option<(&K, &mut V)> {
self.0.last_mut()
}
pub fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> {
self.0.shift_remove_index(index)
}
}
impl<'a, K, V, S> IntoIterator for &'a OrderMap<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<'a, K, V, S> IntoIterator for &'a mut OrderMap<K, V, S> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter_mut()
}
}
impl<K, V, S> IntoIterator for OrderMap<K, V, S> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<K, V, Q: ?Sized, S> Index<&Q> for OrderMap<K, V, S>
where
Q: Hash + Equivalent<K>,
K: Hash + Eq,
S: BuildHasher,
{
type Output = V;
fn index(&self, key: &Q) -> &V {
self.0.index(key)
}
}
impl<K, V, Q: ?Sized, S> IndexMut<&Q> for OrderMap<K, V, S>
where
Q: Hash + Equivalent<K>,
K: Hash + Eq,
S: BuildHasher,
{
fn index_mut(&mut self, key: &Q) -> &mut V {
self.0.index_mut(key)
}
}
impl<K, V, S> Index<usize> for OrderMap<K, V, S> {
type Output = V;
fn index(&self, index: usize) -> &V {
self.0.index(index)
}
}
impl<K, V, S> IndexMut<usize> for OrderMap<K, V, S> {
fn index_mut(&mut self, index: usize) -> &mut V {
self.0.index_mut(index)
}
}
impl<K, V, S> FromIterator<(K, V)> for OrderMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher + Default,
{
fn from_iter<I: IntoIterator<Item = (K, V)>>(iterable: I) -> Self {
Self(IndexMap::from_iter(iterable))
}
}
impl<K, V, S> Extend<(K, V)> for OrderMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iterable: I) {
self.0.extend(iterable)
}
}
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for OrderMap<K, V, S>
where
K: 'a + Hash + Eq + Copy,
V: 'a + Copy,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iterable: I) {
self.0.extend(iterable)
}
}
impl<K, V, S> Default for OrderMap<K, V, S>
where
S: Default,
{
fn default() -> Self {
Self(IndexMap::default())
}
}
impl<K, V1, S1, V2, S2> PartialEq<OrderMap<K, V2, S2>> for OrderMap<K, V1, S1>
where
K: Hash + Eq,
V1: PartialEq<V2>,
S1: BuildHasher,
S2: BuildHasher,
{
fn eq(&self, other: &OrderMap<K, V2, S2>) -> bool {
self.0.eq(&other.0)
}
}
impl<K, V, S> Eq for OrderMap<K, V, S>
where
K: Eq + Hash,
V: Eq,
S: BuildHasher,
{
}
impl<S> FromIterator<KeyValue> for OrderMap<Key, Value, S>
where
S: BuildHasher + Default,
{
fn from_iter<I: IntoIterator<Item = KeyValue>>(iterable: I) -> Self {
Self(IndexMap::from_iter(
iterable.into_iter().map(|kv| (kv.key, kv.value)),
))
}
}
impl<S> Extend<KeyValue> for OrderMap<Key, Value, S>
where
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = KeyValue>>(&mut self, iterable: I) {
self.0
.extend(iterable.into_iter().map(|kv| (kv.key, kv.value)))
}
}