HashMap

Safe version of UnsafeHashMap.

Uses reference counting to avoid manual deallocation problems (i.e. double free) and never exposes references to its internal storage (so the API is similar but not the same). Again, this type uses reference counting, so cycles must be avoided to ensure memory doesn't leak.

Members

Functions

get
inout(Value) get(const(Key) key, const(Default) defaultValue)

The safe version matches AA's get.

opBinaryRight
bool opBinaryRight(const(Key) key)

Changed to safely return a bool instead of an internal pointer.

Examples

HashMap!(char, long) outer;
outer['o'] = 0; // outer -> { 'o': 0 }

{
    HashMap!(char, long) inner;
    inner['i'] = 1; // inner -> { 'i': 1 }
    outer = inner;
    // with the previous assignment, { 'o': 0 } was deallocated
}

// as inner goes out of scope, it decreases the ref count of { 'i': 1 }
// but since outer still holds a reference to it, it wasn't deallocated
assert(outer['i'] == 1); // outer -> { 'i': 1 }
assert('o' !in outer);