Skip to content

Commit

Permalink
Merge pull request #513 from sourcebox/additions
Browse files Browse the repository at this point in the history
Added `is_full` function to `BinaryHeap`, `IndexMap`, `IndexSet` and `LinearMap`
  • Loading branch information
Dirbaio authored Oct 7, 2024
2 parents 8ab2335 + 0129924 commit 69add42
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `format` macro.
- Added `String::from_utf16`.
- Added `is_full`, `recent_index`, `oldest`, and `oldest_index` to `HistoryBuffer`
- Added `is_full` to `BinaryHeap`
- Added `is_full` to `IndexMap`
- Added `is_full` to `IndexSet`
- Added `is_full` to `LinearMap`
- Added infallible conversions from arrays to `Vec`.
- Added `Vec::spare_capacity_mut`.
- Added `Extend` impls for `Deque`.
Expand Down
20 changes: 20 additions & 0 deletions src/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@ where
self.len() == 0
}

/// Checks if the binary heap is full.
///
/// ```
/// use heapless::binary_heap::{BinaryHeap, Max};
///
/// let mut heap: BinaryHeap<_, Max, 4> = BinaryHeap::new();
///
/// assert!(!heap.is_full());
///
/// heap.push(1).unwrap();
/// heap.push(2).unwrap();
/// heap.push(3).unwrap();
/// heap.push(4).unwrap();
///
/// assert!(heap.is_full());
/// ```
pub fn is_full(&self) -> bool {
self.len() == self.capacity()
}

/// Returns an iterator visiting all values in the underlying vector, in arbitrary order.
///
/// ```
Expand Down
19 changes: 19 additions & 0 deletions src/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,25 @@ impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
self.len() == 0
}

/// Returns true if the map is full.
///
/// Computes in *O*(1) time.
///
/// ```
/// use heapless::FnvIndexMap;
///
/// let mut a = FnvIndexMap::<_, _, 4>::new();
/// assert!(!a.is_full());
/// a.insert(1, "a");
/// a.insert(2, "b");
/// a.insert(3, "c");
/// a.insert(4, "d");
/// assert!(a.is_full());
/// ```
pub fn is_full(&self) -> bool {
self.len() == self.capacity()
}

/// Remove all key-value pairs in the map, while preserving its capacity.
///
/// Computes in *O*(n) time.
Expand Down
19 changes: 19 additions & 0 deletions src/indexset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ impl<T, S, const N: usize> IndexSet<T, S, N> {
self.map.is_empty()
}

/// Returns `true` if the set is full.
///
/// # Examples
///
/// ```
/// use heapless::FnvIndexSet;
///
/// let mut v: FnvIndexSet<_, 4> = FnvIndexSet::new();
/// assert!(!v.is_full());
/// v.insert(1).unwrap();
/// v.insert(2).unwrap();
/// v.insert(3).unwrap();
/// v.insert(4).unwrap();
/// assert!(v.is_full());
/// ```
pub fn is_full(&self) -> bool {
self.map.is_full()
}

/// Clears the set, removing all values.
///
/// # Examples
Expand Down
21 changes: 21 additions & 0 deletions src/linear_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,27 @@ where
self.len() == 0
}

/// Returns true if the map is full.
///
/// Computes in *O*(1) time.
///
/// # Examples
///
/// ```
/// use heapless::LinearMap;
///
/// let mut a: LinearMap<_, _, 4> = LinearMap::new();
/// assert!(!a.is_full());
/// a.insert(1, "a").unwrap();
/// a.insert(2, "b").unwrap();
/// a.insert(3, "c").unwrap();
/// a.insert(4, "d").unwrap();
/// assert!(a.is_full());
/// ```
pub fn is_full(&self) -> bool {
self.len() == self.capacity()
}

/// An iterator visiting all key-value pairs in arbitrary order.
///
/// # Examples
Expand Down

0 comments on commit 69add42

Please sign in to comment.