From fdb2804883deb31e3aeb15bbe588dcc9b7b76bd0 Mon Sep 17 00:00:00 2001 From: Mica White Date: Mon, 8 Dec 2025 19:56:48 -0500 Subject: Stuff --- engine/src/lazysort.rs | 174 ++++++++++++++++++++++++------------------------- 1 file changed, 87 insertions(+), 87 deletions(-) mode change 100644 => 100755 engine/src/lazysort.rs (limited to 'engine/src/lazysort.rs') diff --git a/engine/src/lazysort.rs b/engine/src/lazysort.rs old mode 100644 new mode 100755 index f028778..9d54fe5 --- a/engine/src/lazysort.rs +++ b/engine/src/lazysort.rs @@ -1,87 +1,87 @@ -use arrayvec::ArrayVec; - -pub struct LazySort R, R: Ord, const CAPACITY: usize> { - collection: ArrayVec, - sorted: usize, - sort_by: F, -} - -pub struct LazySortIter R, R: Ord, const CAPACITY: usize> { - sorter: LazySort, - index: usize, -} - -impl R, R: Ord, const CAPACITY: usize> LazySort { - pub fn new(collection: impl IntoIterator, sort_by: F) -> Self { - Self { - collection: collection.into_iter().collect(), - sort_by, - sorted: 0, - } - } - - 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; - for i in index..self.collection.len() { - if let Some(min) = &mut min { - let res = (self.sort_by)(&self.collection[i]); - if res < *min { - *min = res; - min_index = Some(i); - } - } - } - - if let Some(min_index) = min_index { - self.collection.swap(index, min_index); - } - } - - fn sort_between(&mut self, start: usize, end: usize) { - for i in start..=end { - self.sort(i); - } - } - - pub fn get(&mut self, index: usize) -> Option<&T> { - if index >= self.sorted { - self.sort_between(self.sorted, index); - self.sorted = index; - } - - self.collection.get(index) - } -} - -impl R, R: Ord, const CAPACITY: usize> IntoIterator - for LazySort -{ - type IntoIter = LazySortIter; - type Item = T; - - fn into_iter(self) -> Self::IntoIter { - LazySortIter { - sorter: self, - index: 0, - } - } -} - -impl R, R: Ord, const CAPACITY: usize> Iterator - for LazySortIter -{ - type Item = T; - - fn next(&mut self) -> Option { - let r = self.sorter.get(self.index); - self.index += 1; - r.cloned() - } -} +use arrayvec::ArrayVec; + +pub struct LazySort R, R: Ord, const CAPACITY: usize> { + collection: ArrayVec, + sorted: usize, + sort_by: F, +} + +pub struct LazySortIter R, R: Ord, const CAPACITY: usize> { + sorter: LazySort, + index: usize, +} + +impl R, R: Ord, const CAPACITY: usize> LazySort { + pub fn new(collection: impl IntoIterator, sort_by: F) -> Self { + Self { + collection: collection.into_iter().collect(), + sort_by, + sorted: 0, + } + } + + 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; + for i in index..self.collection.len() { + if let Some(min) = &mut min { + let res = (self.sort_by)(&self.collection[i]); + if res < *min { + *min = res; + min_index = Some(i); + } + } + } + + if let Some(min_index) = min_index { + self.collection.swap(index, min_index); + } + } + + fn sort_between(&mut self, start: usize, end: usize) { + for i in start..=end { + self.sort(i); + } + } + + pub fn get(&mut self, index: usize) -> Option<&T> { + if index >= self.sorted { + self.sort_between(self.sorted, index); + self.sorted = index; + } + + self.collection.get(index) + } +} + +impl R, R: Ord, const CAPACITY: usize> IntoIterator + for LazySort +{ + type IntoIter = LazySortIter; + type Item = T; + + fn into_iter(self) -> Self::IntoIter { + LazySortIter { + sorter: self, + index: 0, + } + } +} + +impl R, R: Ord, const CAPACITY: usize> Iterator + for LazySortIter +{ + type Item = T; + + fn next(&mut self) -> Option { + let r = self.sorter.get(self.index); + self.index += 1; + r.cloned() + } +} -- cgit v1.2.3