summaryrefslogtreecommitdiff
path: root/engine/src/lazysort.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2023-12-21 19:31:09 -0500
committerMicha White <botahamec@outlook.com>2023-12-21 19:31:09 -0500
commit655e896fb57ede428f889c6c7598f8954784e0dd (patch)
tree1b9bfbe379ad99acd285ef7997d1ebe4bb3f3f30 /engine/src/lazysort.rs
parent0185f2ef9159a868afa03c6e8d76f6d77f52f4f9 (diff)
Remove some memory allocations
Diffstat (limited to 'engine/src/lazysort.rs')
-rw-r--r--engine/src/lazysort.rs33
1 files changed, 20 insertions, 13 deletions
diff --git a/engine/src/lazysort.rs b/engine/src/lazysort.rs
index bec372c..ba32ee8 100644
--- a/engine/src/lazysort.rs
+++ b/engine/src/lazysort.rs
@@ -1,28 +1,31 @@
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct LazySort<T, F: Fn(&T) -> R, R: Ord> {
- collection: Box<[T]>,
+use crate::stackvec::StackVec;
+
+pub struct LazySort<T: Clone, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> {
+ collection: StackVec<T, CAPACITY>,
sorted: usize,
sort_by: F,
}
-pub struct LazySortIter<T, F: Fn(&T) -> R, R: Ord> {
- sorter: LazySort<T, F, R>,
+pub struct LazySortIter<T: Clone, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> {
+ sorter: LazySort<T, F, R, CAPACITY>,
index: usize,
}
-impl<T: Clone, F: Fn(&T) -> R, R: Ord> LazySort<T, F, R> {
- pub fn new(collection: &[T], sort_by: F) -> Self {
+impl<T: Clone, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> LazySort<T, F, R, CAPACITY> {
+ pub fn new(collection: impl IntoIterator<Item = T>, sort_by: F) -> Self {
Self {
- collection: collection.into(),
+ collection: collection.into_iter().collect(),
sort_by,
sorted: 0,
}
}
- fn len(&self) -> usize {
- self.collection.len()
+ pub fn is_empty(&self) -> bool {
+ self.collection.is_empty()
}
+}
+impl<T: Clone, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> LazySort<T, F, R, CAPACITY> {
fn sort(&mut self, index: usize) {
let mut min: Option<R> = None;
let mut min_index = None;
@@ -56,8 +59,10 @@ impl<T: Clone, F: Fn(&T) -> R, R: Ord> LazySort<T, F, R> {
}
}
-impl<T: Copy, F: Fn(&T) -> R, R: Ord> IntoIterator for LazySort<T, F, R> {
- type IntoIter = LazySortIter<T, F, R>;
+impl<T: Copy, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> IntoIterator
+ for LazySort<T, F, R, CAPACITY>
+{
+ type IntoIter = LazySortIter<T, F, R, CAPACITY>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
@@ -68,7 +73,9 @@ impl<T: Copy, F: Fn(&T) -> R, R: Ord> IntoIterator for LazySort<T, F, R> {
}
}
-impl<T: Copy, F: Fn(&T) -> R, R: Ord> Iterator for LazySortIter<T, F, R> {
+impl<T: Copy, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> Iterator
+ for LazySortIter<T, F, R, CAPACITY>
+{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {