Skip to content

Commit e38efc7

Browse files
committed
Add new helpers
1 parent da43443 commit e38efc7

File tree

3 files changed

+783
-0
lines changed

3 files changed

+783
-0
lines changed

src/lib.rs

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,181 @@ impl<K: Eq + Hash + Clone, V> SieveCache<K, V> {
602602
}
603603
}
604604
}
605+
606+
/// Removes all entries from the cache.
607+
///
608+
/// This operation clears all stored values and resets the cache to an empty state,
609+
/// while maintaining the original capacity.
610+
///
611+
/// # Examples
612+
///
613+
/// ```rust
614+
/// # #[cfg(feature = "doctest")]
615+
/// # {
616+
/// use sieve_cache::SieveCache;
617+
///
618+
/// let mut cache = SieveCache::new(100).unwrap();
619+
/// cache.insert("key1".to_string(), "value1".to_string());
620+
/// cache.insert("key2".to_string(), "value2".to_string());
621+
///
622+
/// assert_eq!(cache.len(), 2);
623+
///
624+
/// cache.clear();
625+
/// assert_eq!(cache.len(), 0);
626+
/// assert!(cache.is_empty());
627+
/// # }
628+
/// ```
629+
pub fn clear(&mut self) {
630+
self.map.clear();
631+
self.head = None;
632+
self.tail = None;
633+
self.hand = None;
634+
self.len = 0;
635+
}
636+
637+
/// Returns an iterator over all keys in the cache.
638+
///
639+
/// The order of keys is not specified and should not be relied upon.
640+
///
641+
/// # Examples
642+
///
643+
/// ```rust
644+
/// # #[cfg(feature = "doctest")]
645+
/// # {
646+
/// use sieve_cache::SieveCache;
647+
/// use std::collections::HashSet;
648+
///
649+
/// let mut cache = SieveCache::new(100).unwrap();
650+
/// cache.insert("key1".to_string(), "value1".to_string());
651+
/// cache.insert("key2".to_string(), "value2".to_string());
652+
///
653+
/// let keys: HashSet<_> = cache.keys().collect();
654+
/// assert_eq!(keys.len(), 2);
655+
/// assert!(keys.contains(&"key1".to_string()));
656+
/// assert!(keys.contains(&"key2".to_string()));
657+
/// # }
658+
/// ```
659+
pub fn keys(&self) -> impl Iterator<Item = &K> {
660+
self.map.keys()
661+
}
662+
663+
/// Returns an iterator over all values in the cache.
664+
///
665+
/// The order of values is not specified and should not be relied upon.
666+
///
667+
/// # Examples
668+
///
669+
/// ```rust
670+
/// # #[cfg(feature = "doctest")]
671+
/// # {
672+
/// use sieve_cache::SieveCache;
673+
/// use std::collections::HashSet;
674+
///
675+
/// let mut cache = SieveCache::new(100).unwrap();
676+
/// cache.insert("key1".to_string(), "value1".to_string());
677+
/// cache.insert("key2".to_string(), "value2".to_string());
678+
///
679+
/// let values: HashSet<_> = cache.values().collect();
680+
/// assert_eq!(values.len(), 2);
681+
/// assert!(values.contains(&"value1".to_string()));
682+
/// assert!(values.contains(&"value2".to_string()));
683+
/// # }
684+
/// ```
685+
pub fn values(&self) -> impl Iterator<Item = &V> {
686+
self.map.values().map(|node| &node.value)
687+
}
688+
689+
/// Returns an iterator over all mutable values in the cache.
690+
///
691+
/// The order of values is not specified and should not be relied upon.
692+
/// Note that iterating through this will mark all entries as visited.
693+
///
694+
/// # Examples
695+
///
696+
/// ```rust
697+
/// # #[cfg(feature = "doctest")]
698+
/// # {
699+
/// use sieve_cache::SieveCache;
700+
///
701+
/// let mut cache = SieveCache::new(100).unwrap();
702+
/// cache.insert("key1".to_string(), "value1".to_string());
703+
/// cache.insert("key2".to_string(), "value2".to_string());
704+
///
705+
/// // Update all values by appending text
706+
/// for value in cache.values_mut() {
707+
/// *value = format!("{}_updated", value);
708+
/// }
709+
///
710+
/// assert_eq!(cache.get("key1"), Some(&"value1_updated".to_string()));
711+
/// assert_eq!(cache.get("key2"), Some(&"value2_updated".to_string()));
712+
/// # }
713+
/// ```
714+
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
715+
self.map.values_mut().map(|node| {
716+
node.visited = true;
717+
&mut node.value
718+
})
719+
}
720+
721+
/// Returns an iterator over all key-value pairs in the cache.
722+
///
723+
/// The order of pairs is not specified and should not be relied upon.
724+
///
725+
/// # Examples
726+
///
727+
/// ```rust
728+
/// # #[cfg(feature = "doctest")]
729+
/// # {
730+
/// use sieve_cache::SieveCache;
731+
/// use std::collections::HashMap;
732+
///
733+
/// let mut cache = SieveCache::new(100).unwrap();
734+
/// cache.insert("key1".to_string(), "value1".to_string());
735+
/// cache.insert("key2".to_string(), "value2".to_string());
736+
///
737+
/// let entries: HashMap<_, _> = cache.iter().collect();
738+
/// assert_eq!(entries.len(), 2);
739+
/// assert_eq!(entries.get(&"key1".to_string()), Some(&&"value1".to_string()));
740+
/// assert_eq!(entries.get(&"key2".to_string()), Some(&&"value2".to_string()));
741+
/// # }
742+
/// ```
743+
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
744+
self.map.iter().map(|(k, v)| (k, &v.value))
745+
}
746+
747+
/// Returns an iterator over all key-value pairs in the cache, with mutable references to values.
748+
///
749+
/// The order of pairs is not specified and should not be relied upon.
750+
/// Note that iterating through this will mark all entries as visited.
751+
///
752+
/// # Examples
753+
///
754+
/// ```rust
755+
/// # #[cfg(feature = "doctest")]
756+
/// # {
757+
/// use sieve_cache::SieveCache;
758+
///
759+
/// let mut cache = SieveCache::new(100).unwrap();
760+
/// cache.insert("key1".to_string(), "value1".to_string());
761+
/// cache.insert("key2".to_string(), "value2".to_string());
762+
///
763+
/// // Update all values associated with keys containing '1'
764+
/// for (key, value) in cache.iter_mut() {
765+
/// if key.contains('1') {
766+
/// *value = format!("{}_special", value);
767+
/// }
768+
/// }
769+
///
770+
/// assert_eq!(cache.get("key1"), Some(&"value1_special".to_string()));
771+
/// assert_eq!(cache.get("key2"), Some(&"value2".to_string()));
772+
/// # }
773+
/// ```
774+
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
775+
self.map.iter_mut().map(|(k, v)| {
776+
v.visited = true;
777+
(k, &mut v.value)
778+
})
779+
}
605780
}
606781

