Skip to content

Commit

Permalink
Merge pull request #4 from arindas/develop
Browse files Browse the repository at this point in the history
feat: improves documentation and tests on arena
  • Loading branch information
arindas authored Aug 22, 2023
2 parents 6265503 + 10a5be7 commit 4be7968
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT"
keywords = ["cache", "lru-cache", "generational-arena", "no-std"]
categories = ["algorithms", "caching"]
exclude = [".github/"]
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Generational Arena based cache impls. in 100% safe, [no_std] compatible Rust.

```toml
[dependencies]
generational-cache = "0.1.0"
generational-cache = "0.1.1"
```

Refer to latest git [API Documentation](https://arindas.github.io/generational-cache/docs/generational_cache/)
Expand Down Expand Up @@ -98,4 +98,6 @@ match cache.insert(0, 0) {
(… we plan on adding more cache implementations in the future).

## License
This repository is licensed under the MIT License. See [LICENSE](./LICENSE) for more details.
This repository is licensed under the MIT License. See
[License](https://raw.githubusercontent.com/arindas/generational-cache/main/LICENSE)
for more details.
29 changes: 27 additions & 2 deletions src/arena/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,34 @@ impl<T> Default for Entry<T> {
///
/// This is inspired from the crate
/// ["generational-arena"](https://docs.rs/generational-arena)
///
/// ## Usage
/// ```
/// #[no_std]
///
/// use generational_cache::prelude::*;
///
/// const CAPACITY: usize = 5;
///
/// let mut arena = Arena::<_, i32>::with_vector(Array::<_, CAPACITY>::new());
/// let index = arena.insert(78).unwrap(); // allocate new element in arena
/// let i_ref = arena.get(&index);
/// assert_eq!(i_ref, Some(&78));
/// let i_m_ref = arena.get_mut(&index).unwrap();
/// *i_m_ref = -68418;
/// assert_eq!(arena.get(&index), Some(&-68418));
///
/// arena.remove(&index).unwrap();
///
/// assert!(arena.get(&index).is_none());
/// ```
pub struct Arena<V, T> {
entries_vec: V,
generation: u64,
free_list_head: Option<usize>,

len: usize,
capacity: usize,

_phantom_type: PhantomData<T>,
}
Expand Down Expand Up @@ -92,7 +114,7 @@ where

self.entries_vec.clear();

let capacity = self.entries_vec.capacity();
let capacity = self.capacity();

for i in 0..capacity {
let next_free_idx = i + 1;
Expand All @@ -112,11 +134,14 @@ where
}

pub fn with_vector(vector: V) -> Self {
let capacity = vector.capacity();

let mut arena = Self {
entries_vec: vector,
generation: 0,
free_list_head: Some(0),
len: 0,
capacity,
_phantom_type: PhantomData,
};

Expand Down Expand Up @@ -207,7 +232,7 @@ where
}

pub fn capacity(&self) -> usize {
self.entries_vec.capacity()
self.capacity
}

pub fn is_empty(&self) -> bool {
Expand Down
7 changes: 7 additions & 0 deletions src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ pub mod tests {
if cap_0 == cap_1 {
assert!(res.is_err());
} else {
assert!(cap_1 > cap_0, "Capacity decreased on push().");
assert!(res.is_ok());
}

let cap_0 = vector.capacity();

vector.clear();

let cap_1 = vector.capacity();

assert!(cap_0 == cap_1, "Capacity changed on clear().");

assert!(vector.is_empty());
}
}

0 comments on commit 4be7968

Please sign in to comment.