| home / programming / javascript / gr / column17 / 1 | [previous] |
|
|
The concrete classes may now be defined to inherit from the implementation of the STDEXTHash class adding only the differences that differentiate each collection type.
//=============================================================================
// STDEXTHashMap
function STDEXTHashMap(hash,less)
{
// add special map behaviour
this.createNode = STDEXTHashMap_createNode;
// initialise hash_map settings
this.type = "hash_map";
this.bMultiContainer = false;
// initialise STDEXTHash settings
this.initialise(hash,less);
}
// inherit behaviour from STDEXTHash
STDEXTHashMap.prototype = STDEXTHash.prototype;
STDEXTHashMap_createNode = function(item)
{
// in a map, the key is item.first
return {item:item,key:item.first,next:null,prev:null};
}
The STDEXTHashMap class implements the hash_map collection. After copying the prototype of the STDEXTHash class, it implements this.createNode() function so that the STDEXTHash code will be able to create a node from a supplied value object. The structure of a node requires an item property that contains the value, a key property which is used to compare and hash and next and prev pointers for use in the linked list. In the map type collections, the key is found in the "first" property of the value.
The STDEXTHashHashMultiMap class is almost identical to the STDEXTHashMap class with the exception of the type and isMultiContainer properties which are set to "hash_multimap" and true respectively...
//=============================================================================
//
STDEXTHashMultiMap
function
STDEXTHashMultiMap(hash,less)
{
// add special map behaviour
this.createNode =
STDEXTHashMap_createNode;
// initialise hash_multimap
settings
this.type = "hash_multimap";
this.bMultiContainer = true;
// initialise STDEXTHash
settings
this.initialise(hash,less);
}
}
//
inherit behaviour from STDEXTHash
STDEXTHashMultiMap.prototype = STDEXTHash.prototype;
STDEXTHashSet and STDEXTHashMultiSet follow a very similar pattern...
//=============================================================================
// STDEXTHashSet
function STDEXTHashSet(hash,less)
{
// initialise hash_set settings
this.type = "hash_set";
this.bMultiContainer = false;
// initialise STDEXTHash settings
this.initialise(hash,less);
}
// inherit behaviour from STDEXTHash
STDEXTHashSet.prototype = STDEXTHash.prototype;
//=============================================================================
//
STDEXTHashMultiSet
function
STDEXTHashMultiSet(hash,less)
{
// initialise map settings
this.type = "hash_multiset";
this.bMultiContainer = true;
// initialise STDEXTHash
settings
this.initialise(hash,less);
}
//
inherit behaviour from STDEXTHash
STDEXTHashMultiSet.prototype
= STDEXTHash.prototype;
In this case the hash_set and hash_multiset can use the default createNode() implementation implemented in STDEXTHash. All that's necessary is to set the type and bMultiContainer properties accordingly.
The demo code above is intended to demonstrate the interchangeability of the different STL collections. One function...
function testSTL(c,title,displayItem,aItems,bMulti)
{
// test a STL collection
// c: the collection to test
//
title: the title of this test
//
displayItem: a function that will display
items
//
aItems: an array of items
// bMulti: whether the collection is a multi collection or not.
...
}
is used to test the map, multimap, hash_map, hash_multimap, set, multiset, hash_set and hash_multiset types. The individual tests for each of these types pass in their collection type followed by apropriate arguments and thanks to the similarity of interface, testSTL() will test them all.
In this article I presented four new collections based on hash tables. These collections are held in a different namespace from the rest of STL called stdext. The four collections share a single implementation in a class called STDEXTHash, adding their own differences in their respective constructors.
The demo code is in the form of a unit test that helps demonstrate how easy it is to exchange different implementations of STL collections depending on the need for performance, memory or ordering.
| home / programming / javascript / gr / column17 / 1 | [previous] |
URL: http://webreference.com/programming/javascript/gr/column17/1