Skip to content

Commit

Permalink
get_or_panic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
beling committed Oct 4, 2024
1 parent 350bcd8 commit d200e6e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
21 changes: 18 additions & 3 deletions csf/src/fp/gomap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<GS: GroupSize, SS: SeedSize, S: BuildSeededHasher> GOMap<GS, SS, S> {
///
/// If the `key` was not in the input key-value collection given during construction,
/// either [`None`] or a value assigned to other key is returned.
pub fn get_stats<K: Hash, A: stats::AccessStatsCollector>(&self, key: &K, access_stats: &mut A) -> Option<u8> {
pub fn get_stats<K: Hash + ?Sized, A: stats::AccessStatsCollector>(&self, key: &K, access_stats: &mut A) -> Option<u8> {
let mut groups_before = 0u64;
let mut level_nr = 0u32;
loop {
Expand All @@ -62,9 +62,24 @@ impl<GS: GroupSize, SS: SeedSize, S: BuildSeededHasher> GOMap<GS, SS, S> {
///
/// If the `key` was not in the input key-value collection given during construction,
/// either [`None`] or a value assigned to other key is returned.
#[inline] pub fn get<K: Hash>(&self, key: &K) -> Option<u8> {
#[inline] pub fn get<K: Hash + ?Sized>(&self, key: &K) -> Option<u8> {
self.get_stats(key, &mut ())
}


/// Gets the value associated with the given `key` and reports statistics to `access_stats`.
///
/// If the `key` was not in the input key-value collection given during construction,
/// it either panics or returns a value assigned to other key is returned.
#[inline] pub fn get_stats_or_panic<K: Hash + ?Sized, A: stats::AccessStatsCollector>(&self, key: &K, access_stats: &mut A) -> u8 {
self.get_stats(key, access_stats).expect("Invalid access to a key outside the set given during construction.")
}

/// Gets the value associated with the given `key`.
///
/// If the `key` was not in the input key-value collection given during construction,
/// it either panics or returns a value assigned to other key is returned.
#[inline] pub fn get_or_panic<K: Hash + ?Sized>(&self, key: &K) -> u8 {
self.get_stats_or_panic(key, &mut ())
}

}
22 changes: 19 additions & 3 deletions csf/src/fp/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<S: BuildSeededHasher> GetSize for Map<S> {
}

#[inline]
fn index<H: BuildSeededHasher, K: Hash>(hash: &H, k: &K, level_nr: u32, level_size: usize) -> usize {
fn index<H: BuildSeededHasher, K: Hash + ?Sized>(hash: &H, k: &K, level_nr: u32, level_size: usize) -> usize {
ph::utils::map64_to_64(hash.hash_one(k, level_nr), level_size as u64) as usize
}

Expand Down Expand Up @@ -76,7 +76,7 @@ impl<S: BuildSeededHasher> Map<S> {
///
/// If the `key` was not in the input key-value collection given during construction,
/// either [`None`] or a value assigned to other key is returned.
pub fn get_stats<K: Hash, A: stats::AccessStatsCollector>(&self, key: &K, access_stats: &mut A) -> Option<u8> {
pub fn get_stats<K: Hash + ?Sized, A: stats::AccessStatsCollector>(&self, key: &K, access_stats: &mut A) -> Option<u8> {
let mut array_begin_index = 0usize;
let mut level = 0u32;
loop {
Expand All @@ -95,10 +95,26 @@ impl<S: BuildSeededHasher> Map<S> {
///
/// If the `key` was not in the input key-value collection given during construction,
/// either [`None`] or a value assigned to other key is returned.
pub fn get<K: Hash>(&self, key: &K) -> Option<u8> {
pub fn get<K: Hash + ?Sized>(&self, key: &K) -> Option<u8> {
self.get_stats(key, &mut ())
}

/// Gets the value associated with the given `key` and reports statistics to `access_stats`.
///
/// If the `key` was not in the input key-value collection given during construction,
/// it either panics or returns a value assigned to other key is returned.
#[inline] pub fn get_stats_or_panic<K: Hash + ?Sized, A: stats::AccessStatsCollector>(&self, key: &K, access_stats: &mut A) -> u8 {
self.get_stats(key, access_stats).expect("Invalid access to a key outside the set given during construction.")
}

/// Gets the value associated with the given `key`.
///
/// If the `key` was not in the input key-value collection given during construction,
/// it either panics or returns a value assigned to other key is returned.
#[inline] pub fn get_or_panic<K: Hash + ?Sized>(&self, key: &K) -> u8 {
self.get_stats_or_panic(key, &mut ())
}

/// Pre-builds [`Map`] for given key-value pairs `kv`, using the build configuration `conf` and reporting statistics with `stats`.
/// After return `kv` contains the pairs which could not be added to the map.
/// It is empty when construction is completed successfully.
Expand Down

0 comments on commit d200e6e

Please sign in to comment.