607782
#[test]
@@ -629,3 +804,64 @@ fn test_visited_flag_update() {
629804
cache.insert("key3".to_string(), "value3".to_string());
630805
assert_eq!(cache.get("key1"), Some(&"updated".to_string()));
631806
}
807+
808+
#[test]
809+
fn test_clear() {
810+
let mut cache = SieveCache::new(10).unwrap();
811+
cache.insert("key1".to_string(), "value1".to_string());
812+
cache.insert("key2".to_string(), "value2".to_string());
813+
814+
assert_eq!(cache.len(), 2);
815+
assert!(!cache.is_empty());
816+
817+
cache.clear();
818+
819+
assert_eq!(cache.len(), 0);
820+
assert!(cache.is_empty());
821+
assert_eq!(cache.get("key1"), None);
822+
assert_eq!(cache.get("key2"), None);
823+
}
824+
825+
#[test]
826+
fn test_iterators() {
827+
let mut cache = SieveCache::new(10).unwrap();
828+
cache.insert("key1".to_string(), "value1".to_string());
829+
cache.insert("key2".to_string(), "value2".to_string());
830+
831+
// Test keys iterator
832+
let keys: Vec<&String> = cache.keys().collect();
833+
assert_eq!(keys.len(), 2);
834+
assert!(keys.contains(&&"key1".to_string()));
835+
assert!(keys.contains(&&"key2".to_string()));
836+
837+
// Test values iterator
838+
let values: Vec<&String> = cache.values().collect();
839+
assert_eq!(values.len(), 2);
840+
assert!(values.contains(&&"value1".to_string()));
841+
assert!(values.contains(&&"value2".to_string()));
842+
843+
// Test values_mut iterator
844+
for value in cache.values_mut() {
845+
*value = format!("{}_updated", value);
846+
}
847+
848+
assert_eq!(cache.get("key1"), Some(&"value1_updated".to_string()));
849+
assert_eq!(cache.get("key2"), Some(&"value2_updated".to_string()));
850+
851+
// Test key-value iterator
852+
let entries: Vec<(&String, &String)> = cache.iter().collect();
853+
assert_eq!(entries.len(), 2);
854+
855+
// Test key-value mutable iterator
856+
for (key, value) in cache.iter_mut() {
857+
if key == "key1" {
858+
*value = format!("{}_special", value);
859+
}
860+
}
861+
862+
assert_eq!(
863+
cache.get("key1"),
864+
Some(&"value1_updated_special".to_string())
865+
);
866+
assert_eq!(cache.get("key2"), Some(&"value2_updated".to_string()));
867+
}

0 commit comments

Comments
 (0)