use std::ops::{Index, IndexMut, RangeBounds};
use std::slice::{Iter, IterMut, SliceIndex};
use std::vec::Drain;
use delegate::delegate;
use std::fmt::Debug;
use super::List;
#[derive(Default)]
pub struct UniqueItemList<T>
where T: PartialEq
{
contents: List<T>
}
impl<T> UniqueItemList<T>
where T: PartialEq
{
pub fn new() -> Self
{
Self
{
contents: List::new()
}
}
pub fn with_capacity(capacity: usize) -> Self
{
Self
{
contents: List::with_capacity(capacity)
}
}
pub fn add(&mut self, value: T) -> bool
{
if !self.contents.contains(&value)
{
self.contents.add(value);
return true;
}
false
}
pub fn add_copy(&mut self, value: &T) -> bool
where T: Copy
{
if !self.contents.contains(value)
{
self.contents.add(*value);
return true;
}
false
}
pub fn add_clone(&mut self, value: &T) -> bool
where T: Clone
{
if !self.contents.contains(value)
{
self.contents.add(value.clone());
return true;
}
false
}
delegate! {
to self.contents {
pub fn add_or_repace(&mut self, value: T);
pub fn remove(&mut self, value: T) -> bool;
pub fn contains(&self, x: &T) -> bool;
pub fn len(&self) -> usize;
pub fn is_empty(&self) -> bool;
pub fn to_vec(&self) -> Vec<T> where T: Clone;
pub fn pop(&mut self) -> Option<T>;
pub fn insert(&mut self, index: usize, element: T);
pub fn remove_at(&mut self, index: usize) -> T;
pub fn first(&self) -> Option<&T>;
pub fn last(&self) -> Option<&T>;
pub fn iter(&self) -> Iter<'_, T>;
pub fn get_last_index(&self) -> Option<usize>;
pub fn index_of(&self, value: &T) -> Option<usize>;
pub fn reverse(&mut self);
pub fn get<I>(&self, index: I) -> Option<&<I as SliceIndex<[T]>>::Output> where I: SliceIndex<[T]>;
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T> where R: RangeBounds<usize>;
pub fn drain_all(&mut self) -> Drain<'_, T>;
}
}
}
impl<T> UniqueItemList<T> where
T: Copy + PartialEq
{
delegate! {
to self.contents {
pub fn add_or_repace_copy(&mut self, value: T) -> Option<T>;
}
}
}
impl<T> UniqueItemList<T> where
T: Clone + PartialEq
{
delegate! {
to self.contents {
pub fn add_or_repace_clone(&mut self, value: T) -> Option<T>;
}
}
}
impl<T> Index<usize> for UniqueItemList<T> where
T: PartialEq
{
type Output = T;
delegate! {
to self.contents {
fn index(&self, index: usize) -> &Self::Output;
}
}
}
impl<T> IndexMut<usize> for UniqueItemList<T> where
T: PartialEq
{
delegate! {
to self.contents {
fn index_mut(&mut self, index: usize) -> &mut Self::Output;
}
}
}
impl<T> Clone for UniqueItemList<T> where
T: PartialEq + Clone
{
fn clone(&self) -> Self
{
Self
{
contents: self.contents.clone()
}
}
}
impl<T> Debug for UniqueItemList<T>
where T: PartialEq + Debug
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UniqueItemList").field("contents", &self.contents).finish()
}
}