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