Skip to main content

tagged_vec/
lib.rs

1//! An alternative to the standard libraries' [`Vec`] which is indexed with a custom type instead of [`usize`].
2//!
3//! This is useful to catch errors like using the wrong variable to index the vector.
4
5#![warn(missing_docs)]
6
7use std::{marker::PhantomData, ops::RangeBounds};
8
9use mapped_range_bounds::MappedRangeBounds;
10
11mod mapped_range_bounds;
12mod trait_impls;
13
14/// A [`Vec`] wrapper that allows indexing only via the given `Index` type.
15///
16/// For actual operation, `Index` must implement [`From<usize>`] and [`Into<usize>`].
17pub struct TaggedVec<Index, Value> {
18    index_type: PhantomData<Index>,
19    vec: Vec<Value>,
20}
21
22impl<Index, Value> TaggedVec<Index, Value> {
23    /// Creates a new empty `TaggedVec`.
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    /// Creates a new empty `TaggedVec` with at least the specified capacity.
29    pub fn with_capacity(capacity: usize) -> Self {
30        Self {
31            index_type: PhantomData,
32            vec: Vec::with_capacity(capacity),
33        }
34    }
35
36    /// Returns the number of elements in the `TaggedVec`.
37    pub fn len(&self) -> usize {
38        self.vec.len()
39    }
40
41    /// Returns `true` if the `TaggedVec` contains no elements.
42    pub fn is_empty(&self) -> bool {
43        self.vec.is_empty()
44    }
45
46    /// Returns the total number of elements the `TaggedVec` can hold without reallocating.
47    pub fn capacity(&self) -> usize {
48        self.vec.capacity()
49    }
50
51    /// Inserts the given value at the back of the `TaggedVec`, returning its index.
52    pub fn push(&mut self, value: Value) -> Index
53    where
54        Index: From<usize>,
55    {
56        let index = self.vec.len().into();
57        self.vec.push(value);
58        index
59    }
60
61    /// Insert a single value into the `TaggedVec` by constructing it in place.
62    ///
63    /// This method allows to create the value while already knowing its index.
64    /// Returns the index.
65    pub fn push_in_place(&mut self, value: impl FnOnce(Index) -> Value) -> Index
66    where
67        Index: From<usize>,
68    {
69        let index = self.vec.len();
70        self.vec.push(value(index.into()));
71        index.into()
72    }
73
74    /// Removes the value at the back of the `TaggedVec` and returns it with its index.
75    pub fn pop(&mut self) -> Option<(Index, Value)>
76    where
77        Index: From<usize>,
78    {
79        if let Some(value) = self.vec.pop() {
80            Some((self.vec.len().into(), value))
81        } else {
82            None
83        }
84    }
85
86    /// Inserts the given `value` at position `index`, shifting all existing values in range `index..` one position to the right.
87    pub fn insert(&mut self, index: Index, value: Value)
88    where
89        Index: Into<usize>,
90    {
91        self.vec.insert(index.into(), value);
92    }
93
94    /// See [`Vec::splice`].
95    pub fn splice<I: IntoIterator<Item = Value>>(
96        &mut self,
97        range: impl RangeBounds<Index>,
98        replace_with: I,
99    ) -> std::vec::Splice<'_, I::IntoIter>
100    where
101        usize: for<'a> From<&'a Index>,
102    {
103        self.vec.splice(MappedRangeBounds::new(range), replace_with)
104    }
105
106    /// Retains only the values specified by the predicate.
107    ///
108    /// In other words, remove all values `v` for which `f(&v)` returns `false`.
109    /// This method operates in place, visiting each value exactly once in the original order, and preserves the order of the retained values.
110    pub fn retain(&mut self, f: impl FnMut(&Value) -> bool) {
111        self.vec.retain(f);
112    }
113
114    /// Removes the elements at the specified indices, shifting other elements to the left to fill gaps as required.
115    ///
116    /// The provided indices must be sorted.
117    pub fn remove_multi(&mut self, indices: impl IntoIterator<Item = Index>)
118    where
119        Index: Into<usize> + Clone,
120    {
121        let mut indices = indices.into_iter().peekable();
122        let mut current_index = 0;
123        self.vec.retain(|_| {
124            if let Some(next_delete_index) = indices.peek() {
125                let next_delete_index = next_delete_index.clone().into();
126                let result = if next_delete_index == current_index {
127                    indices.next();
128
129                    if let Some(next_next_delete_index) = indices.peek() {
130                        let next_next_delete_index: usize = next_next_delete_index.clone().into();
131                        assert!(next_next_delete_index > next_delete_index);
132                    }
133
134                    false
135                } else {
136                    true
137                };
138                current_index += 1;
139                result
140            } else {
141                true
142            }
143        });
144
145        assert!(indices.next().is_none());
146    }
147
148    /// Returns an iterator over references to the entries of the `TaggedVec`.
149    pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Index, &Value)> + ExactSizeIterator
150    where
151        Index: From<usize>,
152    {
153        self.vec
154            .iter()
155            .enumerate()
156            .map(|(index, value)| (index.into(), value))
157    }
158
159    /// Returns an iterator over mutable references to the entries of the `TaggedVec`.
160    pub fn iter_mut(
161        &mut self,
162    ) -> impl DoubleEndedIterator<Item = (Index, &mut Value)> + ExactSizeIterator
163    where
164        Index: From<usize>,
165    {
166        self.vec
167            .iter_mut()
168            .enumerate()
169            .map(|(index, value)| (index.into(), value))
170    }
171
172    /// Returns an iterator over references to the values of the `TaggedVec`.
173    pub fn iter_values(&self) -> std::slice::Iter<'_, Value> {
174        self.vec.iter()
175    }
176
177    /// Returns an iterator over mutable references to the values of the `TaggedVec`.
178    pub fn iter_values_mut(&mut self) -> std::slice::IterMut<'_, Value> {
179        self.vec.iter_mut()
180    }
181
182    /// Returns an iterator over the indices of the `TaggedVec`.
183    pub fn iter_indices(&self) -> impl DoubleEndedIterator<Item = Index> + ExactSizeIterator
184    where
185        Index: From<usize>,
186    {
187        (0..self.vec.len()).map(Into::into)
188    }
189
190    /// Consumes the `TaggedVec`, returning an iterator over the values.
191    pub fn into_values_iter(self) -> std::vec::IntoIter<Value> {
192        self.vec.into_iter()
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use crate::TaggedVec;
199
200    #[test]
201    fn delete_multi() {
202        let mut v = TaggedVec::<usize, _>::from_iter([0, 1, 2, 3, 4]);
203        v.remove_multi([0, 4]);
204        assert_eq!(v, vec![1, 2, 3].into());
205
206        let mut v = TaggedVec::<usize, _>::from_iter([0, 1, 2, 3, 4]);
207        v.remove_multi([0, 2, 4]);
208        assert_eq!(v, vec![1, 3].into());
209
210        let mut v = TaggedVec::<usize, _>::from_iter([0, 1, 2, 3, 4]);
211        v.remove_multi([1, 3]);
212        assert_eq!(v, vec![0, 2, 4].into());
213    }
214}