binary_vec/
lib.rs

1#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
2pub struct BinaryVec<T> {
3    vec: Vec<T>,
4}
5
6impl<T: Ord> BinaryVec<T> {
7    /// Creates a new empty `BinaryVec`.
8    pub fn new() -> Self {
9        BinaryVec { vec: Vec::new() }
10    }
11
12    /// Creates a new `BinaryVec` with the specified capacity.
13    pub fn with_capacity(capacity: usize) -> Self {
14        BinaryVec {
15            vec: Vec::with_capacity(capacity),
16        }
17    }
18
19    /// Returns the vector internally used by `BinaryVec`.
20    pub fn into_vec(self) -> Vec<T> {
21        self.vec
22    }
23
24    /// Returns an iterator over references to the items in the `BinaryVec`.
25    pub fn iter(&self) -> std::slice::Iter<T> {
26        self.vec.iter()
27    }
28
29    /// Returns an iterator over mutable references to the items in the `BinaryVec`.
30    pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
31        self.vec.iter_mut()
32    }
33
34    /// Inserts a value into the `BinaryVec`, maintaining sorted order, and returns the index where
35    /// the value was inserted.
36    pub fn insert(&mut self, value: T) -> usize {
37        match self.vec.binary_search(&value) {
38            Ok(index) => index,
39            Err(index) => {
40                self.vec.insert(index, value);
41
42                index
43            }
44        }
45    }
46
47    /// Returns the item at the specified index, or `None` if the index is out of bounds.
48    pub fn get(&self, index: usize) -> Option<&T> {
49        self.vec.get(index)
50    }
51
52    /// Returns the index of the specified value, or `None` if the value is not found.
53    pub fn get_index(&self, value: &T) -> Option<usize> {
54        self.vec.binary_search(value).ok()
55    }
56
57    /// Removes the item at the specified index, returning it if it exists, or `None` if the index
58    /// is out of bounds.
59    pub fn remove(&mut self, index: usize) -> Option<T> {
60        if index < self.vec.len() {
61            Some(self.vec.remove(index))
62        } else {
63            None
64        }
65    }
66
67    /// Removes the specified item from the `BinaryVec`, returning it if it exists, or `None` if the
68    /// item is not found.
69    pub fn remove_item(&mut self, value: &T) -> Option<T> {
70        match self.vec.binary_search(value) {
71            Ok(index) => Some(self.vec.remove(index)),
72            Err(_) => None,
73        }
74    }
75
76    /// Checks if the `BinaryVec` contains the specified value.
77    pub fn contains(&self, value: &T) -> bool {
78        self.vec.binary_search(value).is_ok()
79    }
80
81    /// Returns the number of elements that can be stored in the `BinaryVec` without reallocating.
82    pub fn capacity(&self) -> usize {
83        self.vec.capacity()
84    }
85
86    /// Returns the number of elements in the `BinaryVec`.
87    pub fn len(&self) -> usize {
88        self.vec.len()
89    }
90
91    /// Checks if the `BinaryVec` is empty.
92    pub fn is_empty(&self) -> bool {
93        self.vec.is_empty()
94    }
95
96    /// Clears the `BinaryVec`, removing all elements.
97    pub fn clear(&mut self) {
98        self.vec.clear();
99    }
100
101    /// Reserves capacity for at least `additional` more elements to be inserted in the `BinaryVec`.
102    pub fn reserve(&mut self, additional: usize) {
103        self.vec.reserve(additional);
104    }
105
106    /// Shrinks the capacity of the `BinaryVec` to fit its current length.
107    pub fn shrink_to_fit(&mut self) {
108        self.vec.shrink_to_fit();
109    }
110
111    /// Resizes the `BinaryVec` to contain `new_len` elements, filling new elements with `value`.
112    pub fn resize(&mut self, new_len: usize, value: T)
113    where
114        T: Clone,
115    {
116        self.vec.resize(new_len, value);
117    }
118
119    /// Returns a reference to the underlying vector.
120    pub fn as_slice(&self) -> &[T] {
121        &self.vec
122    }
123
124    /// Returns a mutable reference to the underlying vector.
125    pub fn as_mut_slice(&mut self) -> &mut [T] {
126        &mut self.vec
127    }
128
129    /// Returns the first element of the `BinaryVec`, or `None` if it is empty.
130    pub fn first(&self) -> Option<&T> {
131        self.vec.first()
132    }
133
134    /// Returns the last element of the `BinaryVec`, or `None` if it is empty.
135    pub fn last(&self) -> Option<&T> {
136        self.vec.last()
137    }
138}
139
140impl<T: Ord> Default for BinaryVec<T> {
141    fn default() -> Self {
142        Self::new()
143    }
144}
145
146impl<T> IntoIterator for BinaryVec<T> {
147    type Item = T;
148    type IntoIter = std::vec::IntoIter<T>;
149
150    fn into_iter(self) -> Self::IntoIter {
151        self.vec.into_iter()
152    }
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158
159    #[test]
160    fn test_insert_and_get() {
161        let mut binary_vec = BinaryVec::new();
162        assert_eq!(binary_vec.insert(5), 0);
163        assert_eq!(binary_vec.insert(3), 0);
164        assert_eq!(binary_vec.insert(7), 2);
165        assert_eq!(binary_vec.get(0), Some(&3));
166        assert_eq!(binary_vec.get(1), Some(&5));
167        assert_eq!(binary_vec.get(2), Some(&7));
168    }
169
170    #[test]
171    fn test_remove() {
172        let mut binary_vec = BinaryVec::new();
173        binary_vec.insert(5);
174        binary_vec.insert(3);
175        binary_vec.insert(7);
176
177        assert_eq!(binary_vec.remove(1), Some(5));
178        assert_eq!(binary_vec.get(0), Some(&3));
179        assert_eq!(binary_vec.get(1), Some(&7));
180        assert_eq!(binary_vec.remove(10), None); // Out of bounds
181    }
182
183    #[test]
184    fn test_remove_item() {
185        let mut binary_vec = BinaryVec::new();
186        binary_vec.insert(5);
187        binary_vec.insert(3);
188        binary_vec.insert(7);
189
190        assert_eq!(binary_vec.remove_item(&5), Some(5));
191        assert_eq!(binary_vec.get(0), Some(&3));
192        assert_eq!(binary_vec.get(1), Some(&7));
193        assert_eq!(binary_vec.remove_item(&10), None); // Not found
194    }
195
196    #[test]
197    fn test_get_index() {
198        let mut binary_vec = BinaryVec::new();
199        binary_vec.insert(5);
200        binary_vec.insert(3);
201        binary_vec.insert(7);
202
203        assert_eq!(binary_vec.get_index(&5), Some(1));
204        assert_eq!(binary_vec.get_index(&3), Some(0));
205        assert_eq!(binary_vec.get_index(&7), Some(2));
206        assert_eq!(binary_vec.get_index(&10), None); // Not found
207    }
208}