mod entry;
mod iter;
mod mutable;
mod slice;
pub mod raw_entry_v1;
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub mod serde_seq;
#[cfg(test)]
mod tests;
pub use self::entry::{Entry, IndexedEntry, OccupiedEntry, VacantEntry};
pub use self::mutable::MutableEntryKey;
pub use self::mutable::MutableKeys;
pub use self::raw_entry_v1::RawEntryApiV1;
pub use indexmap::map::{
Drain, ExtractIf, IntoIter, IntoKeys, IntoValues, Iter, IterMut, IterMut2, Keys, Slice, Splice,
Values, ValuesMut,
};
#[cfg(feature = "rayon")]
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))]
pub mod rayon;
use alloc::boxed::Box;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{BuildHasher, Hash, Hasher};
use core::ops::{Index, IndexMut, RangeBounds};
use indexmap::IndexMap;
#[cfg(doc)]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::collections::hash_map::RandomState;
use crate::{Equivalent, GetDisjointMutError, TryReserveError};
#[cfg(feature = "std")]
pub struct OrderMap<K, V, S = RandomState> {
pub(crate) inner: IndexMap<K, V, S>,
}
#[cfg(not(feature = "std"))]
pub struct OrderMap<K, V, S> {
pub(crate) inner: IndexMap<K, V, S>,
}
impl<K, V, S> Clone for OrderMap<K, V, S>
where
K: Clone,
V: Clone,
S: Clone,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
fn clone_from(&mut self, other: &Self) {
self.inner.clone_from(&other.inner);
}
}
impl<K, V, S> fmt::Debug for OrderMap<K, V, S>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<K, V> OrderMap<K, V> {
#[inline]
pub fn new() -> Self {
Self {
inner: IndexMap::new(),
}
}
#[inline]
pub fn with_capacity(n: usize) -> Self {
Self {
inner: 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 {
inner: IndexMap::with_capacity_and_hasher(n, hash_builder),
}
}
pub const fn with_hasher(hash_builder: S) -> Self {
Self {
inner: IndexMap::with_hasher(hash_builder),
}
}
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
pub fn hasher(&self) -> &S {
self.inner.hasher()
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn iter(&self) -> Iter<'_, K, V> {
self.inner.iter()
}
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
self.inner.iter_mut()
}
pub fn keys(&self) -> Keys<'_, K, V> {
self.inner.keys()
}
pub fn into_keys(self) -> IntoKeys<K, V> {
self.inner.into_keys()
}
pub fn values(&self) -> Values<'_, K, V> {
self.inner.values()
}
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
self.inner.values_mut()
}
pub fn into_values(self) -> IntoValues<K, V> {
self.inner.into_values()
}
pub fn clear(&mut self) {
self.inner.clear();
}
pub fn truncate(&mut self, len: usize) {
self.inner.truncate(len);
}
#[track_caller]
pub fn drain<R>(&mut self, range: R) -> Drain<'_, K, V>
where
R: RangeBounds<usize>,
{
self.inner.drain(range)
}
#[track_caller]
pub fn extract_if<F, R>(&mut self, range: R, pred: F) -> ExtractIf<'_, K, V, F>
where
F: FnMut(&K, &mut V) -> bool,
R: RangeBounds<usize>,
{
self.inner.extract_if(range, pred)
}
#[track_caller]
pub fn split_off(&mut self, at: usize) -> Self
where
S: Clone,
{
Self {
inner: self.inner.split_off(at),
}
}
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional);
}
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional);
}
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve(additional)
}
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve_exact(additional)
}
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to(0);
}
pub fn shrink_to(&mut self, min_capacity: usize) {
self.inner.shrink_to(min_capacity);
}
}
impl<K, V, S> OrderMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.inner.insert(key, value)
}
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) {
self.inner.insert_full(key, value)
}
pub fn insert_sorted(&mut self, key: K, value: V) -> (usize, Option<V>)
where
K: Ord,
{
self.inner.insert_sorted(key, value)
}
pub fn insert_sorted_by<F>(&mut self, key: K, value: V, cmp: F) -> (usize, Option<V>)
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
self.inner.insert_sorted_by(key, value, cmp)
}
pub fn insert_sorted_by_key<B, F>(
&mut self,
key: K,
value: V,
sort_key: F,
) -> (usize, Option<V>)
where
B: Ord,
F: FnMut(&K, &V) -> B,
{
self.inner.insert_sorted_by_key(key, value, sort_key)
}
#[track_caller]
pub fn insert_before(&mut self, index: usize, key: K, value: V) -> (usize, Option<V>) {
self.inner.insert_before(index, key, value)
}
#[track_caller]
pub fn shift_insert(&mut self, index: usize, key: K, value: V) -> Option<V> {
self.inner.shift_insert(index, key, value)
}
#[track_caller]
pub fn replace_index(&mut self, index: usize, key: K) -> Result<K, (usize, K)> {
self.inner.replace_index(index, key)
}
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
Entry::new(self.inner.entry(key))
}
#[track_caller]
pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, K, V, S>
where
R: RangeBounds<usize>,
I: IntoIterator<Item = (K, V)>,
{
self.inner.splice(range, replace_with)
}
pub fn append<S2>(&mut self, other: &mut OrderMap<K, V, S2>) {
self.inner.append(&mut other.inner);
}
}
impl<K, V, S> OrderMap<K, V, S>
where
S: BuildHasher,
{
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.contains_key(key)
}
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get(key)
}
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_key_value(key)
}
pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_full(key)
}
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_index_of(key)
}
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_mut(key)
}
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_key_value_mut(key)
}
pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_full_mut(key)
}
pub fn get_disjoint_mut<Q, const N: usize>(&mut self, keys: [&Q; N]) -> [Option<&mut V>; N]
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.get_disjoint_mut(keys)
}
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.shift_remove(key)
}
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.shift_remove_entry(key)
}
pub fn remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.shift_remove_full(key)
}
pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.swap_remove(key)
}
pub fn swap_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.swap_remove_entry(key)
}
pub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)>
where
Q: ?Sized + Hash + Equivalent<K>,
{
self.inner.swap_remove_full(key)
}
}
impl<K, V, S> OrderMap<K, V, S> {
#[doc(alias = "pop_last")] pub fn pop(&mut self) -> Option<(K, V)> {
self.inner.pop()
}
pub fn retain<F>(&mut self, keep: F)
where
F: FnMut(&K, &mut V) -> bool,
{
self.inner.retain(keep);
}
pub fn sort_keys(&mut self)
where
K: Ord,
{
self.inner.sort_keys();
}
pub fn sort_by<F>(&mut self, cmp: F)
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
self.inner.sort_by(cmp);
}
pub fn sorted_by<F>(self, cmp: F) -> IntoIter<K, V>
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
self.inner.sorted_by(cmp)
}
pub fn sort_by_key<T, F>(&mut self, sort_key: F)
where
T: Ord,
F: FnMut(&K, &V) -> T,
{
self.inner.sort_by_key(sort_key)
}
pub fn sort_unstable_keys(&mut self)
where
K: Ord,
{
self.inner.sort_unstable_keys();
}
pub fn sort_unstable_by<F>(&mut self, cmp: F)
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
self.inner.sort_unstable_by(cmp);
}
#[inline]
pub fn sorted_unstable_by<F>(self, cmp: F) -> IntoIter<K, V>
where
F: FnMut(&K, &V, &K, &V) -> Ordering,
{
self.inner.sorted_unstable_by(cmp)
}
pub fn sort_unstable_by_key<T, F>(&mut self, sort_key: F)
where
T: Ord,
F: FnMut(&K, &V) -> T,
{
self.inner.sort_unstable_by_key(sort_key)
}
pub fn sort_by_cached_key<T, F>(&mut self, sort_key: F)
where
T: Ord,
F: FnMut(&K, &V) -> T,
{
self.inner.sort_by_cached_key(sort_key);
}
pub fn binary_search_keys(&self, x: &K) -> Result<usize, usize>
where
K: Ord,
{
self.inner.binary_search_keys(x)
}
#[inline]
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
where
F: FnMut(&'a K, &'a V) -> Ordering,
{
self.inner.binary_search_by(f)
}
#[inline]
pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize>
where
F: FnMut(&'a K, &'a V) -> B,
B: Ord,
{
self.inner.binary_search_by_key(b, f)
}
#[inline]
pub fn is_sorted(&self) -> bool
where
K: PartialOrd,
{
self.inner.is_sorted()
}
#[inline]
pub fn is_sorted_by<'a, F>(&'a self, cmp: F) -> bool
where
F: FnMut(&'a K, &'a V, &'a K, &'a V) -> bool,
{
self.inner.is_sorted_by(cmp)
}
#[inline]
pub fn is_sorted_by_key<'a, F, T>(&'a self, sort_key: F) -> bool
where
F: FnMut(&'a K, &'a V) -> T,
T: PartialOrd,
{
self.inner.is_sorted_by_key(sort_key)
}
#[must_use]
pub fn partition_point<P>(&self, pred: P) -> usize
where
P: FnMut(&K, &V) -> bool,
{
self.inner.partition_point(pred)
}
pub fn reverse(&mut self) {
self.inner.reverse()
}
pub fn as_slice(&self) -> &Slice<K, V> {
self.inner.as_slice()
}
pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
self.inner.as_mut_slice()
}
pub fn into_boxed_slice(self) -> Box<Slice<K, V>> {
self.inner.into_boxed_slice()
}
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
self.inner.get_index(index)
}
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> {
self.inner.get_index_mut(index)
}
pub fn get_index_entry(&mut self, index: usize) -> Option<IndexedEntry<'_, K, V>> {
self.inner.get_index_entry(index).map(IndexedEntry::new)
}
pub fn get_disjoint_indices_mut<const N: usize>(
&mut self,
indices: [usize; N],
) -> Result<[(&K, &mut V); N], GetDisjointMutError> {
self.as_mut_slice().get_disjoint_mut(indices)
}
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> {
self.inner.get_range(range)
}
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> {
self.inner.get_range_mut(range)
}
#[doc(alias = "first_key_value")] pub fn first(&self) -> Option<(&K, &V)> {
self.inner.first()
}
pub fn first_mut(&mut self) -> Option<(&K, &mut V)> {
self.inner.first_mut()
}
pub fn first_entry(&mut self) -> Option<IndexedEntry<'_, K, V>> {
self.inner.first_entry().map(IndexedEntry::new)
}
#[doc(alias = "last_key_value")] pub fn last(&self) -> Option<(&K, &V)> {
self.inner.last()
}
pub fn last_mut(&mut self) -> Option<(&K, &mut V)> {
self.inner.last_mut()
}
pub fn last_entry(&mut self) -> Option<IndexedEntry<'_, K, V>> {
self.inner.last_entry().map(IndexedEntry::new)
}
pub fn remove_index(&mut self, index: usize) -> Option<(K, V)> {
self.inner.shift_remove_index(index)
}
pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
self.inner.swap_remove_index(index)
}
#[track_caller]
pub fn move_index(&mut self, from: usize, to: usize) {
self.inner.move_index(from, to)
}
#[track_caller]
pub fn swap_indices(&mut self, a: usize, b: usize) {
self.inner.swap_indices(a, b)
}
}
impl<K, V, Q: ?Sized, S> Index<&Q> for OrderMap<K, V, S>
where
Q: Hash + Equivalent<K>,
S: BuildHasher,
{
type Output = V;
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
}
impl<K, V, Q: ?Sized, S> IndexMut<&Q> for OrderMap<K, V, S>
where
Q: Hash + Equivalent<K>,
S: BuildHasher,
{
fn index_mut(&mut self, key: &Q) -> &mut V {
self.get_mut(key).expect("no entry found for key")
}
}
impl<K, V, S> Index<usize> for OrderMap<K, V, S> {
type Output = V;
fn index(&self, index: usize) -> &V {
if let Some((_, value)) = self.get_index(index) {
value
} else {
panic!(
"index out of bounds: the len is {len} but the index is {index}",
len = self.len()
);
}
}
}
impl<K, V, S> IndexMut<usize> for OrderMap<K, V, S> {
fn index_mut(&mut self, index: usize) -> &mut V {
let len: usize = self.len();
if let Some((_, value)) = self.get_index_mut(index) {
value
} else {
panic!("index out of bounds: the len is {len} but the index is {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 {
inner: IndexMap::from_iter(iterable),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<K, V, const N: usize> From<[(K, V); N]> for OrderMap<K, V, RandomState>
where
K: Hash + Eq,
{
fn from(arr: [(K, V); N]) -> Self {
Self::from_iter(arr)
}
}
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.inner.extend(iterable);
}
}
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for OrderMap<K, V, S>
where
K: Hash + Eq + Copy,
V: Copy,
S: BuildHasher,
{
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iterable: I) {
self.inner.extend(iterable);
}
}
impl<K, V, S> Default for OrderMap<K, V, S>
where
S: Default,
{
fn default() -> Self {
Self::with_capacity_and_hasher(0, S::default())
}
}
impl<K, V, S1, S2> PartialEq<OrderMap<K, V, S2>> for OrderMap<K, V, S1>
where
K: PartialEq,
V: PartialEq,
{
fn eq(&self, other: &OrderMap<K, V, S2>) -> bool {
self.len() == other.len() && self.iter().eq(other)
}
}
impl<K, V, S> Eq for OrderMap<K, V, S>
where
K: Eq,
V: Eq,
{
}
impl<K, V, S1, S2> PartialOrd<OrderMap<K, V, S2>> for OrderMap<K, V, S1>
where
K: PartialOrd,
V: PartialOrd,
{
fn partial_cmp(&self, other: &OrderMap<K, V, S2>) -> Option<Ordering> {
self.iter().partial_cmp(other)
}
}
impl<K, V, S> Ord for OrderMap<K, V, S>
where
K: Ord,
V: Ord,
{
fn cmp(&self, other: &Self) -> Ordering {
self.iter().cmp(other)
}
}
impl<K, V, S> Hash for OrderMap<K, V, S>
where
K: Hash,
V: Hash,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.len().hash(state);
for (key, value) in self {
key.hash(state);
value.hash(state);
}
}
}