Skip to content

Commit

Permalink
feat: add insert_probe to sync_index_set
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker committed Oct 26, 2023
1 parent 6080ee8 commit aec4451
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/sync_index_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ impl<T: Eq + Hash + StableDeref, S: BuildHasher> FrozenIndexSet<T, S> {
ret
}

/// If the key exists in the set, returns a reference to the index of the corresponding
/// value and false, otherwise inserts a new entry in the set for that value
/// and returns its index and true.
///
/// Existing values are never overwritten.
///
/// # Example
/// ```
/// use elsa::sync_index_set::FrozenIndexSet;
/// let map = FrozenIndexSet::new();
/// assert_eq!(map.insert_probe(Box::new("a")), (0, true));
/// assert_eq!(map.insert_probe(Box::new("b")), (1, true));
/// assert_eq!(map.insert_probe(Box::new("a")), (0, false));
/// ```
pub fn insert_probe(&self, value: T) -> (usize, bool) {
let mut set = self.set.write().unwrap();
(*set).insert_full(value)
}

/// Returns a reference to the value passed as argument if present in the set.
///
/// # Examples
Expand Down

0 comments on commit aec4451

Please sign in to comment.