mod impls;
use super::TryReserveError;
use alloc::vec::{self, Vec};
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::mem;
use core::ops::RangeBounds;
use core::slice;
pub(crate) trait Keyed<K>
where
K: ?Sized,
{
fn key(&self) -> &K;
}
impl<K> Keyed<K> for K {
fn key(&self) -> &K {
self
}
}
#[derive(Clone, Debug)]
pub(crate) struct KeyedVecSet<K, V> {
base: Vec<V>,
_marker: core::marker::PhantomData<K>,
}
impl<K, V> KeyedVecSet<K, V> {
pub const fn new() -> Self {
KeyedVecSet {
base: Vec::new(),
_marker: core::marker::PhantomData,
}
}
pub fn with_capacity(capacity: usize) -> Self {
KeyedVecSet {
base: Vec::with_capacity(capacity),
_marker: core::marker::PhantomData,
}
}
pub fn capacity(&self) -> usize {
self.base.capacity()
}
pub fn len(&self) -> usize {
self.base.len()
}
pub fn is_empty(&self) -> bool {
self.base.is_empty()
}
pub fn clear(&mut self) {
self.base.clear();
}
pub fn truncate(&mut self, len: usize) {
self.base.truncate(len);
}
pub fn reverse(&mut self) {
self.base.reverse();
}
pub fn reserve(&mut self, additional: usize) {
self.base.reserve(additional);
}
pub fn reserve_exact(&mut self, additional: usize) {
self.base.reserve_exact(additional);
}
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.base.try_reserve(additional)
}
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.base.try_reserve_exact(additional)
}
pub fn shrink_to_fit(&mut self) {
self.base.shrink_to_fit();
}
pub fn shrink_to(&mut self, min_capacity: usize) {
self.base.shrink_to(min_capacity);
}
pub fn split_off(&mut self, at: usize) -> KeyedVecSet<K, V> {
KeyedVecSet {
base: self.base.split_off(at),
_marker: core::marker::PhantomData,
}
}
pub fn drain<R>(&mut self, range: R) -> vec::Drain<'_, V>
where
R: RangeBounds<usize>,
{
self.base.drain(range)
}
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&V) -> bool,
{
self.base.retain(f);
}
pub fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut V) -> bool,
{
self.base.retain_mut(f);
}
pub fn sort_by<F>(&mut self, compare: F)
where
F: FnMut(&V, &V) -> Ordering,
{
self.base.sort_by(compare);
}
pub fn sort_unstable_by<F>(&mut self, compare: F)
where
F: FnMut(&V, &V) -> Ordering,
{
self.base.sort_unstable_by(compare);
}
pub fn sort_by_cached_key<T, F>(&mut self, sort_key: F)
where
T: Ord,
F: FnMut(&V) -> T,
{
self.base.sort_by_cached_key(sort_key);
}
pub fn swap_indices(&mut self, a: usize, b: usize) {
self.base.swap(a, b);
}
pub fn as_slice(&self) -> &[V] {
self.base.as_slice()
}
pub fn as_mut_slice(&mut self) -> &mut [V] {
self.base.as_mut_slice()
}
pub fn to_vec(&self) -> Vec<V>
where
V: Clone,
{
self.base.clone()
}
pub fn into_vec(self) -> Vec<V> {
self.base
}
pub unsafe fn from_vec_unchecked(base: Vec<V>) -> Self {
KeyedVecSet {
base,
_marker: core::marker::PhantomData,
}
}
}
impl<K, V> KeyedVecSet<K, V>
where
V: Keyed<K>,
{
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
if self.base.is_empty() {
return None;
}
self.base.iter().position(|elem| elem.key().borrow() == key)
}
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
self.get_index_of(key).is_some()
}
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
self.get_index_of(key).map(|index| &self.base[index])
}
pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &V)>
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
self.get_index_of(key)
.map(|index| (index, &self.base[index]))
}
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
self.get_index_of(key).map(|index| &mut self.base[index])
}
pub fn get_index(&self, index: usize) -> Option<&V> {
self.base.get(index)
}
pub fn get_index_mut(&mut self, index: usize) -> Option<&mut V> {
self.base.get_mut(index)
}
pub fn first(&self) -> Option<&V> {
self.base.first()
}
pub fn first_mut(&mut self) -> Option<&mut V> {
self.base.first_mut()
}
pub fn last(&self) -> Option<&V> {
self.base.last()
}
pub fn last_mut(&mut self) -> Option<&mut V> {
self.base.last_mut()
}
}
impl<K, V> KeyedVecSet<K, V> {
pub fn pop(&mut self) -> Option<V> {
self.base.pop()
}
pub fn remove_index(&mut self, index: usize) -> V {
self.base.remove(index)
}
pub fn swap_remove_index(&mut self, index: usize) -> V {
self.base.swap_remove(index)
}
}
impl<K, V> KeyedVecSet<K, V>
where
V: Keyed<K>,
{
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
self.get_index_of(key).map(|index| self.remove_index(index))
}
pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: Eq + ?Sized,
{
self.get_index_of(key)
.map(|index| self.swap_remove_index(index))
}
}
impl<K, V> KeyedVecSet<K, V> {
pub(crate) fn push(&mut self, value: V) {
self.base.push(value);
}
}
impl<K, V> KeyedVecSet<K, V>
where
V: Keyed<K>,
K: Eq,
{
pub fn insert(&mut self, value: V) -> Option<V> {
self.insert_full(value).1
}
pub fn insert_full(&mut self, value: V) -> (usize, Option<V>) {
let key = value.key();
if let Some(index) = self.get_index_of(key) {
let old = mem::replace(&mut self.base[index], value);
(index, Some(old))
} else {
let index = self.base.len();
self.base.push(value);
(index, None)
}
}
pub fn insert_at(&mut self, index: usize, value: V) -> Option<(usize, V)> {
let key = value.key();
if let Some(old_index) = self.get_index_of(key) {
let old_value = if old_index == index {
mem::replace(&mut self.base[index], value)
} else {
let old_value = self.remove_index(old_index);
self.base.insert(index, value);
old_value
};
Some((old_index, old_value))
} else {
self.base.insert(index, value);
None
}
}
pub fn append(&mut self, other: &mut KeyedVecSet<K, V>) {
self.reserve(other.len());
for value in other.drain(..) {
self.insert(value);
}
}
}
impl<K, V> KeyedVecSet<K, V> {
pub fn iter(&self) -> slice::Iter<'_, V> {
self.base.iter()
}
}