PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.3.2/jsdoc-toolkit/app/frame/Hash.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 84 lines | 60 code | 11 blank | 13 comment | 18 complexity | 611fac9de45ba885e04052402b352072 MD5 | raw file
 1/**
 2	@constructor
 3	@example
 4	var _index = new Hash();
 5	_index.set("a", "apple");
 6	_index.set("b", "blue");
 7	_index.set("c", "coffee");
 8
 9	for (var p = _index.first(); p; p = _index.next()) {
10		print(p.key+" is for "+p.value);
11	}
12	
13 */
14var Hash = function() {
15	this._map = {};
16	this._keys = [];
17	this._vals = [];
18	this.reset();
19}
20
21Hash.prototype.set = function(k, v) {
22	if (k != "") {
23		this._keys.push(k);
24		this._map["="+k] = this._vals.length;
25		this._vals.push(v);
26	}
27}
28
29Hash.prototype.replace = function(k, k2, v) {
30	if (k == k2) return;
31
32	var offset = this._map["="+k];
33	this._keys[offset] = k2;
34	if (typeof v != "undefined") this._vals[offset] = v;
35	this._map["="+k2] = offset;
36	delete(this._map["="+k]);
37}
38
39Hash.prototype.drop = function(k) {
40	if (k != "") {
41		var offset = this._map["="+k];
42		this._keys.splice(offset, 1);
43		this._vals.splice(offset, 1);
44		delete(this._map["="+k]);
45		for (var p in this._map) {
46			if (this._map[p] >= offset) this._map[p]--;
47		}
48		if (this._cursor >= offset && this._cursor > 0) this._cursor--;
49	}
50}
51
52Hash.prototype.get = function(k) {
53	if (k != "") {
54		return this._vals[this._map["="+k]];
55	}
56}
57
58Hash.prototype.keys = function() {
59	return this._keys;
60}
61
62Hash.prototype.hasKey = function(k) {
63	if (k != "") {
64		return (typeof this._map["="+k] != "undefined");
65	}
66}
67
68Hash.prototype.values = function() {
69	return this._vals;
70}
71
72Hash.prototype.reset = function() {
73	this._cursor = 0;
74}
75
76Hash.prototype.first = function() {
77	this.reset();
78	return this.next();
79}
80
81Hash.prototype.next = function() {
82	if (this._cursor++ < this._keys.length)
83		return {key: this._keys[this._cursor-1], value: this._vals[this._cursor-1]};
84}