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    /// Returns the untagged slice of the `Vec` underlying this `TaggedVec`.
52    pub fn as_untagged_slice(&self) -> &[Value] {
53        &self.vec
54    }
55
56    /// Returns the untagged mutable slice of the `Vec` underlying this `TaggedVec`.
57    pub fn as_untagged_mut_slice(&mut self) -> &mut [Value] {
58        &mut self.vec
59    }
60
61    /// Inserts the given value at the back of the `TaggedVec`, returning its index.
62    pub fn push(&mut self, value: Value) -> Index
63    where
64        Index: From<usize>,
65    {
66        let index = self.vec.len().into();
67        self.vec.push(value);
68        index
69    }
70
71    /// Insert a single value into the `TaggedVec` by constructing it in place.
72    ///
73    /// This method allows to create the value while already knowing its index.
74    /// Returns the index.
75    pub fn push_in_place(&mut self, value: impl FnOnce(Index) -> Value) -> Index
76    where
77        Index: From<usize>,
78    {
79        let index = self.vec.len();
80        self.vec.push(value(index.into()));
81        index.into()
82    }
83
84    /// Removes the value at the back of the `TaggedVec` and returns it with its index.
85    pub fn pop(&mut self) -> Option<(Index, Value)>
86    where
87        Index: From<usize>,
88    {
89        if let Some(value) = self.vec.pop() {
90            Some((self.vec.len().into(), value))
91        } else {
92            None
93        }
94    }
95
96    /// Inserts the given `value` at position `index`, shifting all existing values in range `index..` one position to the right.
97    pub fn insert(&mut self, index: Index, value: Value)
98    where
99        Index: Into<usize>,
100    {
101        self.vec.insert(index.into(), value);
102    }
103
104    /// See [`Vec::splice`].
105    pub fn splice<I: IntoIterator<Item = Value>>(
106        &mut self,
107        range: impl RangeBounds<Index>,
108        replace_with: I,
109    ) -> std::vec::Splice<'_, I::IntoIter>
110    where
111        usize: for<'a> From<&'a Index>,
112    {
113        self.vec.splice(MappedRangeBounds::new(range), replace_with)
114    }
115
116    /// Retains only the values specified by the predicate.
117    ///
118    /// In other words, remove all values `v` for which `f(&v)` returns `false`.
119    /// This method operates in place, visiting each value exactly once in the original order, and preserves the order of the retained values.
120    pub fn retain(&mut self, f: impl FnMut(&Value) -> bool) {
121        self.vec.retain(f);
122    }
123
124    /// Removes the elements at the specified indices, shifting other elements to the left to fill gaps as required.
125    ///
126    /// The provided indices must be sorted.
127    pub fn remove_multi(&mut self, indices: impl IntoIterator<Item = Index>)
128    where
129        Index: Into<usize> + Clone,
130    {
131        let mut indices = indices.into_iter().peekable();
132        let mut current_index = 0;
133        self.vec.retain(|_| {
134            if let Some(next_delete_index) = indices.peek() {
135                let next_delete_index = next_delete_index.clone().into();
136                let result = if next_delete_index == current_index {
137                    indices.next();
138
139                    if let Some(next_next_delete_index) = indices.peek() {
140                        let next_next_delete_index: usize = next_next_delete_index.clone().into();
141                        assert!(next_next_delete_index > next_delete_index);
142                    }
143
144                    false
145                } else {
146                    true
147                };
148                current_index += 1;
149                result
150            } else {
151                true
152            }
153        });
154
155        assert!(indices.next().is_none());
156    }
157
158    /// Returns an iterator over references to the entries of the `TaggedVec`.
159    pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Index, &Value)> + ExactSizeIterator
160    where
161        Index: From<usize>,
162    {
163        self.vec
164            .iter()
165            .enumerate()
166            .map(|(index, value)| (index.into(), value))
167    }
168
169    /// Returns an iterator over mutable references to the entries of the `TaggedVec`.
170    pub fn iter_mut(
171        &mut self,
172    ) -> impl DoubleEndedIterator<Item = (Index, &mut Value)> + ExactSizeIterator
173    where
174        Index: From<usize>,
175    {
176        self.vec
177            .iter_mut()
178            .enumerate()
179            .map(|(index, value)| (index.into(), value))
180    }
181
182    /// Returns an iterator over references to the values of the `TaggedVec`.
183    pub fn iter_values(&self) -> std::slice::Iter<'_, Value> {
184        self.vec.iter()
185    }
186
187    /// Returns an iterator over mutable references to the values of the `TaggedVec`.
188    pub fn iter_values_mut(&mut self) -> std::slice::IterMut<'_, Value> {
189        self.vec.iter_mut()
190    }
191
192    /// Returns an iterator over the indices of the `TaggedVec`.
193    pub fn iter_indices(&self) -> impl DoubleEndedIterator<Item = Index> + ExactSizeIterator
194    where
195        Index: From<usize>,
196    {
197        (0..self.vec.len()).map(Into::into)
198    }
199
200    /// Consumes the `TaggedVec`, returning an iterator over the values.
201    pub fn into_values_iter(self) -> std::vec::IntoIter<Value> {
202        self.vec.into_iter()
203    }
204}
205
206#[cfg(test)]
207mod tests {
208    use crate::TaggedVec;
209
210    #[test]
211    fn delete_multi() {
212        let mut v = TaggedVec::<usize, _>::from_iter([0, 1, 2, 3, 4]);
213        v.remove_multi([0, 4]);
214        assert_eq!(v, vec![1, 2, 3].into());
215
216        let mut v = TaggedVec::<usize, _>::from_iter([0, 1, 2, 3, 4]);
217        v.remove_multi([0, 2, 4]);
218        assert_eq!(v, vec![1, 3].into());
219
220        let mut v = TaggedVec::<usize, _>::from_iter([0, 1, 2, 3, 4]);
221        v.remove_multi([1, 3]);
222        assert_eq!(v, vec![0, 2, 4].into());
223    }
224}