#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct BinaryVec<T> {
vec: Vec<T>,
}
impl<T: Ord> BinaryVec<T> {
pub fn new() -> Self {
BinaryVec { vec: Vec::new() }
}
pub fn with_capacity(capacity: usize) -> Self {
BinaryVec {
vec: Vec::with_capacity(capacity),
}
}
pub fn into_vec(self) -> Vec<T> {
self.vec
}
pub fn iter(&self) -> std::slice::Iter<T> {
self.vec.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
self.vec.iter_mut()
}
pub fn insert(&mut self, value: T) -> usize {
match self.vec.binary_search(&value) {
Ok(index) => index,
Err(index) => {
self.vec.insert(index, value);
index
}
}
}
pub fn get(&self, index: usize) -> Option<&T> {
self.vec.get(index)
}
pub fn get_index(&self, value: &T) -> Option<usize> {
self.vec.binary_search(value).ok()
}
pub fn remove(&mut self, index: usize) -> Option<T> {
if index < self.vec.len() {
Some(self.vec.remove(index))
} else {
None
}
}
pub fn remove_item(&mut self, value: &T) -> Option<T> {
match self.vec.binary_search(value) {
Ok(index) => Some(self.vec.remove(index)),
Err(_) => None,
}
}
pub fn contains(&self, value: &T) -> bool {
self.vec.binary_search(value).is_ok()
}
pub fn capacity(&self) -> usize {
self.vec.capacity()
}
pub fn len(&self) -> usize {
self.vec.len()
}
pub fn is_empty(&self) -> bool {
self.vec.is_empty()
}
pub fn clear(&mut self) {
self.vec.clear();
}
pub fn reserve(&mut self, additional: usize) {
self.vec.reserve(additional);
}
pub fn shrink_to_fit(&mut self) {
self.vec.shrink_to_fit();
}
pub fn resize(&mut self, new_len: usize, value: T)
where
T: Clone,
{
self.vec.resize(new_len, value);
}
pub fn as_slice(&self) -> &[T] {
&self.vec
}
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.vec
}
pub fn first(&self) -> Option<&T> {
self.vec.first()
}
pub fn last(&self) -> Option<&T> {
self.vec.last()
}
}
impl<T: Ord> Default for BinaryVec<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> IntoIterator for BinaryVec<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.vec.into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_insert_and_get() {
let mut binary_vec = BinaryVec::new();
assert_eq!(binary_vec.insert(5), 0);
assert_eq!(binary_vec.insert(3), 0);
assert_eq!(binary_vec.insert(7), 2);
assert_eq!(binary_vec.get(0), Some(&3));
assert_eq!(binary_vec.get(1), Some(&5));
assert_eq!(binary_vec.get(2), Some(&7));
}
#[test]
fn test_remove() {
let mut binary_vec = BinaryVec::new();
binary_vec.insert(5);
binary_vec.insert(3);
binary_vec.insert(7);
assert_eq!(binary_vec.remove(1), Some(5));
assert_eq!(binary_vec.get(0), Some(&3));
assert_eq!(binary_vec.get(1), Some(&7));
assert_eq!(binary_vec.remove(10), None); }
#[test]
fn test_remove_item() {
let mut binary_vec = BinaryVec::new();
binary_vec.insert(5);
binary_vec.insert(3);
binary_vec.insert(7);
assert_eq!(binary_vec.remove_item(&5), Some(5));
assert_eq!(binary_vec.get(0), Some(&3));
assert_eq!(binary_vec.get(1), Some(&7));
assert_eq!(binary_vec.remove_item(&10), None); }
#[test]
fn test_get_index() {
let mut binary_vec = BinaryVec::new();
binary_vec.insert(5);
binary_vec.insert(3);
binary_vec.insert(7);
assert_eq!(binary_vec.get_index(&5), Some(1));
assert_eq!(binary_vec.get_index(&3), Some(0));
assert_eq!(binary_vec.get_index(&7), Some(2));
assert_eq!(binary_vec.get_index(&10), None); }
}