orlp / slotmap

Slotmap data structure for Rust
zlib License
1.16k stars 70 forks source link

Remove a `SecondaryMap` slot regardless of the version of the key #112

Open vitreo12 opened 1 year ago

vitreo12 commented 1 year ago

Is it possible to remove a slot regardless of the version of the key, but only looking at its idx? Currently, remove is defined as:

    pub fn remove(&mut self, key: K) -> Option<V> {
        let kd = key.data();
        if let Some(slot) = self.slots.get_mut(kd.idx as usize) {
            if slot.version() == kd.version.get() {
                self.num_elems -= 1;
                return replace(slot, Slot::new_vacant()).into_option();
            }
        }

        None
    }

This function could look something like:

    pub fn remove_index(&mut self, key: K) -> Option<V> {
        let kd = key.data();
        if let Some(slot) = self.slots.get_mut(kd.idx as usize) {
            if slot.occupied() {
                self.num_elems -= 1;
                return replace(slot, Slot::new_vacant()).into_option();
            }
        }

        None
    }