Skip to content

Commit 69add42

Browse files
authored
Merge pull request #513 from sourcebox/additions
Added `is_full` function to `BinaryHeap`, `IndexMap`, `IndexSet` and `LinearMap`
2 parents 8ab2335 + 0129924 commit 69add42

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- Added `format` macro.
1313
- Added `String::from_utf16`.
1414
- Added `is_full`, `recent_index`, `oldest`, and `oldest_index` to `HistoryBuffer`
15+
- Added `is_full` to `BinaryHeap`
16+
- Added `is_full` to `IndexMap`
17+
- Added `is_full` to `IndexSet`
18+
- Added `is_full` to `LinearMap`
1519
- Added infallible conversions from arrays to `Vec`.
1620
- Added `Vec::spare_capacity_mut`.
1721
- Added `Extend` impls for `Deque`.

src/binary_heap.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@ where
262262
self.len() == 0
263263
}
264264

265+
/// Checks if the binary heap is full.
266+
///
267+
/// ```
268+
/// use heapless::binary_heap::{BinaryHeap, Max};
269+
///
270+
/// let mut heap: BinaryHeap<_, Max, 4> = BinaryHeap::new();
271+
///
272+
/// assert!(!heap.is_full());
273+
///
274+
/// heap.push(1).unwrap();
275+
/// heap.push(2).unwrap();
276+
/// heap.push(3).unwrap();
277+
/// heap.push(4).unwrap();
278+
///
279+
/// assert!(heap.is_full());
280+
/// ```
281+
pub fn is_full(&self) -> bool {
282+
self.len() == self.capacity()
283+
}
284+
265285
/// Returns an iterator visiting all values in the underlying vector, in arbitrary order.
266286
///
267287
/// ```

src/indexmap.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,25 @@ impl<K, V, S, const N: usize> IndexMap<K, V, S, N> {
926926
self.len() == 0
927927
}
928928

929+
/// Returns true if the map is full.
930+
///
931+
/// Computes in *O*(1) time.
932+
///
933+
/// ```
934+
/// use heapless::FnvIndexMap;
935+
///
936+
/// let mut a = FnvIndexMap::<_, _, 4>::new();
937+
/// assert!(!a.is_full());
938+
/// a.insert(1, "a");
939+
/// a.insert(2, "b");
940+
/// a.insert(3, "c");
941+
/// a.insert(4, "d");
942+
/// assert!(a.is_full());
943+
/// ```
944+
pub fn is_full(&self) -> bool {
945+
self.len() == self.capacity()
946+
}
947+
929948
/// Remove all key-value pairs in the map, while preserving its capacity.
930949
///
931950
/// Computes in *O*(n) time.

src/indexset.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ impl<T, S, const N: usize> IndexSet<T, S, N> {
180180
self.map.is_empty()
181181
}
182182

183+
/// Returns `true` if the set is full.
184+
///
185+
/// # Examples
186+
///
187+
/// ```
188+
/// use heapless::FnvIndexSet;
189+
///
190+
/// let mut v: FnvIndexSet<_, 4> = FnvIndexSet::new();
191+
/// assert!(!v.is_full());
192+
/// v.insert(1).unwrap();
193+
/// v.insert(2).unwrap();
194+
/// v.insert(3).unwrap();
195+
/// v.insert(4).unwrap();
196+
/// assert!(v.is_full());
197+
/// ```
198+
pub fn is_full(&self) -> bool {
199+
self.map.is_full()
200+
}
201+
183202
/// Clears the set, removing all values.
184203
///
185204
/// # Examples

src/linear_map.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,27 @@ where
227227
self.len() == 0
228228
}
229229

230+
/// Returns true if the map is full.
231+
///
232+
/// Computes in *O*(1) time.
233+
///
234+
/// # Examples
235+
///
236+
/// ```
237+
/// use heapless::LinearMap;
238+
///
239+
/// let mut a: LinearMap<_, _, 4> = LinearMap::new();
240+
/// assert!(!a.is_full());
241+
/// a.insert(1, "a").unwrap();
242+
/// a.insert(2, "b").unwrap();
243+
/// a.insert(3, "c").unwrap();
244+
/// a.insert(4, "d").unwrap();
245+
/// assert!(a.is_full());
246+
/// ```
247+
pub fn is_full(&self) -> bool {
248+
self.len() == self.capacity()
249+
}
250+
230251
/// An iterator visiting all key-value pairs in arbitrary order.
231252
///
232253
/// # Examples

0 commit comments

Comments
 (0)