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