Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added _yes get_or_intern methods #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
153 changes: 141 additions & 12 deletions src/rodeo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,42 @@ where
.expect("Failed to get or intern string")
}

/// Get a boolean signifying whether the string is previously unseen and the key for it, interning it if it is
///
/// # Panics
///
/// Panics if the key's `try_from_usize` function fails. With the default keys, this means that
/// you've interned more strings than it can handle. (For [`Spur`] this means that `u32::MAX - 1`
/// unique strings were interned)
///
/// # Example
///
/// ```rust
/// use lasso::Rodeo;
///
/// let mut rodeo = Rodeo::default();
///
/// // Interned the string
/// let (is_new, key) = rodeo.insert("Strings of things with wings and dings");
/// assert!(is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
///
/// // No string was interned, as it was already contained
/// let (is_new, key) = rodeo.insert("Strings of things with wings and dings");
/// assert!(!is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
/// ```
///
/// [`Spur`]: crate::Spur
#[cfg_attr(feature = "inline-more", inline)]
pub fn insert<T>(&mut self, val: T) -> (bool, K)
where
T: AsRef<str>,
{
self.try_insert(val)
.expect("Failed to get or intern string")
}

/// Get the key for a string, interning it if it does not yet exist
///
/// # Example
Expand All @@ -311,6 +347,34 @@ where
///
#[cfg_attr(feature = "inline-more", inline)]
pub fn try_get_or_intern<T>(&mut self, val: T) -> LassoResult<K>
where
T: AsRef<str>,
{
self.try_insert(val).map(|(_yes, key)| key)
}

/// Get a boolean signifying whether the string is previously unseen and the key for it, interning it if it is
///
/// # Example
///
/// ```rust
/// use lasso::Rodeo;
///
/// let mut rodeo = Rodeo::default();
///
/// // Interned the string
/// let (is_new, key) = rodeo.try_insert("Strings of things with wings and dings").unwrap();
/// assert!(is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
///
/// // No string was interned, as it was already contained
/// let (is_new, key) = rodeo.try_insert("Strings of things with wings and dings").unwrap();
/// assert!(!is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
/// ```
///
#[cfg_attr(feature = "inline-more", inline)]
pub fn try_insert<T>(&mut self, val: T) -> LassoResult<(bool, K)>
where
T: AsRef<str>,
{
Expand All @@ -327,9 +391,9 @@ where
let hash = hash_string(hasher, string_slice);

// Get the map's entry that the string should occupy
let key = match get_string_entry_mut(map, strings, hash, string_slice) {
match get_string_entry_mut(map, strings, hash, string_slice) {
// The string already exists, so return its key
RawEntryMut::Occupied(entry) => *entry.into_key(),
RawEntryMut::Occupied(entry) => Ok((false, *entry.into_key())),

// The string does not yet exist, so insert it and create its key
RawEntryMut::Vacant(entry) => {
Expand All @@ -347,11 +411,9 @@ where
// Insert the key with the hash of the string that it points to, reusing the hash we made earlier
insert_string(entry, strings, hasher, hash, key);

key
Ok((true, key))
}
};

Ok(key)
}
}

/// Get the key for a static string, interning it if it does not yet exist
Expand Down Expand Up @@ -386,6 +448,41 @@ where
.expect("Failed to get or intern static string")
}

/// Get a boolean signifying whether the static string is previously unseen and the key for it, interning it if it is
///
/// This will not reallocate or copy the given string
///
/// # Panics
///
/// Panics if the key's `try_from_usize` function fails. With the default keys, this means that
/// you've interned more strings than it can handle. (For [`Spur`] this means that `u32::MAX - 1`
/// unique strings were interned)
///
/// # Example
///
/// ```rust
/// use lasso::Rodeo;
///
/// let mut rodeo = Rodeo::default();
///
/// // Interned the string
/// let (is_new, key) = rodeo.insert_static("Strings of things with wings and dings");
/// assert!(is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
///
/// // No string was interned, as it was already contained
/// let (is_new, key) = rodeo.insert_static("Strings of things with wings and dings");
/// assert!(!is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
///
/// ```
///
#[cfg_attr(feature = "inline-more", inline)]
pub fn insert_static(&mut self, string: &'static str) -> (bool, K) {
self.try_insert_static(string)
.expect("Failed to get or intern static string")
}

/// Get the key for a static string, interning it if it does not yet exist
///
/// This will not reallocate or copy the given string
Expand All @@ -408,6 +505,33 @@ where
///
#[cfg_attr(feature = "inline-more", inline)]
pub fn try_get_or_intern_static(&mut self, string: &'static str) -> LassoResult<K> {
self.try_insert_static(string).map(|(_yes, key)| key)
}

/// Get a boolean signifying whether the static string is previously unseen and the key for it, interning it if it is
///
/// This will not reallocate or copy the given string
///
/// # Example
///
/// ```rust
/// use lasso::Rodeo;
///
/// let mut rodeo = Rodeo::default();
///
/// // Interned the string
/// let (is_new, key) = rodeo.try_insert_static("Strings of things with wings and dings").unwrap();
/// assert!(is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
///
/// // No string was interned, as it was already contained
/// let (is_new, key) = rodeo.try_insert_static("Strings of things with wings and dings").unwrap();
/// assert!(!is_new);
/// assert_eq!("Strings of things with wings and dings", rodeo.resolve(&key));
/// ```
///
#[cfg_attr(feature = "inline-more", inline)]
pub fn try_insert_static(&mut self, string: &'static str) -> LassoResult<(bool, K)> {
let Self {
map,
hasher,
Expand All @@ -419,9 +543,9 @@ where
let hash = hash_string(hasher, string);

// Get the map's entry that the string should occupy
let key = match get_string_entry_mut(map, strings, hash, string) {
match get_string_entry_mut(map, strings, hash, string) {
// The string already exists, so return its key
RawEntryMut::Occupied(entry) => *entry.into_key(),
RawEntryMut::Occupied(entry) => Ok((false, *entry.into_key())),

// The string does not yet exist, so insert it and create its key
RawEntryMut::Vacant(entry) => {
Expand All @@ -435,11 +559,9 @@ where
// Insert the key with the hash of the string that it points to, reusing the hash we made earlier
insert_string(entry, strings, hasher, hash, key);

key
Ok((true, key))
}
};

Ok(key)
}
}

/// Get the key value of a string, returning `None` if it doesn't exist
Expand Down Expand Up @@ -761,6 +883,13 @@ impl<K, S> Rodeo<K, S> {
Strings::from_rodeo(self)
}

/// Returns a reference to the slice of interned strings
#[cfg_attr(feature = "inline-more", inline)]
pub fn as_str(&self) -> &[&str] {
// Safety: we are shortening the &str lifetime from static to self, so they won't outlive the arens
&self.strings
}

/// Set the `Rodeo`'s maximum memory usage while in-flight
///
/// Note that setting the maximum memory usage to below the currently allocated
Expand Down
Loading