mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Remove HashCache.
This commit is contained in:
parent
24fe6bc4da
commit
fa05a309f3
2 changed files with 2 additions and 64 deletions
|
@ -4,58 +4,9 @@
|
|||
|
||||
use rand;
|
||||
use rand::Rng;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
||||
use std::default::Default;
|
||||
use std::hash::{BuildHasherDefault, Hash, Hasher, SipHasher};
|
||||
use std::hash::{Hash, Hasher, SipHasher};
|
||||
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> {
|
||||
entries: Vec<(K, V)>,
|
||||
cache_size: usize,
|
||||
|
|
|
@ -3,20 +3,7 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::cell::Cell;
|
||||
use util::cache::{HashCache, 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());
|
||||
}
|
||||
use util::cache::LRUCache;
|
||||
|
||||
#[test]
|
||||
fn test_lru_cache() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue