@@ -43,7 +43,6 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
43
43
. map ( |f| f. to_owned ( ) )
44
44
. collect ( ) ;
45
45
counts. sort_unstable ( ) ;
46
- // panic!("{:?}", counts);
47
46
48
47
// Get the value under each key
49
48
for count in counts. iter ( ) . rev ( ) {
@@ -61,13 +60,13 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
61
60
}
62
61
63
62
/// Remove an item from the internal order store
64
- fn purge_from_order ( & mut self , item : & T , count : & u64 ) {
65
- if let Some ( order) = self . order . get_mut ( count) {
63
+ fn purge_from_order ( & mut self , item : & T , count : u64 ) {
64
+ if let Some ( order) = self . order . get_mut ( & count) {
66
65
// If there was data there, remove the existing item
67
66
if !order. is_empty ( ) {
68
67
order. retain ( |i| i != item) ;
69
68
if order. is_empty ( ) {
70
- self . order . remove ( count) ;
69
+ self . order . remove ( & count) ;
71
70
}
72
71
} ;
73
72
} ;
@@ -79,7 +78,7 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
79
78
80
79
/// Update the internal item order HashMap
81
80
fn update_order ( & mut self , item : T , old_count : & u64 , new_count : & u64 ) {
82
- self . purge_from_order ( & item, old_count) ;
81
+ self . purge_from_order ( & item, * old_count) ;
83
82
match self . order . get_mut ( new_count) {
84
83
Some ( v) => {
85
84
v. push ( item) ;
@@ -92,7 +91,7 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
92
91
93
92
/// Increment an item into the counter, creating if it does not exist
94
93
fn increment ( & mut self , item : T ) {
95
- let old_count = self . state . get ( & item) . unwrap_or ( & 0 ) . to_owned ( ) ;
94
+ let old_count = * self . state . get ( & item) . unwrap_or ( & 0 ) ;
96
95
let new_count = old_count. checked_add ( 1 ) ;
97
96
match new_count {
98
97
Some ( count) => self . state . insert ( item. to_owned ( ) , count) ,
@@ -103,7 +102,7 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
103
102
104
103
/// Reduce an item from the counter, removing if it becomes 0
105
104
fn decrement ( & mut self , item : T ) {
106
- let old_count = self . state . get ( & item) . unwrap_or ( & 0 ) . to_owned ( ) ;
105
+ let old_count = * self . state . get ( & item) . unwrap_or ( & 0 ) ;
107
106
let new_count = old_count. checked_sub ( 1 ) ;
108
107
match new_count {
109
108
Some ( count) => {
@@ -122,10 +121,10 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
122
121
123
122
/// Remove an item from the counter completely
124
123
fn delete ( & mut self , item : & T ) {
125
- // TODO: Make this safe
126
- let count = self . state . get ( item) . unwrap ( ) . to_owned ( ) ;
127
- self . purge_from_order ( item, & count ) ;
128
- self . purge_from_state ( item ) ;
124
+ if let Some ( count ) = self . state . get ( item ) {
125
+ self . purge_from_order ( item, * count ) ;
126
+ self . purge_from_state ( item) ;
127
+ }
129
128
}
130
129
}
131
130
0 commit comments