diff options
author | Aria Shrimpton <me@aria.rip> | 2024-01-31 22:59:04 +0000 |
---|---|---|
committer | Aria Shrimpton <me@aria.rip> | 2024-01-31 22:59:04 +0000 |
commit | c082d63ef2632ec08c658f09930cbab69c2bb2e5 (patch) | |
tree | a892ae0849c24be2769828fdb43b5a406ae4c83a /src | |
parent | 9bcd8b81cb49b58c8139d574d2cd056ed199f393 (diff) |
library: require intoiterator
Diffstat (limited to 'src')
-rw-r--r-- | src/crates/library/src/eager_sorted_vector.rs | 9 | ||||
-rw-r--r-- | src/crates/library/src/eager_unique_vector.rs | 9 | ||||
-rw-r--r-- | src/crates/library/src/profiler.rs | 52 | ||||
-rw-r--r-- | src/crates/library/src/traits.rs | 4 |
4 files changed, 51 insertions, 23 deletions
diff --git a/src/crates/library/src/eager_sorted_vector.rs b/src/crates/library/src/eager_sorted_vector.rs index bf67105..20d270a 100644 --- a/src/crates/library/src/eager_sorted_vector.rs +++ b/src/crates/library/src/eager_sorted_vector.rs @@ -211,6 +211,15 @@ impl<T: Ord> Default for EagerSortedVec<T> { } } +impl<T> IntoIterator for EagerSortedVec<T> { + type Item = T; + type IntoIter = std::vec::IntoIter<T>; + + fn into_iter(self) -> Self::IntoIter { + self.v.into_iter() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/crates/library/src/eager_unique_vector.rs b/src/crates/library/src/eager_unique_vector.rs index 632762b..a8f5abf 100644 --- a/src/crates/library/src/eager_unique_vector.rs +++ b/src/crates/library/src/eager_unique_vector.rs @@ -218,6 +218,15 @@ impl<T: Ord> Default for EagerUniqueVec<T> { } } +impl<T> IntoIterator for EagerUniqueVec<T> { + type Item = T; + type IntoIter = std::vec::IntoIter<T>; + + fn into_iter(self) -> Self::IntoIter { + self.v.into_iter() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/crates/library/src/profiler.rs b/src/crates/library/src/profiler.rs index 585d745..2385553 100644 --- a/src/crates/library/src/profiler.rs +++ b/src/crates/library/src/profiler.rs @@ -9,7 +9,7 @@ use std::{ use crate::traits::{Container, Indexable, Mapping, Stack}; pub struct ProfilerWrapper<const ID: usize, T, E> { - inner: T, + inner: Option<T>, max_n: usize, n_contains: usize, n_insert: usize, @@ -27,7 +27,7 @@ pub struct ProfilerWrapper<const ID: usize, T, E> { impl<const ID: usize, T: Default, E> Default for ProfilerWrapper<ID, T, E> { fn default() -> Self { Self { - inner: T::default(), + inner: Some(T::default()), max_n: 0, n_contains: 0, n_insert: 0, @@ -46,47 +46,47 @@ impl<const ID: usize, T: Default, E> Default for ProfilerWrapper<ID, T, E> { impl<const ID: usize, T: Container<E>, E> ProfilerWrapper<ID, T, E> { fn add_n(&mut self) { - self.max_n = self.max_n.max(self.inner.len()); + self.max_n = self.max_n.max(self.inner.as_ref().unwrap().len()); } } impl<const ID: usize, T: Mapping<K, V>, K, V> ProfilerWrapper<ID, T, (K, V)> { fn add_n_map(&mut self) { - self.max_n = self.max_n.max(self.inner.len()); + self.max_n = self.max_n.max(self.inner.as_ref().unwrap().len()); } } impl<const ID: usize, T: Container<E>, E> Container<E> for ProfilerWrapper<ID, T, E> { fn len(&self) -> usize { - self.inner.len() + self.inner.as_ref().unwrap().len() } fn contains(&mut self, x: &E) -> bool { self.add_n(); self.n_contains += 1; - self.inner.contains(x) + self.inner.as_mut().unwrap().contains(x) } fn is_empty(&self) -> bool { - self.inner.is_empty() + self.inner.as_ref().unwrap().is_empty() } fn insert(&mut self, elt: E) { self.add_n(); self.n_insert += 1; - self.inner.insert(elt) + self.inner.as_mut().unwrap().insert(elt) } fn clear(&mut self) { self.add_n(); self.n_clear += 1; - self.inner.clear() + self.inner.as_mut().unwrap().clear() } fn remove(&mut self, elt: E) -> Option<E> { self.add_n(); self.n_remove += 1; - self.inner.remove(elt) + self.inner.as_mut().unwrap().remove(elt) } } @@ -96,19 +96,19 @@ impl<const ID: usize, T: Indexable<E> + Container<E>, E> Indexable<E> fn first(&mut self) -> Option<&E> { self.add_n(); self.n_first += 1; - self.inner.first() + self.inner.as_mut().unwrap().first() } fn last(&mut self) -> Option<&E> { self.add_n(); self.n_last += 1; - self.inner.last() + self.inner.as_mut().unwrap().last() } fn nth(&mut self, n: usize) -> Option<&E> { self.add_n(); self.n_nth += 1; - self.inner.nth(n) + self.inner.as_mut().unwrap().nth(n) } } @@ -116,49 +116,49 @@ impl<const ID: usize, T: Stack<E> + Container<E>, E> Stack<E> for ProfilerWrappe fn push(&mut self, elt: E) { self.add_n(); self.n_push += 1; - self.inner.push(elt) + self.inner.as_mut().unwrap().push(elt) } fn pop(&mut self) -> Option<E> { self.add_n(); self.n_pop += 1; - self.inner.pop() + self.inner.as_mut().unwrap().pop() } } impl<const ID: usize, T: Mapping<K, V>, K, V> Mapping<K, V> for ProfilerWrapper<ID, T, (K, V)> { fn len(&self) -> usize { - self.inner.len() + self.inner.as_ref().unwrap().len() } fn contains(&mut self, x: &K) -> bool { self.add_n_map(); self.n_contains += 1; - self.inner.contains(x) + self.inner.as_mut().unwrap().contains(x) } fn insert(&mut self, key: K, val: V) -> Option<V> { self.add_n_map(); self.n_insert += 1; - self.inner.insert(key, val) + self.inner.as_mut().unwrap().insert(key, val) } fn get(&mut self, key: &K) -> Option<&V> { self.add_n_map(); self.n_get += 1; - self.inner.get(key) + self.inner.as_mut().unwrap().get(key) } fn remove(&mut self, key: &K) -> Option<V> { self.add_n_map(); self.n_remove += 1; - self.inner.remove(key) + self.inner.as_mut().unwrap().remove(key) } fn clear(&mut self) { self.add_n_map(); self.n_clear += 1; - self.inner.clear() + self.inner.as_mut().unwrap().clear() } } @@ -188,3 +188,13 @@ impl<const ID: usize, T, E> Drop for ProfilerWrapper<ID, T, E> { writeln!(f, "{}", self.n_get).unwrap(); } } + +impl<const ID: usize, T: IntoIterator, E> IntoIterator for ProfilerWrapper<ID, T, E> { + type Item = T::Item; + + type IntoIter = T::IntoIter; + + fn into_iter(mut self) -> Self::IntoIter { + self.inner.take().unwrap().into_iter() + } +} diff --git a/src/crates/library/src/traits.rs b/src/crates/library/src/traits.rs index bf4616a..528e643 100644 --- a/src/crates/library/src/traits.rs +++ b/src/crates/library/src/traits.rs @@ -1,5 +1,5 @@ //! Common traits for primrose container types -pub trait Container<T>: Default { +pub trait Container<T>: Default + IntoIterator<Item = T> { /// Get the current number of elements fn len(&self) -> usize; @@ -30,7 +30,7 @@ pub trait Indexable<T> { fn nth(&mut self, n: usize) -> Option<&T>; } -pub trait Mapping<K, V>: Default { +pub trait Mapping<K, V>: Default + IntoIterator<Item = (K, V)> { /// Get the current number of elements fn len(&self) -> usize; |