From 655e896fb57ede428f889c6c7598f8954784e0dd Mon Sep 17 00:00:00 2001 From: Micha White Date: Thu, 21 Dec 2023 19:31:09 -0500 Subject: Remove some memory allocations --- engine/src/lazysort.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'engine/src/lazysort.rs') 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 R, R: Ord> { - collection: Box<[T]>, +use crate::stackvec::StackVec; + +pub struct LazySort R, R: Ord, const CAPACITY: usize> { + collection: StackVec, sorted: usize, sort_by: F, } -pub struct LazySortIter R, R: Ord> { - sorter: LazySort, +pub struct LazySortIter R, R: Ord, const CAPACITY: usize> { + sorter: LazySort, index: usize, } -impl R, R: Ord> LazySort { - pub fn new(collection: &[T], sort_by: F) -> Self { +impl R, R: Ord, const CAPACITY: usize> LazySort { + pub fn new(collection: impl IntoIterator, 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 R, R: Ord, const CAPACITY: usize> LazySort { fn sort(&mut self, index: usize) { let mut min: Option = None; let mut min_index = None; @@ -56,8 +59,10 @@ impl R, R: Ord> LazySort { } } -impl R, R: Ord> IntoIterator for LazySort { - type IntoIter = LazySortIter; +impl R, R: Ord, const CAPACITY: usize> IntoIterator + for LazySort +{ + type IntoIter = LazySortIter; type Item = T; fn into_iter(self) -> Self::IntoIter { @@ -68,7 +73,9 @@ impl R, R: Ord> IntoIterator for LazySort { } } -impl R, R: Ord> Iterator for LazySortIter { +impl R, R: Ord, const CAPACITY: usize> Iterator + for LazySortIter +{ type Item = T; fn next(&mut self) -> Option { -- cgit v1.2.3