all: split reusable components into workspace crates
This commit is contained in:
parent
2a079ed800
commit
657e7ce2f7
225 changed files with 7422 additions and 17602 deletions
58
utils/src/vecset.rs
Normal file
58
utils/src/vecset.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
pub struct VecSet<T> {
|
||||
vec: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T> Default for VecSet<T> {
|
||||
fn default() -> Self {
|
||||
Self { vec: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for VecSet<T> {
|
||||
type Target = [T];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.vec
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> VecSet<T> {
|
||||
pub fn clear(&mut self) {
|
||||
self.vec.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> VecSet<T> {
|
||||
pub fn insert(&mut self, val: T) -> bool {
|
||||
if self.vec.contains(&val) {
|
||||
return false;
|
||||
}
|
||||
self.vec.push(val);
|
||||
true
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, val: &T) -> bool {
|
||||
for i in 0..self.vec.len() {
|
||||
if self.vec[i] == *val {
|
||||
self.vec.swap_remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.vec.pop()
|
||||
}
|
||||
|
||||
pub fn extend(&mut self, vals: &[T])
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
for v in vals.iter().copied() {
|
||||
self.insert(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue