-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move BoundedVecDeque to aptos-collections
- Loading branch information
1 parent
634e70a
commit 012db41
Showing
7 changed files
with
117 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "aptos-collections" | ||
description = "Aptos Collections Library" | ||
version = "0.1.0" | ||
|
||
# Workspace inherited keys | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
publish = { workspace = true } | ||
repository = { workspace = true } | ||
rust-version = { workspace = true } | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::collections::{vec_deque, VecDeque}; | ||
|
||
pub struct BoundedVecDeque<T> { | ||
inner: VecDeque<T>, | ||
capacity: usize, | ||
} | ||
|
||
impl<T> BoundedVecDeque<T> { | ||
pub fn new(capacity: usize) -> Self { | ||
assert!(capacity > 0); | ||
Self { | ||
inner: VecDeque::with_capacity(capacity), | ||
capacity, | ||
} | ||
} | ||
|
||
pub fn is_full(&self) -> bool { | ||
self.inner.len() == self.capacity | ||
} | ||
|
||
pub fn push_back(&mut self, item: T) -> Option<T> { | ||
let oldest = if self.is_full() { | ||
self.inner.pop_front() | ||
} else { | ||
None | ||
}; | ||
|
||
self.inner.push_back(item); | ||
assert!(self.inner.len() <= self.capacity); | ||
oldest | ||
} | ||
|
||
pub fn iter(&self) -> vec_deque::Iter<'_, T> { | ||
self.inner.iter() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::BoundedVecDeque; | ||
|
||
#[test] | ||
fn test_bounded_vec_deque_capacity() { | ||
let capacity = 10; | ||
let mut queue = BoundedVecDeque::new(capacity); | ||
for i in 0..capacity { | ||
queue.push_back(i); | ||
} | ||
|
||
assert!(queue.is_full()); | ||
|
||
assert_eq!(queue.push_back(capacity), Some(0)); | ||
} | ||
|
||
#[test] | ||
fn test_bounded_vec_deque_iter() { | ||
let capacity = 10; | ||
let mut queue = BoundedVecDeque::new(capacity); | ||
for i in 0..capacity { | ||
queue.push_back(i); | ||
} | ||
|
||
for (i, item) in queue.iter().enumerate() { | ||
assert_eq!(i, *item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod bounded_vec_deque; | ||
|
||
pub use bounded_vec_deque::BoundedVecDeque; |