@@ -441,6 +441,48 @@ impl<K: Hash + Eq, V, S: BuildHasher> ArcCache<K, V, S> {
441
441
}
442
442
}
443
443
444
+
445
+ pub fn get_or_insert < F > ( & mut self , k : K , f : F ) -> & V
446
+ where
447
+ F : FnOnce ( ) -> V , {
448
+ & * self . get_or_insert_mut ( k, f)
449
+ }
450
+
451
+ pub fn get_or_insert_mut < F > ( & mut self , k : K , f : F ) -> & mut V
452
+ where
453
+ F : FnOnce ( ) -> V , {
454
+
455
+ if let Some ( ( key, val) ) = self . main_lru . remove ( & k) {
456
+ self . main_lfu . insert ( key, val) ;
457
+ return self . main_lfu . get_mut_key_value ( & k) . map ( |( _, v) | v) . unwrap ( ) ;
458
+ }
459
+
460
+ if let Some ( ( key, val) ) = self . ghost_lfu . remove ( & k) {
461
+ self . main_lfu . full_increase ( ) ;
462
+ self . main_lru . full_decrease ( ) ;
463
+ self . main_lfu . insert ( key, val) ;
464
+ return self . main_lfu . get_mut_key_value ( & k) . map ( |( _, v) | v) . unwrap ( ) ;
465
+ }
466
+
467
+ if let Some ( ( key, val) ) = self . ghost_lru . remove ( & k) {
468
+ self . main_lru . full_increase ( ) ;
469
+ self . main_lfu . full_decrease ( ) ;
470
+ self . main_lru . insert ( key, val) ;
471
+ return self . main_lru . get_mut_key_value ( & k) . map ( |( _, v) | v) . unwrap ( ) ;
472
+ }
473
+
474
+ if self . main_lfu . contains_key ( & k) {
475
+ return self . main_lfu . get_mut_key_value ( & k) . map ( |( _, v) | v) . unwrap ( ) ;
476
+ }
477
+
478
+ if self . main_lru . is_full ( ) {
479
+ let ( pk, pv) = self . main_lru . pop_last ( ) . unwrap ( ) ;
480
+ self . ghost_lru . insert ( pk, pv) ;
481
+ }
482
+ self . get_or_insert_mut ( k, f)
483
+ }
484
+
485
+
444
486
/// 移除元素
445
487
///
446
488
/// ```
@@ -490,6 +532,17 @@ impl<K: Hash + Eq, V, S: BuildHasher> ArcCache<K, V, S> {
490
532
}
491
533
}
492
534
535
+
536
+ impl < K : Hash + Eq , V : Default , S : BuildHasher > ArcCache < K , V , S > {
537
+ pub fn get_or_insert_default ( & mut self , k : K ) -> & V {
538
+ & * self . get_or_insert_mut ( k, || V :: default ( ) )
539
+ }
540
+
541
+ pub fn get_or_insert_default_mut ( & mut self , k : K ) -> & mut V {
542
+ self . get_or_insert_mut ( k, || V :: default ( ) )
543
+ }
544
+ }
545
+
493
546
impl < K : Clone + Hash + Eq , V : Clone , S : Clone + BuildHasher > Clone for ArcCache < K , V , S > {
494
547
fn clone ( & self ) -> Self {
495
548
ArcCache {
@@ -739,6 +792,9 @@ where
739
792
}
740
793
}
741
794
795
+ unsafe impl < K : Send , V : Send , S : Send > Send for ArcCache < K , V , S > { }
796
+ unsafe impl < K : Sync , V : Sync , S : Sync > Sync for ArcCache < K , V , S > { }
797
+
742
798
#[ cfg( test) ]
743
799
mod tests {
744
800
use std:: collections:: hash_map:: RandomState ;
@@ -1111,19 +1167,18 @@ mod tests {
1111
1167
assert_eq ! ( a[ & 3 ] , "three" ) ;
1112
1168
}
1113
1169
1114
- // #[test]
1115
- // fn test_drain() {
1116
- // let mut a = ArcCache::new(3);
1117
- // a.insert(1, 1);
1118
- // a.insert(2, 2);
1119
- // a.insert(3, 3);
1120
-
1121
- // assert_eq!(a.len(), 3);
1122
- // {
1123
- // let mut drain = a.drain();
1124
- // assert_eq!(drain.next().unwrap(), (1, 1));
1125
- // assert_eq!(drain.next().unwrap(), (2, 2));
1126
- // }
1127
- // assert_eq!(a.len(), 0);
1128
- // }
1170
+
1171
+ #[ test]
1172
+ fn test_send ( ) {
1173
+ use std:: thread;
1174
+
1175
+ let mut cache = ArcCache :: new ( 4 ) ;
1176
+ cache. insert ( 1 , "a" ) ;
1177
+
1178
+ let handle = thread:: spawn ( move || {
1179
+ assert_eq ! ( cache. get( & 1 ) , Some ( & "a" ) ) ;
1180
+ } ) ;
1181
+
1182
+ assert ! ( handle. join( ) . is_ok( ) ) ;
1183
+ }
1129
1184
}
0 commit comments