summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2021-02-24 14:22:45 -0500
committerTavian Barnes <tavianator@tavianator.com>2021-02-24 14:22:45 -0500
commit55a2ed2a94a4a6f0a43d4a78e348ebec411dff41 (patch)
tree62376023deeb5967727a1ecf474273435bd5646f
parentaa310a561f9b6f430db74cf1dda30192501869e3 (diff)
downloadacap-55a2ed2a94a4a6f0a43d4a78e348ebec411dff41.tar.xz
exhaustive: Add a non-consuming iter()
-rw-r--r--src/exhaustive.rs40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/exhaustive.rs b/src/exhaustive.rs
index 320994e..442850c 100644
--- a/src/exhaustive.rs
+++ b/src/exhaustive.rs
@@ -15,9 +15,9 @@ impl<T> ExhaustiveSearch<T> {
Self(Vec::new())
}
- /// Add a new item to the index.
- pub fn push(&mut self, item: T) {
- self.0.push(item);
+ /// Iterate over the items stored in this index.
+ pub fn iter(&self) -> Iter<T> {
+ (&self).into_iter()
}
/// Get the size of this index.
@@ -29,6 +29,11 @@ impl<T> ExhaustiveSearch<T> {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
+
+ /// Add a new item to the index.
+ pub fn push(&mut self, item: T) {
+ self.0.push(item);
+ }
}
impl<T> Default for ExhaustiveSearch<T> {
@@ -64,6 +69,35 @@ impl<T> IntoIterator for ExhaustiveSearch<T> {
}
}
+/// An iterator over the values in an exhaustive index.
+#[derive(Debug)]
+pub struct Iter<'a, T>(std::slice::Iter<'a, T>);
+
+impl<'a, T> Iterator for Iter<'a, T> {
+ type Item = &'a T;
+
+ fn next(&mut self) -> Option<&'a T> {
+ self.0.next()
+ }
+}
+
+impl<'a, T> IntoIterator for &'a ExhaustiveSearch<T> {
+ type Item = &'a T;
+ type IntoIter = Iter<'a, T>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ Iter(self.0.iter())
+ }
+}
+
+impl<T> Extend<T> for ExhaustiveSearch<T> {
+ fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
+ for value in iter {
+ self.push(value);
+ }
+ }
+}
+
impl<K: Proximity<V>, V> NearestNeighbors<K, V> for ExhaustiveSearch<V> {
fn search<'k, 'v, N>(&'v self, mut neighborhood: N) -> N
where