ocean.util.container.FixedKeyMap

Map template with a fixed set of keys.

Map template with a fixed set of keys, specified in the class' constructor. If an item is added whose key is not in the fixed set, an exception is thrown.

Such a map can be faster than a standard hash map, as the fixed set of possible keys means that a fast binary search can be used to find the index of the corresponding value. This of course only holds true when the time taken to generate a hash from a key would be slower than the time taken to do a binary search over the key. In the case of char[] keys, tests have shown that for keys of 5 characters or longer, the FixedKeyMap starts to be faster than the StandardKeyHashingMap, and that in the case of long keys (100 characters) it is an order of magnitude faster.

Usage example:

import ocean.util.container.FixedKeyMap;

// Create map instance
auto map = new FixedKeyMap!(char[], char[])("first", "second", "third");

// Add and check an entry
map["first"] = "hello";
assert(map["first"] == "hello");

// Example of adding an entry which will be rejected
try
{
    map["fifth"] = "should fail";
}
catch ( map.FixedKeyMapException e )
{
    // expected
}

// Example of checking if a key is in the map (this does not throw an
// exception if the key is not found)
auto nine = "ninth" in map;

Members

Classes

FixedKeyMap
class FixedKeyMap(K, V)

Fixed key map class template.

Meta

License

Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. Alternatively, this file may be distributed under the terms of the Tango 3-Clause BSD License (see LICENSE_BSD.txt for details).