summaryrefslogtreecommitdiff
path: root/engine/src/lazysort.rs
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2025-12-08 19:56:48 -0500
committerMica White <botahamec@outlook.com>2025-12-08 19:56:48 -0500
commitfdb2804883deb31e3aeb15bbe588dcc9b7b76bd0 (patch)
treea4c51cd88664cab7b6673dcd3b425fb7a0202a38 /engine/src/lazysort.rs
parent93461aa9399d981db7d8611b3eb166636de4d971 (diff)
Diffstat (limited to 'engine/src/lazysort.rs')
-rwxr-xr-x[-rw-r--r--]engine/src/lazysort.rs174
1 files changed, 87 insertions, 87 deletions
diff --git a/engine/src/lazysort.rs b/engine/src/lazysort.rs
index f028778..9d54fe5 100644..100755
--- a/engine/src/lazysort.rs
+++ b/engine/src/lazysort.rs
@@ -1,87 +1,87 @@
-use arrayvec::ArrayVec;
-
-pub struct LazySort<T: Clone, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> {
- collection: ArrayVec<T, CAPACITY>,
- sorted: usize,
- sort_by: F,
-}
-
-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, const CAPACITY: usize> LazySort<T, F, R, CAPACITY> {
- pub fn new(collection: impl IntoIterator<Item = T>, 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<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;
- 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<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 {
- LazySortIter {
- sorter: self,
- index: 0,
- }
- }
-}
-
-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> {
- let r = self.sorter.get(self.index);
- self.index += 1;
- r.cloned()
- }
-}
+use arrayvec::ArrayVec;
+
+pub struct LazySort<T: Clone, F: Fn(&T) -> R, R: Ord, const CAPACITY: usize> {
+ collection: ArrayVec<T, CAPACITY>,
+ sorted: usize,
+ sort_by: F,
+}
+
+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, const CAPACITY: usize> LazySort<T, F, R, CAPACITY> {
+ pub fn new(collection: impl IntoIterator<Item = T>, 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<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;
+ 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<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 {
+ LazySortIter {
+ sorter: self,
+ index: 0,
+ }
+ }
+}
+
+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> {
+ let r = self.sorter.get(self.index);
+ self.index += 1;
+ r.cloned()
+ }
+}