File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use std:: cmp:: Ordering ;
12use std:: collections:: HashMap ;
23use std:: env;
34use 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 ) ]
5455pub ( 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+ }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments