Skip to content

Commit 1a97f4d

Browse files
committed
Sorting is now time sensitive
1 parent 5f8d6cc commit 1a97f4d

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/entries/frequency.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp::Ordering;
12
use std::collections::HashMap;
23
use std::env;
34
use std::ops::{Deref, DerefMut};
@@ -50,7 +51,7 @@ impl DerefMut for Frequencies {
5051
}
5152
}
5253

53-
#[derive(Serialize, Deserialize)]
54+
#[derive(Clone, Serialize, Deserialize, Eq)]
5455
pub(super) struct EntryFrequency {
5556
score: u32,
5657
last_used: SystemTime,
@@ -88,3 +89,33 @@ impl EntryFrequency {
8889
self.last_used = SystemTime::now();
8990
}
9091
}
92+
93+
impl Default for EntryFrequency {
94+
fn default() -> Self {
95+
Self {
96+
score: 0,
97+
last_used: SystemTime::UNIX_EPOCH,
98+
}
99+
}
100+
}
101+
102+
impl Ord for EntryFrequency {
103+
fn cmp(&self, other: &Self) -> Ordering {
104+
match self.score().cmp(&other.score()) {
105+
Ordering::Equal => self.last_used.cmp(&other.last_used),
106+
order => order,
107+
}
108+
}
109+
}
110+
111+
impl PartialOrd for EntryFrequency {
112+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
113+
Some(self.cmp(other))
114+
}
115+
}
116+
117+
impl PartialEq for EntryFrequency {
118+
fn eq(&self, other: &Self) -> bool {
119+
self.cmp(other).is_eq()
120+
}
121+
}

src/entries/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@ impl SearchEntries {
7979
}
8080

8181
pub fn sort_by_frequency(&mut self) {
82-
self.entries.sort_by_key(|e| {
83-
Reverse(match self.frequencies.get(e.id()) {
84-
Some(frequency) => frequency.score(),
85-
None => 0,
86-
})
82+
self.entries.sort_by_cached_key(|e| {
83+
Reverse(self.frequencies.get(e.id()).cloned().unwrap_or_default())
8784
});
8885
}
8986

0 commit comments

Comments
 (0)