Skip to content

Draft: const generics version of uluru #17

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ readme = "README.md"
name = "uluru"
path = "lib.rs"

[dependencies]
arrayvec = { version = "0.5", default-features = false } # public dependency
[dependencies.arrayvec]
git = "https://github.com/mbrubeck/arrayvec"
branch = "min-const-gen"
default-features = false
features = ["unstable-const-generics"]

[dev-dependencies]
quickcheck = "0.9"
Expand Down
42 changes: 19 additions & 23 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

extern crate arrayvec;

use arrayvec::{Array, ArrayVec};
use arrayvec::r#const::ArrayVec;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be arrayvec::ArrayVec when merged I think.


use core::fmt;

Expand All @@ -40,15 +40,15 @@ mod tests;
/// # Example
///
/// ```
/// use uluru::{LRUCache, Entry};
/// use uluru::LRUCache;
///
/// struct MyValue {
/// id: u32,
/// name: &'static str,
/// }
///
/// // A cache with a capacity of three.
/// type MyCache = LRUCache<[Entry<MyValue>; 3]>;
/// type MyCache = LRUCache<MyValue, 3>;
///
/// // Create an empty cache, then insert some items.
/// let mut cache = MyCache::default();
Expand All @@ -67,11 +67,11 @@ mod tests;
/// cache.insert(MyValue { id: 4, name: "Mars" });
/// assert!(cache.find(|x| x.id == 2).is_none());
/// ```
pub struct LRUCache<A: Array> {
pub struct LRUCache<T, const N: usize> {
/// The most-recently-used entry is at index `head`. The entries form a linked list, linked to
/// each other by indices within the `entries` array. After an entry is added to the array,
/// its index never changes, so these links are never invalidated.
entries: ArrayVec<A>,
entries: ArrayVec<Entry<T>, N>,
/// Index of the first entry. If the cache is empty, ignore this field.
head: u16,
/// Index of the last entry. If the cache is empty, ignore this field.
Expand All @@ -80,15 +80,15 @@ pub struct LRUCache<A: Array> {

/// An entry in an LRUCache.
#[derive(Debug, Clone)]
pub struct Entry<T> {
struct Entry<T> {
val: T,
/// Index of the previous entry. If this entry is the head, ignore this field.
prev: u16,
/// Index of the next entry. If this entry is the tail, ignore this field.
next: u16,
}

impl<A: Array> Default for LRUCache<A> {
impl<T, const N: usize> Default for LRUCache<T, N> {
fn default() -> Self {
let cache = LRUCache {
entries: ArrayVec::new(),
Expand All @@ -103,10 +103,7 @@ impl<A: Array> Default for LRUCache<A> {
}
}

impl<T, A> LRUCache<A>
where
A: Array<Item = Entry<T>>,
{
impl<T, const N: usize> LRUCache<T, N> {
/// Returns the number of elements in the cache.
pub fn num_entries(&self) -> usize {
self.entries.len()
Expand Down Expand Up @@ -205,7 +202,7 @@ where
}

/// Iterate mutably over the contents of this cache.
fn iter_mut(&mut self) -> IterMut<A> {
fn iter_mut(&mut self) -> IterMut<T, N> {
IterMut {
pos: self.head,
cache: self,
Expand Down Expand Up @@ -263,9 +260,9 @@ where
}
}

impl<T, A> Clone for LRUCache<A>
/* TODO
impl<T, const N: usize> Clone for LRUCache<T, N>
where
A: Array<Item = Entry<T>>,
T: Clone,
{
fn clone(&self) -> Self {
Expand All @@ -276,10 +273,11 @@ where
}
}
}
*/

impl<T, A> fmt::Debug for LRUCache<A>
/* TODO
impl<T, const N: usize> fmt::Debug for LRUCache<T, N>
where
A: Array<Item = Entry<T>>,
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -290,23 +288,21 @@ where
.finish()
}
}
*/

/// Mutable iterator over values in an LRUCache, from most-recently-used to least-recently-used.
struct IterMut<'a, A: 'a + Array> {
cache: &'a mut LRUCache<A>,
struct IterMut<'a, T, const N: usize> {
cache: &'a mut LRUCache<T, N>,
pos: u16,
}

impl<'a, A, T> IterMut<'a, A>
where
A: Array<Item = Entry<T>>,
{
impl<'a, T, const N: usize> IterMut<'a, T, N> {
fn next(&mut self) -> Option<(u16, &mut T)> {
let index = self.pos;
let entry = self.cache.entries.get_mut(index as usize)?;

self.pos = if index == self.cache.tail {
A::CAPACITY as u16 // Point past the end of the array to signal we are done.
N as u16 // Point past the end of the array to signal we are done.
} else {
entry.next
};
Expand Down
5 changes: 2 additions & 3 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ extern crate std;
use self::std::vec::Vec;
use super::*;

type TestCache = LRUCache<[Entry<i32>; 4]>;
type TestCache = LRUCache<i32, 4>;

/// Convenience function for test assertions
fn items<T, A>(cache: &mut LRUCache<A>) -> Vec<T>
fn items<T, const N: usize>(cache: &mut LRUCache<T, N>) -> Vec<T>
where
T: Clone,
A: Array<Item = Entry<T>>,
{
let mut v = Vec::new();
let mut iter = cache.iter_mut();
Expand Down