randaasealed

A sealed, reference counted hash table implementation that uses randomized probing for collision resolution. This struct has the following advantages over D's builtin associative arrays:

1. Deterministic, ref counted freeing of memory, which is important for huge AAs or very memory-tight systems.

2. Plays nicely with conservative GC. Keys, values and hashes are in separate arrays, meaning that they don't all get GC scanned just because one needs to be GC scanned. This also saves on alignment overhead.

3. The implementation is based on parallel arrays, and allows for space to be reserved such that a certain number of elements can be added with a guarantee of no reallocation. Because of this, insertions are much faster for large arrays and don't interfere with multithreading.

4. The linear congruential probing retains its O(1) expected lookup time as long as there are minimal collisions in full (32- or 64-bit) hash space, regardless of how many collisions there are in modulus hash space.

The main disadvantages are:

1. The combination of parallel arrays and randomized probing wreaks havoc on cache performance, meaning lookups are somewhat slower than with the builtin AA.

2. The worst-case lookup time is unbounded, though this is more theoretical trivia than a practical issue (except maybe in high security situations where a hash table is not the proper data structre anyhow). The distribution of lookup times is geometric, meaning that large lookup times are extremely improbable.

Known Bugs/TODOs:

Const correctness.

Finding good linear congruential parameters for 64-bit size_t/hash_t.

Copyright (C) 2009-2010 David Simcha

License:
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:

The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class KeyError : object.Exception;
Exception thrown on missing keys.

struct RandAARange (alias fun,T);
Forward range to iterate over keys or values of a RandAA. Note that this range is invalidated by any changes made to the underlying RandAA.

struct RandAA (K,V,bool storeHash = shouldStoreHash!(K));
An associative array class that uses randomized probing and open addressing. K is the key type, V is the value type, storeHash determines whether the hash of each key is stored in the array. This increases space requirements, but allows for faster rehashing. By default, the hash is stored unless the array is an array of floating point or integer types.

__ctor ;
Constructor to reserve a pre-specified amount of space.

void rehash ();


@property size_t capacity ();
The number of elements that can be in the array without triggering a reallocation.

void reserve (size_t newSize);
Reserves enough space to guarantee that no reallocation will be necessary until (newSize - length) elements are added.

V opIndex (K index);


V opIndexAssign (V val, K index);


template opIndexOpAssign (string op)


V opIndexOpAssign (V value, K index);


V get (K index, lazy V defaultVal);


V remove (K index);


bool opIn_r (K index);


@property size_t length ();


@property RandAARange!(extractKeys,Data) byKey ();
Iterate lazily over the keys. Note that adding or removing a key invalidates the range.

@property RandAARange!(extractVals,Data) byValue ();
Iterate lazily over the values. Note that adding or removing a value invalidates the range.

int opApply (int delegate(ref K, ref V) dg);
Efficiently iterate over keys and values in lockstep.

BUGS:
Escapes references because there's no way to avoid this with opApply . See DMD Bug 2443.

@property K[] keys ();
Eagerly create an array of keys .

@property V[] values ();
Eagerly create an array of values .

Page was generated with on Sat Jan 29 16:27:34 2011