Struct lockfree::map::Map[][src]

pub struct Map<K, V, H = RandomState> { /* fields omitted */ }

A lock-free map. Implemented using multi-level hash-tables (in a tree fashion) with ordered buckets.

Design

In order to implement this map, we shall fix a constant named BITS, which should be smaller than the number of bits in the hash. We chose 8 for it. Now, we define a table structure: an array of nodes with length 1 << BITS (256 in this case).

For inserting, we take the first BITS bits of the hash. Now, we verify the node. If it is empty, insert a new bucket with our entry (a leaf of the tree), and assign our hash to the bucket. If there is a branch (i.e. a sub-table), we shift the hash BITS bits to the left, but we also keep the original hash for consultation. Then we try again in the sub-table. If there is another leaf, and if the hash of the leaf's bucket is equal to ours, we insert our entry into the bucket. If the hashes are not equal, we create a sub-table, insert the old leaf into the new sub-table, and insert our pair after.

Entries in a bucket are a single linked list ordered by key. The ordering of the list is because of possible race conditions if e.g. new nodes were always inserted at end. And if a bucket is detected to be empty, the table will be requested to delete the bucket.

For searching, in a similar way, the hash is shifted and sub-tables are entered until either a node is empty or a leaf is found. If the hash of the leaf's bucket is equal to our hash, we search for the entry into the bucket. Because the bucket is ordered, we may know the entry is not present with ease.

Because of limitation of sharing in concurrent contexts, we do return references to the entries, neither allow the user to move out removed values, as they must be deinitialized correctly. Returning references would also imply pausing the deallocation of sensitive resources for indefinite time.

Methods

impl<K, V> Map<K, V>
[src]

Creates a new empty map with a random state.

impl<K, V, H> Map<K, V, H>
[src]

Creates a new empty map with a hash builder.

Sets the mapped value of a key, disregarding it exists or not. If it does exists, the old pair is removed and returned.

An interactive insertion. Instead of providing a key and value, one provides a key and a closure. The closure will check the status of the insertion and then return an action. The closure may be called multiple times, as different things are discovered by the map implementation.

The first argument of the closure is the provided key. The second argument of the closure is a value previously generated by the closure, if there is one. The third argument is the stored pair, if there is one. If there was not one, None is passed.

Reinserts a removed pair (which can have been removed from any map), disregarding the key entry exists or not. If it does exists, the old pair is removed and returned.

An interactive reinsertion. A closure and a previously removed entry is provided. The closure will check the status of the insertion and then return whether the condition met with the expected ones. The closure may be called multiple times, as different things are discovered by the map implementation.

The first argument of the closure is the provided removed entry. The second argument is the pair found in the map's stored entry. If there was no entry, None is passed.

Gets a reference to the mapped value of a key, it exists. Then, it calls the reader function argument with the reference. Please note that returning a reference would imply in pausing any sensitive incinerator resource deallocation for indefinite time.

Same as get, but calls the reader function argument with key and value, respectively, instead.

Removes the given entry identified by the given key.

Important traits for Iter<'map, K, V, R>

Iterates over the map with a given reader. The reader must be a function/closure. This methods is just a specific version of iter_with_reader due to Rust limitations on inference of closures polymorphic on lifetimes.

Important traits for Iter<'map, K, V, R>

Iterates over the map with a given reader. The reader may be a closure, primitive function, or any other type that implements the reader trait.

Important traits for &'a mut R

The hasher builder with which this map was created.

Removes any unnecessary sub-table. This operation cannot be performed with a shared map, and the mutable reference required by this method ensures that.

Trait Implementations

impl<K, V, H> Drop for Map<K, V, H>
[src]

Executes the destructor for this type. Read more

impl<K, V, H> Default for Map<K, V, H> where
    H: BuildHasher + Default
[src]

Returns the "default value" for a type. Read more

impl<K, V, H> Send for Map<K, V, H> where
    K: Send + Sync,
    V: Send + Sync,
    H: Send
[src]

impl<K, V, H> Sync for Map<K, V, H> where
    K: Send + Sync,
    V: Send + Sync,
    H: Sync
[src]

impl<K, V, H> Debug for Map<K, V, H> where
    H: Debug
[src]

Formats the value using the given formatter. Read more