Lazily compute common style affecting attribute info.

This commit is contained in:
Emilio Cobos Álvarez 2016-08-11 16:44:21 -07:00
parent 51b6568273
commit a46f0c3b24
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
2 changed files with 26 additions and 16 deletions

View file

@ -7,7 +7,7 @@
use rand;
use rand::Rng;
use std::hash::{Hash, Hasher, SipHasher};
use std::slice::Iter;
use std::slice::{Iter, IterMut};
pub struct LRUCache<K, V> {
entries: Vec<(K, V)>,
@ -17,7 +17,7 @@ pub struct LRUCache<K, V> {
impl<K: PartialEq, V: Clone> LRUCache<K, V> {
pub fn new(size: usize) -> LRUCache<K, V> {
LRUCache {
entries: vec!(),
entries: vec![],
cache_size: size,
}
}
@ -36,6 +36,10 @@ impl<K: PartialEq, V: Clone> LRUCache<K, V> {
self.entries.iter()
}
pub fn iter_mut(&mut self) -> IterMut<(K, V)> {
self.entries.iter_mut()
}
pub fn insert(&mut self, key: K, val: V) {
if self.entries.len() == self.cache_size {
self.entries.remove(0);