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

Add functions to check for numerical and/or sequential keys #115

Merged
merged 1 commit into from
Dec 13, 2021
Merged
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
54 changes: 54 additions & 0 deletions src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,60 @@ impl ZendHashTable {
Ok(())
}

/// Checks if the hashtable only contains numerical keys.
///
/// # Returns
///
/// True if all keys on the hashtable are numerical.
///
/// # Example
///
/// ```no_run
/// use ext_php_rs::types::ZendHashTable;
///
/// let mut ht = ZendHashTable::new();
///
/// ht.push(0);
/// ht.push(3);
/// ht.push(9);
/// assert!(ht.has_numerical_keys());
///
/// ht.insert("obviously not numerical", 10);
/// assert!(!ht.has_numerical_keys());
/// ```
pub fn has_numerical_keys(&self) -> bool {
!self.iter().any(|(_, k, _)| k.is_some())
}

/// Checks if the hashtable has numerical, sequential keys.
///
/// # Returns
///
/// True if all keys on the hashtable are numerical and are in sequential
/// order (i.e. starting at 0 and not skipping any keys).
///
/// # Example
///
/// ```no_run
/// use ext_php_rs::types::ZendHashTable;
///
/// let mut ht = ZendHashTable::new();
///
/// ht.push(0);
/// ht.push(3);
/// ht.push(9);
/// assert!(ht.has_sequential_keys());
///
/// ht.insert_at_index(90, 10);
/// assert!(!ht.has_sequential_keys());
/// ```
pub fn has_sequential_keys(&self) -> bool {
!self
.iter()
.enumerate()
.any(|(i, (k, strk, _))| i as u64 != k || strk.is_some())
}

/// Returns an iterator over the key(s) and value contained inside the
/// hashtable.
///
Expand Down