home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Minami 80
/
MINAMI80.iso
/
Extra
/
DivXInstaller.exe
/
$PLUGINSDIR
/
GoogleToolbarFirefox.msi
/
xpi
/
amulet-jslib
/
firefox
/
objectsafemap.js
< prev
next >
Wrap
Text File
|
2006-05-15
|
3KB
|
99 lines
function G_ObjectSafeMap(opt_name) {
this.debugZone = "objectsafemap";
this.name_ = opt_name ? opt_name : "noname";
this.keys_ = [];
this.values_ = [];
}
G_ObjectSafeMap.prototype.indexOfKey_ = function(key) {
for (var i = 0; i < this.keys_.length; i++)
if (this.keys_[i] === key)
return i;
return -1;
}
G_ObjectSafeMap.prototype.insert = function(key, value) {
if (key === null)
throw new Error("Can't use null as a key");
if (value === undefined)
throw new Error("Can't store undefined values in this map");
var i = this.indexOfKey_(key);
if (i == -1) {
this.keys_.push(key);
this.values_.push(value);
} else {
this.keys_[i] = key;
this.values_[i] = value;
}
G_Assert(this, this.keys_.length == this.values_.length,
"Different number of keys than values!");
}
G_ObjectSafeMap.prototype.erase = function(key) {
var keyLocation = this.indexOfKey_(key);
var keyFound = keyLocation != -1;
if (keyFound) {
this.keys_.splice(keyLocation, 1);
this.values_.splice(keyLocation, 1);
}
G_Assert(this, this.keys_.length == this.values_.length,
"Different number of keys than values!");
return keyFound;
}
G_ObjectSafeMap.prototype.find = function(key) {
var keyLocation = this.indexOfKey_(key);
return keyLocation == -1 ? undefined : this.values_[keyLocation];
}
G_ObjectSafeMap.prototype.replace = function(other) {
this.keys_ = [];
this.values_ = [];
for (var i = 0; i < other.keys_.length; i++) {
this.keys_.push(other.keys_[i]);
this.values_.push(other.values_[i]);
}
G_Assert(this, this.keys_.length == this.values_.length,
"Different number of keys than values!");
}
G_ObjectSafeMap.prototype.forEach = function(func) {
if (typeof func != "function")
throw new Error("argument to forEach is not a function, it's a(n) " +
typeof func);
for (var i = 0; i < this.keys_.length; i++)
func(this.keys_[i], this.values_[i]);
}
G_ObjectSafeMap.prototype.size = function() {
return this.keys_.length;
}
function TEST_G_ObjectSafeMap() {
if (G_GDEBUG) {
var z = "map UNITTEST";
G_debugService.enableZone(z);
G_Debug(z, "Starting");
var m = new G_ObjectSafeMap();
G_Assert(z, m.size() == 0, "Initial size not zero");
var o1 = new Object;
var v1 = "1";
var o2 = new Object;
var v2 = "1";
G_Assert(z, m.find(o1) == undefined, "Found non-existent item");
m.insert(o1, v1);
m.insert(o2, v2);
G_Assert(z, m.size() == 2, "Size not 2");
G_Assert(z, m.find(o1) == "1", "Didn't find item 1");
G_Assert(z, m.find(o2) == "1", "Didn't find item 1");
m.insert(o1, "2");
G_Assert(z, m.size() == 2, "Size not 2");
G_Assert(z, m.find(o1) == "2", "Didn't find item 1");
G_Assert(z, m.find(o2) == "1", "Didn't find item 1");
m.erase(o1);
G_Assert(z, m.size() == 1, "Size not 1");
G_Assert(z, m.find(o1) == undefined, "Found item1");
G_Assert(z, m.find(o2) == "1", "Didn't find item 2");
m.erase(o1);
G_Assert(z, m.size() == 1, "Size not 1");
G_Assert(z, m.find(o1) == undefined, "Found item1");
G_Assert(z, m.find(o2) == "1", "Didn't find item 2");
m.erase(o2);
G_Assert(z, m.size() == 0, "Size not 0");
G_Assert(z, m.find(o2) == undefined, "Found item2");
G_Debug(z, "PASSED");
}
}