| 1 | use crate::*; |
| 2 | use std::ops::{Index, IndexMut}; |
| 3 | |
| 4 | /// Collection with mutable capacity. |
| 5 | pub trait CapacityMut = Capacity + Reserve; |
| 6 | |
| 7 | /// Immutable stack data structure. |
| 8 | /// |
| 9 | /// A stack provides two main operations: |
| 10 | /// - [`PushBack::push_back`], which adds an element to the collection, and |
| 11 | /// - [`PopBack::pop_back`], which removes the most recently added element that was not yet removed. |
| 12 | /// |
| 13 | /// This trait alias describes the immutables operations derived from the two main operation above: |
| 14 | /// - [`Len::len`], returning the number of elements in the stack, and |
| 15 | /// - [`Back::back`], returning a reference to the most recently added element that was not yet removed. |
| 16 | pub trait Stack<T> = Collection<Item = T> + Len + Back; |
| 17 | |
| 18 | /// Mutable stack data structure. |
| 19 | /// |
| 20 | /// This trait alias describes the mutables operations on a stack. |
| 21 | /// See [`Stack`] for more details. |
| 22 | pub trait StackMut<T> = Stack<T> + BackMut + PushBack + PopBack; |
| 23 | |
| 24 | /// Immutable array data structure (conventionally nammed "Vec"). |
| 25 | /// |
| 26 | /// A Vec is essentially a [`Stack`] indexable by a `usize`. |
| 27 | pub trait Vec<T> = Stack<T> + Index<usize, Output = T>; |
| 28 | |
| 29 | /// Mutable Vec data structure. |
| 30 | /// |
| 31 | /// This trait alias describes the mutables operations on a Vec. |
| 32 | /// See [`Vec`] for more details. |
| 33 | pub trait VecMut<T> = Vec<T> + StackMut<T> + IndexMut<usize>; |
| 34 | |
| 35 | /// Immutable double-ended queue. |
| 36 | /// |
| 37 | /// A double-ended queue (abbreviated to deque) is a generalization of a stack in which |
| 38 | /// elements can be added and removed from both ends. |
| 39 | /// Such a data structure provides two additional operations compared to regular stacks: |
| 40 | /// - [`PushFront::push_front`], which adds an element to the front of collection, and |
| 41 | /// - [`PopFront::pop_front`], which removes the front element of the collection. |
| 42 | /// |
| 43 | /// This trait alias describes the immutables operations available on deques. |
| 44 | pub trait Deque<T> = Stack<T> + Front; |
| 45 | |
| 46 | /// Mutable double-ended queue. |
| 47 | /// |
| 48 | /// This trait alias describes the mutables operations on a deque. |
| 49 | /// See [`Deque`] for more details. |
| 50 | pub trait DequeMut<T> = StackMut<T> + FrontMut + PushFront + PopFront; |
| 51 | |
| 52 | /// Immutable indexable deque. |
| 53 | /// |
| 54 | /// See [`Deque`] and [`Vec`] for more details. |
| 55 | pub trait VecDeque<T> = Deque<T> + Vec<T>; |
| 56 | |
| 57 | /// Mutable indexable deque. |
| 58 | /// |
| 59 | /// See [`VecDeque`], [`DequeMut`] and [`VecMut`] for more details. |
| 60 | pub trait VecDequeMut<T> = VecDeque<T> + DequeMut<T> + VecMut<T>; |
| 61 | |
| 62 | /// Imutable set data structure. |
| 63 | /// |
| 64 | /// A set is an unordered collection storing at most one single copy of each element. |
| 65 | pub trait Set<T> = Collection<Item = T> + Len + for<'a> Get<&'a T>; |
| 66 | |
| 67 | /// Mutable set data structure. |
| 68 | pub trait SetMut<T> = Set<T> + Insert<Output = bool> + for<'a> Remove<&'a T>; |
| 69 | |
| 70 | /// Imutable map data structure. |
| 71 | /// |
| 72 | /// A map is an unordered collection storing key-value pairs, indexed by the key. |
| 73 | pub trait Map<K, V> = |
| 74 | Keyed<Key = K, Item = V> + Len + for<'a> Get<&'a K> + for<'a> GetKeyValue<&'a K>; |
| 75 | |
| 76 | /// Mutable map data structure. |
| 77 | pub trait MapMut<K, V> = |
| 78 | Map<K, V> + for<'a> GetMut<&'a K> + MapInsert<K, Output = Option<V>> + for<'a> Remove<&'a K>; |
| 79 | |
| 80 | /// Imutable slab data structure. |
| 81 | /// |
| 82 | /// A slab is a linear collection storing each element at a given index. |
| 83 | /// The index of the element is allocated and returned upon insertion. |
| 84 | pub trait Slab<T> = Collection<Item = T> + Len + Get<usize>; |
| 85 | |
| 86 | /// Mutable slab data structure. |
| 87 | pub trait SlabMut<T> = Slab<T> + GetMut<usize> + Insert<Output = usize> + Remove<usize>; |