fn insert_one_sided<K: Hash, V, H: BuildHasher>(
        key: K,
        key_data: &mut [Bucket<K, usize, B>],
        value_data: &mut [Bucket<V, usize, B>],
        hasher: &H,
    ) -> Result<usize, K> {
        let len = key_data.len();
        let ideal_index = Self::find_ideal_index(&key, hasher, len);

        if key_data[ideal_index].neighbourhood.full() {
            return Err(key);
        }

        let nearest = key_data[ideal_index..]
            .iter()
            .chain(key_data[..ideal_index].iter())
            .enumerate()
            .find(|&(_, bucket)| bucket.data.is_none())
            .map(|(offset, _)| offset);
        if let Some(offset) = nearest {
            // is this free space within the neighbourhood?
            if offset < B::size() {
                // insert and we're done
                let index = (offset + ideal_index) % len;
                Self::mark_as_full(ideal_index, index, key_data);
                key_data[index].data = Some((key, usize::max_value(), ideal_index));
                Ok(index)
            } else {
                // need to make room -> find a space, boot the old thing out to make room, insert,
                // repeat
                let max_offset = (ideal_index + B::size()) % len;
                let nearest = (0..)
                    .map(|i| (len + max_offset - i) % len)
                    .take(B::size() - 1)
                    .skip(1)
                    .find(|&i| {
                        let &(_, _, ideal) = key_data[i].data.as_ref().unwrap();
                        // check if the bucket we're planning to displace is closer to the blank
                        // space than we are, and make sure that it is close enough for us to move
                        // into its spot (more complicated due to wrap around)
                        if ideal > ideal_index {
                            ideal - ideal_index < B::size()
                        } else if ideal < ideal_index {
                            ideal < max_offset && max_offset < ideal
                        } else {
                            false
                        }
                    });
                if let Some(index) = nearest {
                    // we've found a spot to insert into
                    let (new_key, new_value, new_ideal) = key_data[index].data.take().unwrap();
                    Self::mark_as_empty(new_ideal, index, key_data);
                    key_data[index].data = Some((key, usize::max_value(), ideal_index));
                    Self::mark_as_full(ideal_index, index, key_data);
                    match Self::insert_one_sided(new_key, key_data, value_data, hasher) {
                        Ok(new_key_index) => {
                            // the replacement worked
                            {
                                let &mut (_, ref mut paired_key_index, _) =
                                    value_data[new_value].data.as_mut().unwrap();
                                *paired_key_index = new_key_index;
                                let &mut (_, ref mut paired_value_index, _) =
                                    key_data[new_key_index].data.as_mut().unwrap();
                                *paired_value_index = new_value;
                            }
                            Self::mark_as_full(new_ideal, new_key_index, key_data);

                            // finish our insert and return
                            Ok(index)
                        }
                        Err(new_key) => {
                            // the replacement failed - undo our insert
                            Self::mark_as_full(new_ideal, index, key_data);
                            Self::mark_as_empty(ideal_index, index, key_data);
                            let (key, _, _) = key_data[index].data.take().unwrap();
                            key_data[index].data = Some((new_key, new_value, new_ideal));
                            Err(key)
                        }
                    }
                } else {
                    // no spot can be inserted into
                    Err(key)
                }
            }
        } else {
            // there is no free space
            Err(key)
        }
    }
/// Inserts a given key into its data bucket. As this may do reshuffling, it requires a
/// reference to the value data buckets also. Returns, if it was possible to insert the value,
/// the index to which it was inserted. If it was not possible to do the insert, returns the
/// key that was going to be inserted. If this function returns successfully, it is guaranteed
/// that the key is located at the index specified, but its matching value is not set to
/// anything meaningful. This is the callers responsibility.
Inserts a given key into its data bucket. As this may do reshuffling, it requires a
reference to the value data buckets also. Returns, if it was possible to insert the value,
the index to which it was inserted. If it was not possible to do the insert, returns the
key that was going to be inserted. If this function returns successfully, it is guaranteed
that the key is located at the index specified, but its matching value is not set to
anything meaningful. This is the callers responsibility.
Inserts a given key into its data bucket . As this may do reshuffling it requires a reference to the value data buckets also . Returns if it was possible to insert the value the index to which it was inserted . If it was not possible to do the insert returns the key that was going to be inserted . If this function returns successfully it is guaranteed that the key is located at the index specified but its matching value is not set to anything meaningful . This is the callers responsibility .
fn insert_one_sided<K: Hash, V, H: BuildHasher>(
        key: K,
        key_data: &mut [Bucket<K, usize, B>],
        value_data: &mut [Bucket<V, usize, B>],
        hasher: &H,
    ) -> Result<usize, K> {
        let len = key_data.len();
        let ideal_index = Self::find_ideal_index(&key, hasher, len);
        if key_data[ideal_index].neighbourhood.full() {
            return Err(key);
        }
        let nearest = key_data[ideal_index..]
            .iter()
            .chain(key_data[..ideal_index].iter())
            .enumerate()
            .find(|&(_, bucket)| bucket.data.is_none())
            .map(|(offset, _)| offset);
        if let Some(offset) = nearest {
            // is this free space within the neighbourhood?
            if offset < B::size() {
                // insert and we're done
                let index = (offset + ideal_index) % len;
                Self::mark_as_full(ideal_index, index, key_data);
                key_data[index].data = Some((key, usize::max_value(), ideal_index));
                Ok(index)
            } else {
                // need to make room -> find a space, boot the old thing out to make room, insert,
                // repeat
                let max_offset = (ideal_index + B::size()) % len;
                let nearest = (0..)
                    .map(|i| (len + max_offset - i) % len)
                    .take(B::size() - 1)
                    .skip(1)
                    .find(|&i| {
                        let &(_, _, ideal) = key_data[i].data.as_ref().unwrap();
                        // check if the bucket we're planning to displace is closer to the blank
                        // space than we are, and make sure that it is close enough for us to move
                        // into its spot (more complicated due to wrap around)
                        if ideal > ideal_index {
                            ideal - ideal_index < B::size()
                        } else if ideal < ideal_index {
                            ideal < max_offset && max_offset < ideal
                        } else {
                            false
                        }
                    });
                if let Some(index) = nearest {
                    // we've found a spot to insert into
                    let (new_key, new_value, new_ideal) = key_data[index].data.take().unwrap();
                    Self::mark_as_empty(new_ideal, index, key_data);
                    key_data[index].data = Some((key, usize::max_value(), ideal_index));
                    Self::mark_as_full(ideal_index, index, key_data);
                    match Self::insert_one_sided(new_key, key_data, value_data, hasher) {
                        Ok(new_key_index) => {
                            // the replacement worked
                            {
                                let &mut (_, ref mut paired_key_index, _) =
                                    value_data[new_value].data.as_mut().unwrap();
                                *paired_key_index = new_key_index;
                                let &mut (_, ref mut paired_value_index, _) =
                                    key_data[new_key_index].data.as_mut().unwrap();
                                *paired_value_index = new_value;
                            }
                            Self::mark_as_full(new_ideal, new_key_index, key_data);
                            // finish our insert and return
                            Ok(index)
                        }
                        Err(new_key) => {
                            // the replacement failed - undo our insert
                            Self::mark_as_full(new_ideal, index, key_data);
                            Self::mark_as_empty(ideal_index, index, key_data);
                            let (key, _, _) = key_data[index].data.take().unwrap();
                            key_data[index].data = Some((new_key, new_value, new_ideal));
                            Err(key)
                        }
                    }
                } else {
                    // no spot can be inserted into
                    Err(key)
                }
            }
        } else {
            // there is no free space
            Err(key)
        }
    }
