Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use std::marker::PhantomData;
/// a key from a value that is Hash.
///
#[derive(Clone)]
pub struct S3FIFOKey<V: Hash> {
pub struct S3FIFOKey<V> {
hash: u64,
_phantom: PhantomData<V>,
}

impl<V: Hash> PartialEq for S3FIFOKey<V> {
impl<V> PartialEq for S3FIFOKey<V> {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
Expand All @@ -36,16 +36,16 @@ impl<V: Hash> S3FIFOKey<V> {
}
}

impl<V: Hash> std::fmt::Debug for S3FIFOKey<V> {
impl<V> std::fmt::Debug for S3FIFOKey<V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("S3FIFOKey")
.field("hash", &self.hash)
.finish()
}
}

impl<V: Hash> Display for S3FIFOKey<V> {
impl<V> Display for S3FIFOKey<V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:#x}", self.hash)
}
}
}
48 changes: 43 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::{
collections::VecDeque,
fmt::{self, Debug},
sync::atomic::{AtomicI8, Ordering},
};

Expand Down Expand Up @@ -42,6 +43,19 @@ pub struct S3FIFO<K, V> {
main: VecDeque<Item<K, V>>,
ghost: VecDeque<Key<K>>,
}
impl<K, V> std::fmt::Debug for S3FIFO<K, V>
where
K: std::fmt::Debug,
V: std::fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("S3FIFO")
.field("small", &self.small)
.field("main", &self.main)
.field("ghost", &self.ghost)
.finish()
}
}

impl<K: PartialEq + Clone, V> S3FIFO<K, V> {
///
Expand Down Expand Up @@ -155,7 +169,7 @@ impl<K: PartialEq + Clone, V> S3FIFO<K, V> {
evicted = self.evict_main();
}
self.main.push_front(item);
return (&mut self.main.front_mut().unwrap().value, evicted);
(&mut self.main.front_mut().unwrap().value, evicted)
} else {
let item = Item {
key,
Expand All @@ -166,7 +180,7 @@ impl<K: PartialEq + Clone, V> S3FIFO<K, V> {
evicted = self.evict_small();
}
self.small.push_front(item);
return (&mut self.small.front_mut().unwrap().value, evicted);
(&mut self.small.front_mut().unwrap().value, evicted)
}
}

Expand Down Expand Up @@ -220,9 +234,7 @@ impl<K: PartialEq + Clone, V> S3FIFO<K, V> {
// then the maximum number of iterations is 3 * main.len() + 1
let mut iters = (3 * self.main.len() + 1) as isize;
while iters > 0 {
let Some(item) = self.main.pop_back() else {
return None;
};
let item = self.main.pop_back()?;
iters -= 1;
let freq = item.freq.load(Ordering::Relaxed);
if freq > 0 {
Expand All @@ -242,11 +254,37 @@ struct Item<K, V> {
freq: AtomicI8, // not thread-safe
}

impl<K, V> Debug for Item<K, V>
where
K: Debug,
V: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Item")
.field("key", &self.key)
.field("value", &self.value)
.field("freq", &self.freq)
.finish()
}
}

struct Key<K> {
key: K,
freq: AtomicI8, // not thread-safe
}

impl<K> Debug for Key<K>
where
K: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Key")
.field("key", &self.key)
.field("freq", &self.freq)
.finish()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down