1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::vec::Vec;
use core::borrow::{Borrow, BorrowMut};
use core::convert::{AsMut, AsRef};
use core::ops;
pub struct LinearSet<T, const N: usize> {
vec: Vec<T, N>,
}
impl<T, const N: usize> LinearSet<T, N> {
const CAPACITY: usize = N;
pub const fn new() -> Self {
LinearSet { vec: Vec::new() }
}
pub fn capacity(&self) -> usize {
Self::CAPACITY
}
}
impl<T: PartialEq, const N: usize> LinearSet<T, N> {
pub fn get_index(&self, value: &T) -> Option<usize> {
if let Some((i, _item)) = self.iter().enumerate().find(|(_i, item)| item == &value) {
Some(i)
} else {
None
}
}
pub fn get(&self, value: &T) -> Option<&T> {
if let Some(i) = self.get_index(value) {
Some(&self[i])
} else {
None
}
}
pub fn contains(&self, value: &T) -> bool {
self.iter().any(|x| x == value)
}
pub fn insert(&mut self, value: T) -> Result<bool, T> {
if let Some(_) = self.get_index(&value) {
Ok(false)
} else {
self.vec.push(value)?;
Ok(true)
}
}
pub fn remove(&mut self, value: &T) -> bool {
if let Some(i) = self.get_index(value) {
self.vec.swap_remove(i);
true
} else {
false
}
}
}
impl<T, const N: usize> ops::Deref for LinearSet<T, N> {
type Target = [T];
fn deref(&self) -> &[T] {
self.vec.deref()
}
}
impl<T, const N: usize> ops::DerefMut for LinearSet<T, N> {
fn deref_mut(&mut self) -> &mut [T] {
self.vec.deref_mut()
}
}
impl<T, const N: usize> AsRef<[T]> for LinearSet<T, N> {
fn as_ref(&self) -> &[T] {
self
}
}
impl<T, const N: usize> AsMut<[T]> for LinearSet<T, N> {
fn as_mut(&mut self) -> &mut [T] {
self
}
}
impl<T, const N: usize> Borrow<[T]> for LinearSet<T, N> {
fn borrow(&self) -> &[T] {
&self[..]
}
}
impl<T, const N: usize> BorrowMut<[T]> for LinearSet<T, N> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut self[..]
}
}