fn insert_one_sided < K : Hash , V , H : BuildHasher > ( key : K , key_data : & mut [ Bucket < K , usize , B > ] , value_data : & mut [ Bucket < V , usize , B > ] , hasher : & H , ) -> Result < usize , K > { let len = key_data . len ( ) ; let ideal_index = Self :: find_ideal_index ( & key , hasher , len ) ; if key_data [ ideal_index ] . neighbourhood . full ( ) { return Err ( key ) ; } let nearest = key_data [ ideal_index .. ] . iter ( ) . chain ( key_data [ .. ideal_index ] . iter ( ) ) . enumerate ( ) . find ( | & ( _ , bucket ) | bucket . data . is_none ( ) ) . map ( | ( offset , _ ) | offset ) ; if let Some ( offset ) = nearest { // is this free space within the neighbourhood? if offset < B :: size ( ) { // insert and we're done let index = ( offset + ideal_index ) % len ; Self :: mark_as_full ( ideal_index , index , key_data ) ; key_data [ index ] . data = Some ( ( key , usize :: max_value ( ) , ideal_index ) ) ; Ok ( index ) } else { // need to make room -> find a space, boot the old thing out to make room, insert, // repeat let max_offset = ( ideal_index + B :: size ( ) ) % len ; let nearest = ( 0 .. ) . map ( | i | ( len + max_offset - i ) % len ) . take ( B :: size ( ) - 1 ) . skip ( 1 ) . find ( | & i | { let & ( _ , _ , ideal ) = key_data [ i ] . data . as_ref ( ) . unwrap ( ) ; // check if the bucket we're planning to displace is closer to the blank // space than we are, and make sure that it is close enough for us to move // into its spot (more complicated due to wrap around) if ideal > ideal_index { ideal - ideal_index < B :: size ( ) } else if ideal < ideal_index { ideal < max_offset && max_offset < ideal } else { false } } ) ; if let Some ( index ) = nearest { // we've found a spot to insert into let ( new_key , new_value , new_ideal ) = key_data [ index ] . data . take ( ) . unwrap ( ) ; Self :: mark_as_empty ( new_ideal , index , key_data ) ; key_data [ index ] . data = Some ( ( key , usize :: max_value ( ) , ideal_index ) ) ; Self :: mark_as_full ( ideal_index , index , key_data ) ; match Self :: insert_one_sided ( new_key , key_data , value_data , hasher ) { Ok ( new_key_index ) => { // the replacement worked { let & mut ( _ , ref mut paired_key_index , _ ) = value_data [ new_value ] . data . as_mut ( ) . unwrap ( ) ; * paired_key_index = new_key_index ; let & mut ( _ , ref mut paired_value_index , _ ) = key_data [ new_key_index ] . data . as_mut ( ) . unwrap ( ) ; * paired_value_index = new_value ; } Self :: mark_as_full ( new_ideal , new_key_index , key_data ) ; // finish our insert and return Ok ( index ) } Err ( new_key ) => { // the replacement failed - undo our insert Self :: mark_as_full ( new_ideal , index , key_data ) ; Self :: mark_as_empty ( ideal_index , index , key_data ) ; let ( key , _ , _ ) = key_data [ index ] . data . take ( ) . unwrap ( ) ; key_data [ index ] . data = Some ( ( new_key , new_value , new_ideal ) ) ; Err ( key ) } } } else { // no spot can be inserted into Err ( key ) } } } else { // there is no free space Err ( key ) } }
Inserts a given key into its data bucket.
Inserts a given key into its data bucket .
key
K
key_data
&mut [Bucket<K, usize, B>]
value_data
&mut [Bucket<V, usize, B>]
hasher
&H
key
K
key_data
&mut [Bucket<K, usize, B>]
value_data
&mut [Bucket<V, usize, B>]
hasher
&H