1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| <script language="JavaScript" >
alert("jjjj");
Hashtable.prototype.hash = null;
Hashtable.prototype.keys = null;
Hashtable.prototype.location = null;
/**
* Hashtable - Constructor
* Create a new Hashtable object.
*/
function Hashtable(){
this.hash = new Array();
this.keys = new Array();
this.location = 0;
}
/**
* put
* Add new key
* param: key - String, key name
* param: value - Object, the object to insert
*/
Hashtable.prototype.put = function (key, value){
if (value == null)
return;
if (this.hash[key] == null)
this.keys[this.keys.length] = key;
this.hash[key] = value;
}
/**
* get
* Return an element
* param: key - String, key name
* Return: object - The requested object
*/
Hashtable.prototype.get = function (key){
return this.hash[key];
}
/**
* remove
* Remove an element
* param: key - String, key name
*/
Hashtable.prototype.remove = function (key){
for (var i = 0; i < this.keys.length; i++){
//did we found our key?
if (key == this.keys[i]){
//remove it from the hash
this.hash[this.keys[i]] = null;
//and throw away the key...
this.keys.splice(i ,1);
return;
}
}
}
/**
* size
* Return: Number of elements in the hashtable
*/
Hashtable.prototype.size = function (){
return this.keys.length;
}
/**
* populateItems
* Deprecated
*/
Hashtable.prototype.populateItems = function (){}
/**
* next
* Return: true if theres more items
*/
Hashtable.prototype.next = function (){
if (++this.location < this.keys.length)
return true;
else
return false;
}
/**
* moveFirst
* Move to the first item.
*/
Hashtable.prototype.moveFirst = function (){
try {
this.location = -1;
} catch(e) {/*//do nothing here :-)*/}
}
/**
* moveLast
* Move to the last item.
*/
Hashtable.prototype.moveLast = function (){
try {
this.location = this.keys.length - 1;
} catch(e) {/*//do nothing here :-)*/}
}
/**
* getKey
* Return: The value of item in the hash
*/
Hashtable.prototype.getKey = function (){
try {
return this.keys[this.location];
} catch(e) {
return null;
}
}
/**
* getValue
* Return: The value of item in the hash
*/
Hashtable.prototype.getValue = function (){
try {
return this.hash[this.keys[this.location]];
} catch(e) {
return null;
}
}
/**
* getKey
* Return: The first key contains the given value, or null if not found
*/
Hashtable.prototype.getKeyOfValue = function (value){
for (var i = 0; i < this.keys.length; i++)
if (this.hash[this.keys[i]] == value)
return this.keys[i]
return null;
}
/**
* toString
* Returns a string representation of this Hashtable object in the form of a set of entries,
* enclosed in braces and separated by the ASCII characters ", " (comma and space).
* Each entry is rendered as the key, an equals sign =, and the associated element,
* where the toString method is used to convert the key and element to strings.
* Return: a string representation of this hashtable.
*/
Hashtable.prototype.toString = function (){
try {
var s = new Array(this.keys.length);
s[s.length] = "{";
for (var i = 0; i < this.keys.length; i++){
s[s.length] = this.keys[i];
s[s.length] = "=";
var v = this.hash[this.keys[i]];
if (v)
s[s.length] = v.toString();
else
s[s.length] = "null";
if (i != this.keys.length-1)
s[s.length] = ", ";
}
} catch(e) {
//do nothing here :-)
}finally{
s[s.length] = "}";
}
return s.join("");
}
/**
* add
* Concatanates hashtable to another hashtable.
*/
Hashtable.prototype.add = function(ht){
try {
ht.moveFirst();
while(ht.next()){
var key = ht.getKey();
//put the new value in both cases (exists or not).
this.hash[key] = ht.getValue();
//but if it is a new key also increase the key set
if (this.get(key) != null){
this.keys[this.keys.length] = key;
}
}
} catch(e) {
//do nothing here :-)
} finally {
return this;
}
};
</script> |
Partager