spacer

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

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

Software Developer / Programmer - Interview NOW!
Next Step Systems
US-IL-Des Plaines

Justtechjobs.com Post A Job | Post A Resume
Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies


The JavaScript STL (Standard Template Library). Part 2

The deque collection

The deque is almost identical to the vector but with the added advantage of being able to add and remove elements from the front end efficiently.

function STDDeque()
{
  this.data = new Array();
  this.nBegin = 0;
  this.nEnd = 0;
  this.type = "deque";
 
  // add a few specializations...
  this.push_front = STDDeque_push_front;
  this.pop_front = STDDeque_pop_front;
}
// inherit from STDVector
STDDeque.prototype = STDVector.prototype;

In the code, it "inherits" most of its behaviour from the vector code by redirecting its prototype and then attaching its two extra methods in the constructor:

STDDeque_push_front = function(value)
{
  this.data[--this.nBegin] = value;
}
STDDeque_pop_front = function()
{
  if ( this.nEnd > this.nBegin ) delete this.data[this.nBegin++];
}

The push_front() and pop_front() look very similar to the push_back() and pop_back() methods except that they modify the array's start point.

Demo

Download the code

This textbox contains the complete source for the two collections (list and vector) discussed in this article. Copy this code into a file called “JSstl.js” to use with the demo.

This textbox contains a short demonstration of how lists and vectors may be used on a web page.

Conclusion

In this article I've introduced a JavaScript implementation of the C++ Standard Template Library. This library offers a consistent interface with well defined behaviours. The JavaScript STL will never compete in performance with either the C++ version or the native JavaScript collections such as the Array or Object. It is implemented in script but it does offer flexibility and standardization that are not always seen in JavaScript.

In the next issue I will introduce more collections from the STL repertoire and delve deeper into how iterators can do more than just iterate.

Disclaimer

While I have endeavored to make this code as browser compatible as possible, I have only tested it with Internet Explorer (6.0) and Netscape (7.1) as this represents of a large proportion of users.

About the Author

Guyon Roche is a freelance web developer in London, Great Britain. He specializes in Windows platforms with an interest in bridging the gaps between different technologies, visit http://www.silver-daggers.co.uk for more details. He can be reached via e-mail at guyon.roche@btinternet.com.

home / programming / javascript / gr / column14 / 1 To page 1current 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 >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji

Created: March 27, 2003
Revised: August 29, 2005

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