1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
//! # Timed Queue
//!
//! A queue that drops its content after a given amount of time.
//!
//! ## Crate Features
//!
//! * `vecdeque` - Uses a `VecDeque` as the underlying data structure. Enabled by default.
//! * `doublestack` - Uses two stacks (`Vec`) as the underlying data structure. Mutually exclusive with `vecdeque`.
//! * `tokio` - Uses [`tokio::time::Instant`] instead of [`std::time::Instant`].
//!
//! ## Example
//!
//! To implement an FPS counter, you could use the following technique:
//!
//! ```
//! # use std::thread;
//! # use std::time::Duration;
//! # use ttl_queue::TtlQueue;
//! let mut fps_counter = TtlQueue::new(Duration::from_secs_f64(1.0));
//!
//! for i in 0..100 {
//! // Register a new frame and return the number of frames observed
//! // within the last second.
//! let fps = fps_counter.refresh_and_push_back(());
//! debug_assert!(fps >= 1);
//!
//! // Sleep 10 ms to achieve a ~100 Hz frequency.
//! thread::sleep(Duration::from_millis(10));
//! }
//!
//! let fps = fps_counter.refresh();
//! debug_assert!(fps >= 95 && fps <= 105);
//! ```
use std::time::Duration;
#[cfg(not(feature = "tokio"))]
use std::time::Instant;
#[cfg(feature = "tokio")]
use tokio::time::Instant;
#[cfg(feature = "vecdeque")]
use std::collections::VecDeque;
/// A queue that drops its content after a given amount of time.
///
/// ## Example
///
/// To implement an FPS counter, you could use the following technique:
///
/// ```
/// # use std::thread;
/// # use std::time::Duration;
/// # use ttl_queue::TtlQueue;
/// let mut fps_counter = TtlQueue::new(Duration::from_secs_f64(1.0));
///
/// for i in 0..100 {
/// // Register a new frame and return the number of frames observed
/// // within the last second.
/// let fps = fps_counter.refresh_and_push_back(());
/// debug_assert!(fps >= 1);
///
/// // Sleep 10 ms to achieve a ~100 Hz frequency.
/// thread::sleep(Duration::from_millis(10));
/// }
///
/// let fps = fps_counter.refresh();
/// debug_assert!(fps >= 95 && fps <= 105);
/// ```
#[derive(Debug)]
pub struct TtlQueue<T> {
ttl: Duration,
#[cfg(feature = "doublestack")]
stack_1: Vec<(Instant, T)>,
#[cfg(feature = "doublestack")]
stack_2: Vec<(Instant, T)>,
#[cfg(feature = "vecdeque")]
queue: VecDeque<(Instant, T)>,
}
impl<T> TtlQueue<T> {
/// Creates an empty [`TtlQueue`] with default capacity.
pub fn new(ttl: Duration) -> Self {
Self {
ttl,
#[cfg(feature = "doublestack")]
stack_1: Vec::new(),
#[cfg(feature = "doublestack")]
stack_2: Vec::new(),
#[cfg(feature = "vecdeque")]
queue: VecDeque::new(),
}
}
/// Creates an empty [`TtlQueue`] for at least `capacity` elements.
pub fn with_capacity(ttl: Duration, capacity: usize) -> Self {
Self {
ttl,
#[cfg(feature = "doublestack")]
stack_1: Vec::with_capacity(capacity),
#[cfg(feature = "doublestack")]
stack_2: Vec::with_capacity(capacity),
#[cfg(feature = "vecdeque")]
queue: VecDeque::with_capacity(capacity),
}
}
/// Pushes an element to the end of the queue.
pub fn push_back(&mut self, element: T) {
let entry = (Instant::now(), element);
#[cfg(feature = "doublestack")]
{
self.stack_1.push(entry);
}
#[cfg(feature = "vecdeque")]
{
self.queue.push_back(entry)
}
}
/// Pushes an element to the end of the queue and returns the number of items
/// currently in the queue. This operation is O(N) at worst.
pub fn refresh_and_push_back(&mut self, element: T) -> usize {
let count = self.refresh();
self.push_back(element);
count + 1
}
/// Gets the element from the front of the queue if it exists, as well as the
/// time instant at which it was added.
pub fn pop_front(&mut self) -> Option<(Instant, T)> {
#[cfg(feature = "doublestack")]
{
self.ensure_stack_full();
self.stack_2.pop()
}
#[cfg(feature = "vecdeque")]
{
self.queue.pop_front()
}
}
/// Similar to [`pop_front`](Self::pop_front) but without removing the element.
pub fn peek_front(&mut self) -> Option<&(Instant, T)> {
#[cfg(feature = "doublestack")]
{
self.ensure_stack_full();
self.stack_2.first()
}
#[cfg(feature = "vecdeque")]
{
self.queue.front()
}
}
#[cfg(feature = "doublestack")]
fn ensure_stack_full(&mut self) {
if self.stack_2.is_empty() {
while let Some(item) = self.stack_1.pop() {
self.stack_2.push(item);
}
}
}
/// Gets the number elements currently in the queue, including potentially expired elements.
///
/// This operation is O(1). In order to obtain an accurate count in O(N) (worst-case),
/// use [`refresh`](Self::refresh) instead.
pub fn len(&self) -> usize {
#[cfg(feature = "doublestack")]
{
self.stack_1.len() + self.stack_2.len()
}
#[cfg(feature = "vecdeque")]
{
self.queue.len()
}
}
/// Returns `true` if the queue is definitely empty or `false` if the queue is
/// possibly empty.
///
/// This operation is O(1). In order to obtain an accurate count in O(N) (worst-case),
/// use [`refresh`](Self::refresh) instead.
pub fn is_empty(&self) -> bool {
#[cfg(feature = "doublestack")]
{
self.stack_1.is_empty() && self.stack_2.is_empty()
}
#[cfg(feature = "vecdeque")]
{
self.queue.is_empty()
}
}
/// Refreshes the queue and returns the number of currently contained elements.
#[cfg(feature = "doublestack")]
pub fn refresh(&mut self) -> usize {
let now = Instant::now();
while let Some((instant, _element)) = self.stack_2.first() {
if (now - *instant) < self.ttl {
break;
}
let _result = self.stack_2.pop();
debug_assert!(_result.is_some());
}
if !self.stack_2.is_empty() {
return self.len();
}
while let Some((instant, _element)) = self.stack_1.first() {
if (now - *instant) < self.ttl {
break;
}
let _result = self.stack_1.pop();
debug_assert!(_result.is_some());
}
debug_assert_eq!(self.stack_1.len(), self.len());
self.stack_1.len()
}
/// Refreshes the queue and returns the number of currently contained elements.
#[cfg(feature = "vecdeque")]
pub fn refresh(&mut self) -> usize {
let now = Instant::now();
while let Some((instant, _element)) = self.queue.front() {
if (now - *instant) < self.ttl {
break;
}
let _result = self.queue.pop_front();
debug_assert!(_result.is_some());
}
self.queue.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
#[test]
fn it_works() {
let mut queue = TtlQueue::new(Duration::from_millis(50));
queue.push_back(10);
queue.push_back(20);
queue.push_back(30);
assert_eq!(queue.refresh(), 3);
let value = queue.pop_front().unwrap();
assert_eq!(value.1, 10);
assert_eq!(queue.refresh(), 2);
thread::sleep(Duration::from_millis(50));
assert_eq!(queue.refresh(), 0);
}
}