Skip to content

Commit

Permalink
Added peek and contains functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Froelich committed Jan 2, 2017
1 parent 2989b71 commit 1c1e2b9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v0.1.2](https://github.com/jeromefroe/lru-rs/tree/0.1.2) - 2017-01-02

* Add `peek` and `contains` functions.

## [v0.1.1](https://github.com/jeromefroe/lru-rs/tree/0.1.1) - 2016-12-31

* Fix documentation link in Cargo.toml.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lru"
version = "0.1.1"
version = "0.1.2"
authors = ["Jerome Froelich <jeromefroelic@hotmail.com>"]
description = "A LRU cache implementation"
homepage = "https://github.com/jeromefroe/lru-rs"
Expand Down
87 changes: 82 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
}

/// Return the value corresponding to the key in the cache or `None` if it is not
/// present in the cache.
/// present in the cache. Moves the key to the head of the LRU list if it exists.
///
/// # Example
///
Expand All @@ -207,6 +207,9 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
None => (None, None),
Some(node) => {
let node_ptr: *mut LruEntry<K, V> = &mut **node;
// we need to use node_ptr to get a reference to val here because
// detach and attach require a mutable reference to self here which
// would be disallowed if we set value equal to &node.val
(Some(node_ptr), Some(unsafe { &(*node_ptr).val }))
}
};
Expand All @@ -222,6 +225,52 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
value
}

/// Return the value corresponding to the key in the cache or `None` if it is not
/// present in the cache. Unlike `get`, `peek` does not update the LRU list so key's
/// position will be unchanged.
///
/// # Example
///
/// ```
/// use lru::LruCache;
/// let mut cache = LruCache::new(2);
///
/// cache.put(1, "a");
/// cache.put(2, "b");
///
/// assert_eq!(cache.peek(&1), Some(&"a"));
/// assert_eq!(cache.peek(&2), Some(&"b"));
/// ```
pub fn peek<'a>(&'a self, k: &K) -> Option<&'a V> {
let key = KeyRef { k: k };
match self.map.get(&key) {
None => None,
Some(node) => Some(&node.val),
}
}

/// Return a bool indicating whether the given key is in the cache. Does not update the
/// LRU list.
///
/// # Example
///
/// ```
/// use lru::LruCache;
/// let mut cache = LruCache::new(2);
///
/// cache.put(1, "a");
/// cache.put(2, "b");
/// cache.put(3, "c");
///
/// assert!(!cache.contains(&1));
/// assert!(cache.contains(&2));
/// assert!(cache.contains(&3));
/// ```
pub fn contains(&self, k: &K) -> bool {
let key = KeyRef { k: k };
self.map.contains_key(&key)
}

/// Remove and return the value corresponding to the key from the cache or
/// `None` if it does not exist.
///
Expand Down Expand Up @@ -348,8 +397,6 @@ mod tests {

#[test]
fn test_put_update() {
println!("testing put update");

let mut cache = LruCache::new(1);

cache.put("apple".to_string(), "red".to_string());
Expand All @@ -360,7 +407,7 @@ mod tests {
}

#[test]
fn test_remove_oldest() {
fn test_put_removes_oldest() {
let mut cache = LruCache::new(2);

cache.put("apple".to_string(), "red".to_string());
Expand All @@ -380,7 +427,37 @@ mod tests {
}

#[test]
fn test_remove() {
fn test_peek() {
let mut cache: LruCache<String, String> = LruCache::new(2);

cache.put("apple".to_string(), "red".to_string());
cache.put("banana".to_string(), "yellow".to_string());

assert_opt_eq(cache.peek(&"banana".to_string()), "yellow".to_string());
assert_opt_eq(cache.peek(&"apple".to_string()), "red".to_string());

cache.put("pear".to_string(), "green".to_string());

assert!(cache.peek(&"apple".to_string()).is_none());
assert_opt_eq(cache.peek(&"banana".to_string()), "yellow".to_string());
assert_opt_eq(cache.peek(&"pear".to_string()), "green".to_string());
}

#[test]
fn test_contains() {
let mut cache = LruCache::new(2);

cache.put("apple".to_string(), "red".to_string());
cache.put("banana".to_string(), "yellow".to_string());
cache.put("pear".to_string(), "green".to_string());

assert!(!cache.contains(&"apple".to_string()));
assert!(cache.contains(&"banana".to_string()));
assert!(cache.contains(&"pear".to_string()));
}

#[test]
fn test_pop() {
let mut cache = LruCache::new(2);

cache.put("apple".to_string(), "red".to_string());
Expand Down

0 comments on commit 1c1e2b9

Please sign in to comment.