Skip to content

Commit dfcd2e3

Browse files
committed
[eclipse-iceoryx#497] Introduce SlotMap::next_free_key
1 parent f71e100 commit dfcd2e3

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

iceoryx2-bb/container/src/slotmap.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ pub mod details {
274274
}
275275
}
276276

277+
pub(crate) unsafe fn next_free_key_impl(&self) -> Option<SlotMapKey> {
278+
self.idx_to_data_next_free_index
279+
.peek_impl()
280+
.map(|v| SlotMapKey::new(*v))
281+
}
282+
277283
pub(crate) fn len_impl(&self) -> usize {
278284
self.len
279285
}
@@ -386,6 +392,12 @@ pub mod details {
386392
unsafe { self.remove_impl(key) }
387393
}
388394

395+
/// Returns the [`SlotMapKey`] that will be used when the user calls
396+
/// [`SlotMap::insert()`]. If the [`SlotMap`] is full it returns [`None`].
397+
pub fn next_free_key(&self) -> Option<SlotMapKey> {
398+
unsafe { self.next_free_key_impl() }
399+
}
400+
389401
/// Returns the number of stored values.
390402
pub fn len(&self) -> usize {
391403
self.len_impl()
@@ -493,6 +505,17 @@ pub mod details {
493505
self.remove_impl(key)
494506
}
495507

508+
/// Returns the [`SlotMapKey`] that will be used when the user calls
509+
/// [`SlotMap::insert()`]. If the [`SlotMap`] is full it returns [`None`].
510+
///
511+
/// # Safety
512+
///
513+
/// * [`RelocatableSlotMap::init()`] must be called once before
514+
///
515+
pub unsafe fn next_free_key(&self) -> Option<SlotMapKey> {
516+
self.next_free_key_impl()
517+
}
518+
496519
/// Returns the number of stored values.
497520
pub fn len(&self) -> usize {
498521
self.len_impl()
@@ -607,6 +630,12 @@ impl<T, const CAPACITY: usize> FixedSizeSlotMap<T, CAPACITY> {
607630
unsafe { self.state.remove_impl(key) }
608631
}
609632

633+
/// Returns the [`SlotMapKey`] that will be used when the user calls
634+
/// [`SlotMap::insert()`]. If the [`SlotMap`] is full it returns [`None`].
635+
pub fn next_free_key(&self) -> Option<SlotMapKey> {
636+
unsafe { self.state.next_free_key_impl() }
637+
}
638+
610639
/// Returns the number of stored values.
611640
pub fn len(&self) -> usize {
612641
self.state.len_impl()

iceoryx2-bb/container/tests/slotmap_tests.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,31 @@ mod slot_map {
171171
assert_that!(*value, eq 5 * key.value() + 3);
172172
}
173173
}
174+
175+
#[test]
176+
fn next_free_key_returns_key_used_for_insert() {
177+
let mut sut = FixedSizeSut::new();
178+
let mut keys = vec![];
179+
180+
for _ in 0..SUT_CAPACITY / 2 {
181+
keys.push(sut.insert(0).unwrap());
182+
}
183+
184+
let next_key = sut.next_free_key();
185+
assert_that!(next_key, is_some);
186+
assert_that!(sut.insert(0), eq next_key);
187+
}
188+
189+
#[test]
190+
fn next_free_key_returns_none_when_full() {
191+
let mut sut = FixedSizeSut::new();
192+
let mut keys = vec![];
193+
194+
for _ in 0..SUT_CAPACITY {
195+
keys.push(sut.insert(0).unwrap());
196+
}
197+
198+
let next_key = sut.next_free_key();
199+
assert_that!(next_key, is_none);
200+
}
174201
}

0 commit comments

Comments
 (0)