spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / gr / column17 / 1 To page 1To page 2current page
[previous]

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


JavaScript STL (Standard Template Library), Part 4

Concrete Classes

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.

Demo

Click here to show demo.

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.

Conclusion

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 To page 1To page 2current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: December 16, 2005

URL: http://webreference.com/programming/javascript/gr/column17/1