Struct DedupVec

Source
pub struct DedupVec<T, const ON: bool = true>
where Self: AsDedupVec,
{ /* private fields */ }
Expand description

Deduplicated vector.

  • If ON is true, then the vector keeps sorted and deduplicated status. It means the vector will conduct sorting and binary search whenever you insert or remove items into or from the vector. In this case, Vec is used as data container.

  • If ON is false, on the other hand, the vector acts differently according to build mode.

    • debug mode : Vector will panic when duplication detected. SetValueList is used as data container in this case. The container keeps insertion order.
    • release mode : Vector does nothing to keep the deduplicated status. Clients must keep the status on their code. In this case, Vec is used as data container.

§How to determine ON

If sorting is not a burden to you, and you’re going to insert/remove items in any orders, then set ON to true. The vector will keep the deduplicated status always.

If you can guarantee the deduplicated status on your own, then set ON to false. The vector will warn you if the guarantee has been broken in debug mode. In release mode, there won’t be any additional operations to avoid performance penalty.

Trait Implementations§

Source§

impl<T> AsDedupVec for DedupVec<T, true>
where T: Ord,

Source§

fn new() -> Self

Creates a new empty vector.

The vector will automatically sort and deduplicate items for you due to ON = true.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<i32, true>::new();
Source§

fn len(&self) -> usize

Returns number of items.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, true>::new();
v.push(0);
assert_eq!(v.len(), 1);
Source§

fn contains(&self, value: &Self::Item) -> bool

Returns true if the vector contains the given value.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, true>::new();
v.push(0);
assert!(v.contains(&0));
Source§

fn push(&mut self, value: Self::Item)

Appends the given value to the end of the vector.

The vector will automatically sort and deduplicate items for you due to ON = true.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, true>::new();
v.push(0);
v.push(0);
v.push(2);
v.push(1);
let dedup = v.iter().cloned().collect::<Vec<_>>();
assert_eq!(dedup, [0, 1, 2]);
Source§

fn remove(&mut self, value: &Self::Item) -> Option<Self::Item>

Removes an item that is equal to the given value from the vector.

The vector will automatically sort and deduplicate items for you due to ON = true.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, true>::new();
v.push(0);
v.push(2);
v.push(1);
v.remove(&1);
let dedup = v.iter().cloned().collect::<Vec<_>>();
assert_eq!(dedup, [0, 2]);
Source§

fn extend(&mut self, iter: impl IntoIterator<Item = Self::Item>)

Extends the vector with the given iterator.

The vector will automatically sort and deduplicate items for you due to ON = true.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, true>::new();
v.extend([0, 2, 1]);
let dedup = v.iter().cloned().collect::<Vec<_>>();
assert_eq!(dedup, [0, 1, 2]);
Source§

fn iter(&self) -> impl Iterator<Item = &Self::Item>

Returns an iterator visiting all items in the vector.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, true>::new();
v.push(0);
v.push(1);
for x in v.iter() {
    println!("{x}");
}
Source§

type Item = T

Vector item type.
Source§

type Container = Vec<T>

Vector type.
Source§

fn is_empty(&self) -> bool

Returns true if the vector is empty.
Source§

impl<T> AsDedupVec for DedupVec<T, false>
where T: Hash + Eq + Default + Clone,

Source§

fn new() -> Self

Creates a new empty vector.

The vector won’t do anything to keep the deduplicated status, but it panics when you insert duplicate item into the vector in debug mode due to ON = false.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<i32, false>::new();
Source§

fn len(&self) -> usize

Returns number of items.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, false>::new();
v.push(0);
assert_eq!(v.len(), 1);
Source§

fn contains(&self, value: &Self::Item) -> bool

Returns true if the vector contains the given value.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, false>::new();
v.push(0);
assert!(v.contains(&0));
Source§

fn push(&mut self, value: Self::Item)

Appends the given value to the end of the vector.

The vector won’t do anything to keep the deduplicated status, but it panics when you insert duplicate item into the vector in debug mode due to ON = false.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, false>::new();
v.push(0);
v.push(2);
v.push(1);
let dedup = v.iter().cloned().collect::<Vec<_>>();
assert_eq!(dedup, [0, 2, 1]);
Source§

fn remove(&mut self, value: &Self::Item) -> Option<Self::Item>

Removes an item that is equal to the given value from the vector.

The vector won’t do anything to keep the deduplicated status, but it panics when you insert duplicate item into the vector in debug mode due to ON = false.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, false>::new();
v.push(0);
v.push(1);
v.push(2);
v.remove(&1);
let dedup = v.iter().cloned().collect::<Vec<_>>();
assert_eq!(dedup, [0, 2]);
Source§

fn extend(&mut self, iter: impl IntoIterator<Item = Self::Item>)

Extends the vector with the given iterator.

The vector won’t do anything to keep the deduplicated status, but it panics when you insert duplicate item into the vector in debug mode due to ON = false.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, false>::new();
v.extend([0, 2, 1]);
let dedup = v.iter().cloned().collect::<Vec<_>>();
assert_eq!(dedup, [0, 2, 1]);
Source§

fn iter(&self) -> impl Iterator<Item = &Self::Item>

Returns an iterator visiting all items in the vector.

§Examples
use my_ecs::ds::{AsDedupVec, DedupVec};

let mut v = DedupVec::<_, false>::new();
v.push(0);
v.push(1);
for x in v.iter() {
    println!("{x}");
}
Source§

type Item = T

Vector item type.
Source§

type Container = SetValueList<T, RandomState>

Vector type.
Source§

fn is_empty(&self) -> bool

Returns true if the vector is empty.
Source§

impl<T, const ON: bool> Debug for DedupVec<T, ON>
where Self: AsDedupVec, <Self as AsDedupVec>::Container: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<DedupVec<T>> for Vec<T>
where T: Ord,

Source§

fn from(value: DedupVec<T, true>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<DedupVec<T, false>> for Vec<T>
where T: Hash + Eq + Default + Clone,

Source§

fn from(value: DedupVec<T, false>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, const ON: bool = true> !Freeze for DedupVec<T, ON>

§

impl<T, const ON: bool = true> !RefUnwindSafe for DedupVec<T, ON>

§

impl<T, const ON: bool = true> !Send for DedupVec<T, ON>

§

impl<T, const ON: bool = true> !Sync for DedupVec<T, ON>

§

impl<T, const ON: bool = true> !Unpin for DedupVec<T, ON>

§

impl<T, const ON: bool = true> !UnwindSafe for DedupVec<T, ON>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.