Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the cache module out of util. #12311

Merged
merged 4 commits into from Jul 8, 2016
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Remove HashCache.

  • Loading branch information
Ms2ger committed Jul 8, 2016
commit fa05a309f3a1a371ca0f5f8df92ae97652d04f0e
@@ -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() {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.