diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-18 13:04:28 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2021-05-18 13:04:28 -0400 |
commit | 365024d024c0a12cbf502dfc893057ad8e425003 (patch) | |
tree | 6a3e7c68fe81515bf1e2040ab2881a447f2977df /src | |
parent | 209996f53b08cd333e47f932b62fdd1d6d861c3f (diff) |
rework new sorting algorithm
- add default instantiation so we don't have to create
the linked list every time
- use a VecDeque instead of LinkedList
- no real reason for this
Diffstat (limited to 'src')
-rw-r--r-- | src/config/default/sort.rs | 12 | ||||
-rw-r--r-- | src/util/sort.rs | 87 |
2 files changed, 47 insertions, 52 deletions
diff --git a/src/config/default/sort.rs b/src/config/default/sort.rs index bd4c12e..0768c98 100644 --- a/src/config/default/sort.rs +++ b/src/config/default/sort.rs @@ -1,7 +1,6 @@ use serde_derive::Deserialize; use crate::util::sort; -use crate::util::sort::SortType; const fn default_true() -> bool { true @@ -26,20 +25,13 @@ impl SortRawOption { None => sort::SortType::Natural, }; - let mut sort_methods = std::collections::LinkedList::new(); - sort_methods.push_back(sort_method); - sort_methods.push_back(SortType::Natural); - sort_methods.push_back(SortType::Lexical); - sort_methods.push_back(SortType::Size); - sort_methods.push_back(SortType::Mtime); - sort_methods.push_back(SortType::Ext); + let mut sort_methods = sort::SortTypes::default(); + sort_methods.reorganize(sort_method); - let sort_methods = sort::SortTypes { list: sort_methods }; sort::SortOption { directories_first: self.directories_first, case_sensitive: self.case_sensitive, reverse: self.reverse, - sort_method, sort_methods, } } diff --git a/src/util/sort.rs b/src/util/sort.rs index d1a286a..cbe01ee 100644 --- a/src/util/sort.rs +++ b/src/util/sort.rs @@ -1,4 +1,5 @@ use std::cmp; +use std::collections::VecDeque; use std::fs; use std::time; @@ -6,33 +7,6 @@ use serde_derive::Deserialize; use crate::fs::JoshutoDirEntry; -#[derive(Clone, Debug)] -pub struct SortTypes { - pub list: std::collections::LinkedList<SortType>, -} - -impl SortTypes { - pub fn reorganize(&mut self, st: SortType) { - self.list.push_front(st); - self.list.pop_back(); - } - - pub fn cmp( - &self, - f1: &JoshutoDirEntry, - f2: &JoshutoDirEntry, - sort_option: &SortOption, - ) -> cmp::Ordering { - for st in &self.list { - let res = st.cmp(f1, f2, sort_option); - if res != cmp::Ordering::Equal { - return res; - } - } - cmp::Ordering::Equal - } -} - #[derive(Clone, Copy, Debug, Deserialize, PartialEq)] pub enum SortType { Lexical, @@ -86,11 +60,53 @@ impl std::fmt::Display for SortType { } #[derive(Clone, Debug)] +pub struct SortTypes { + pub list: VecDeque<SortType>, +} + +impl SortTypes { + pub fn reorganize(&mut self, st: SortType) { + self.list.push_front(st); + self.list.pop_back(); + } + + pub fn cmp( + &self, + f1: &JoshutoDirEntry, + f2: &JoshutoDirEntry, + sort_option: &SortOption, + ) -> cmp::Ordering { + for st in &self.list { + let res = st.cmp(f1, f2, sort_option); + if res != cmp::Ordering::Equal { + return res; + } + } + cmp::Ordering::Equal + } +} + +impl std::default::Default for SortTypes { + fn default() -> Self { + let list: VecDeque<SortType> = vec![ + SortType::Natural, + SortType::Lexical, + SortType::Size, + SortType::Ext, + SortType::Mtime, + ] + .into_iter() + .collect(); + + Self { list } + } +} + +#[derive(Clone, Debug)] pub struct SortOption { pub directories_first: bool, pub case_sensitive: bool, pub reverse: bool, - pub sort_method: SortType, pub sort_methods: SortTypes, } @@ -126,18 +142,11 @@ impl SortOption { impl std::default::Default for SortOption { fn default() -> Self { - let mut sort_methods = std::collections::LinkedList::new(); - sort_methods.push_back(SortType::Ext); - sort_methods.push_back(SortType::Size); - sort_methods.push_back(SortType::Mtime); - sort_methods.push_back(SortType::Lexical); - sort_methods.push_back(SortType::Natural); SortOption { directories_first: true, case_sensitive: false, reverse: false, - sort_method: SortType::Natural, - sort_methods: SortTypes { list: sort_methods }, + sort_methods: SortTypes::default(), } } } @@ -153,12 +162,6 @@ fn mtime_sort(file1: &JoshutoDirEntry, file2: &JoshutoDirEntry) -> cmp::Ordering let f1_mtime: time::SystemTime = f1_meta.modified()?; let f2_mtime: time::SystemTime = f2_meta.modified()?; Ok(f1_mtime.cmp(&f2_mtime)) - - // Ok(if f1_mtime >= f2_mtime { - // cmp::Ordering::Less - // } else { - // cmp::Ordering::Greater - // }) } compare(&file1, &file2).unwrap_or(cmp::Ordering::Equal) } |