| home / programming / javascript / gr / column14 / 1 | [previous] |
|
|
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.
In this demo, you can compile a sequence of items by inserting new items at the start, end or current iterator position. The iterator can be adjusted using the Advance and Retreat buttons. Use the assign button to modify the value at the current iterator position.
This demo is similar to the vector demo with the added advantage of prepending items at the start.
This demo shows the advantage of using vectors over lists. Items are only added and removed at the end of the vector however any item in the vector can be accessed by position.
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.
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.
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.
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 | [previous] |
Created: March 27, 2003
Revised: August 29, 2005
URL: http://webreference.com/programming/javascript/gr/column14/1