Files
@ 9b5ea2f879a4
Branch filter:
Location: CSY/reowolf/src/collections/sets.rs
9b5ea2f879a4
2.5 KiB
application/rls-services+xml
Implement MPSC queue
Multiple producer, single consumer queue with the purpose of acting
as the inbox for components.
Multiple producer, single consumer queue with the purpose of acting
as the inbox for components.
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #![allow(dead_code)] // For now, come back to this when compiler has matured -- MH 27/05/21
use std::collections::VecDeque;
/// Simple double ended queue that ensures that all elements are unique. Queue
/// elements are not ordered (we expect the queue to be rather small).
pub struct DequeSet<T: Eq> {
inner: VecDeque<T>,
}
impl<T: Eq> DequeSet<T> {
pub fn new() -> Self {
Self{ inner: VecDeque::new() }
}
#[inline]
pub fn pop_front(&mut self) -> Option<T> {
self.inner.pop_front()
}
#[inline]
pub fn pop_back(&mut self) -> Option<T> {
self.inner.pop_back()
}
#[inline]
pub fn push_back(&mut self, to_push: T) {
for element in self.inner.iter() {
if *element == to_push {
return;
}
}
self.inner.push_back(to_push);
}
#[inline]
pub fn push_front(&mut self, to_push: T) {
for element in self.inner.iter() {
if *element == to_push {
return;
}
}
self.inner.push_front(to_push);
}
#[inline]
pub fn clear(&mut self) {
self.inner.clear();
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
}
/// Simple vector set that ensures that all elements are unique. Elements are
/// not ordered (we expect the vector to be small).
pub struct VecSet<T: Eq> {
inner: Vec<T>,
}
impl<T: Eq> VecSet<T> {
pub fn new() -> Self {
Self{ inner: Vec::new() }
}
#[inline]
pub fn pop(&mut self) -> Option<T> {
self.inner.pop()
}
/// Pushes a new element into the set. Returns `false` if it was already
/// present and `true` if it is newly added.
#[inline]
pub fn push(&mut self, to_push: T) -> bool {
for element in self.inner.iter() {
if *element == to_push {
return false;
}
}
self.inner.push(to_push);
return true
}
#[inline]
pub fn clear(&mut self) {
self.inner.clear();
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item=&T> {
return self.inner.iter();
}
#[inline]
pub fn contains(&self, item: &T) -> bool {
return self.inner.contains(item);
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
return self.inner.len();
}
#[inline]
pub fn into_vec(self) -> Vec<T> {
return self.inner;
}
}
|