From ed93db53099778682840a52bb817966ba5ee448c Mon Sep 17 00:00:00 2001 From: Louis Hong Date: Fri, 9 Sep 2022 10:10:19 -0700 Subject: [PATCH] Comments added about evenness/oddness of version --- src/basic.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/basic.rs b/src/basic.rs index 4811f57..8c014f8 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -47,6 +47,7 @@ impl Slot { // Is this slot occupied? #[inline(always)] pub fn occupied(&self) -> bool { + // Oddness check, Even = vacant, odd = occupied. self.version % 2 > 0 } @@ -404,6 +405,7 @@ impl SlotMap { } if let Some(slot) = self.slots.get_mut(self.free_head as usize) { + // By bitwise ORing by 1 on an even number we get an odd number, signifying that the slot is occupied. let occupied_version = slot.version | 1; let kd = KeyData::new(self.free_head, occupied_version); @@ -448,6 +450,9 @@ impl SlotMap { slot.u.next_free = self.free_head; self.free_head = idx as u32; self.num_elems -= 1; + // Increment the slot version. + // By adding 1 on an odd number we get an even number, + // signifying that the slot is vacant. slot.version = slot.version.wrapping_add(1); value