Remove HashCache.

This commit is contained in:
Ms2ger 2016-07-07 12:17:13 +02:00
parent 24fe6bc4da
commit fa05a309f3
2 changed files with 2 additions and 64 deletions

View file

@ -4,58 +4,9 @@
use rand; use rand;
use rand::Rng; use rand::Rng;
use std::collections::HashMap; use std::hash::{Hash, Hasher, SipHasher};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::default::Default;
use std::hash::{BuildHasherDefault, Hash, Hasher, SipHasher};
use std::slice::Iter; use std::slice::Iter;
#[derive(Debug)]
pub struct HashCache<K, V>
where K: PartialEq + Eq + Hash,
V: Clone,
{
entries: HashMap<K, V, BuildHasherDefault<SipHasher>>,
}
impl<K, V> HashCache<K, V>
where K: PartialEq + Eq + Hash,
V: Clone,
{
pub fn new() -> HashCache<K, V> {
HashCache {
entries: HashMap::with_hasher(Default::default()),
}
}
pub fn insert(&mut self, key: K, value: V) {
self.entries.insert(key, value);
}
pub fn find(&self, key: &K) -> Option<V> {
match self.entries.get(key) {
Some(v) => Some(v.clone()),
None => None,
}
}
pub fn find_or_create<F>(&mut self, key: K, mut blk: F) -> V where F: FnMut() -> V {
match self.entries.entry(key) {
Occupied(occupied) => {
(*occupied.get()).clone()
}
Vacant(vacant) => {
(*vacant.insert(blk())).clone()
}
}
}
pub fn evict_all(&mut self) {
self.entries.clear();
}
}
pub struct LRUCache<K, V> { pub struct LRUCache<K, V> {
entries: Vec<(K, V)>, entries: Vec<(K, V)>,
cache_size: usize, cache_size: usize,

View file

@ -3,20 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::cell::Cell; use std::cell::Cell;
use util::cache::{HashCache, LRUCache}; use util::cache::LRUCache;
#[test]
fn test_hashcache() {
let mut cache: HashCache<usize, Cell<&str>> = HashCache::new();
cache.insert(1, Cell::new("one"));
assert!(cache.find(&1).is_some());
assert!(cache.find(&2).is_none());
cache.find_or_create(2, || { Cell::new("two") });
assert!(cache.find(&1).is_some());
assert!(cache.find(&2).is_some());
}
#[test] #[test]
fn test_lru_cache() { fn test_lru_cache() {