/*
* MAP object, implements MAP function
*
* Interface:
* size() Gets the number of MAP elements
* isEmpty() Determines whether the MAP is Is empty
* clear() deletes all elements of MAP
* put(key, value) adds elements (key, value) to MAP
* remove(key) deletes the elements of the specified KEY, returns True if successful , Returns False on failure
* get(key) Gets the element value VALUE of the specified KEY, returns NULL on failure
* element(index) Gets the element at the specified index (use element.key, element.value to get KEY and VALUE) , returns NULL on failure
* containsKey(key) Determine whether the MAP contains the element of the specified KEY
* containsValue(value) Determine whether the MAP contains the element of the specified VALUE
* values() Get all VALUE in the MAP Array (ARRAY)
* keys() Get the array (ARRAY) of all KEYs in MAP
*
* Example:
* var map = new Map();
*
* map.put("key", "value");
* var val = map.get("key")
* ……
*
*/
function Map() {
this.elements = new Array();
//Get the number of MAP elements
this.size = function () {
return this.elements.length;
};
//Determine whether the MAP is empty
this.isEmpty = function() {
return (this.elements .length < 1);
};
//Delete all elements of MAP
this.clear = function() {
this.elements = new Array();
};
//Add elements (key, value) to MAP
this.put = function(_key, _value) {
this.elements.push( {
key : _key ,
value : _value
});
};
//Delete the element of the specified KEY, return True if successful, False if failed
this.removeByKey = function(_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].key == _key) {
this.elements.splice(i, 1);
return true;
}
}
} catch (e) {
bln = false;
}
return bln;
};
//Delete the element of the specified VALUE, return True if successful, False if failed
this.removeByValue = function(_value) {//removeByValueAndKey
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].value == _value) {
this.elements.splice(i, 1);
return true;
}
}
} catch (e) {
bln = false;
}
return bln;
};
//Delete the element of the specified VALUE, return True if successful, False if failed
this.removeByValueAndKey = function(_key,_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].value == _value && this.elements[i ].key == _key) {
this.elements.splice(i, 1);
return true;
}
}
} catch (e) {
bln = false;
}
return bln;
};
//Get the element value VALUE of the specified KEY, and return NULL on failure
this.get = function(_key) {
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].key == _key) {
return this.elements[ i].value;
}
}
} catch (e) {
return false;
}
return false;
};
/ /Get the element at the specified index (use element.key, element.value to get KEY and VALUE), return NULL on failure
this.element = function(_index) {
if (_index < 0 || _index > = this.elements.length) {
return null;
}
return this.elements[_index];
};
//Determine whether the MAP contains the specified KEY Element
this.containsKey = function(_key) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].key == _key) {
bln = true;
}
}
} catch (e) {
bln = false;
}
return bln;
};
//Determine whether the MAP contains the element of the specified VALUE
this.containsValue = function(_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].value == _value) {
bln = true;
}
}
} catch (e) {
bln = false;
}
return bln;
};
//Judge whether in MAP Element containing the specified VALUE
this.containsObj = function(_key,_value) {
var bln = false;
try {
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].value == _value && this.elements[i].key == _key) {
bln = true;
}
}
} catch (e) {
bln = false;
}
return bln;
};
//Get the array (ARRAY) of all VALUE in MAP
this.values = function() {
var arr = new Array();
for (i = 0; i < this.elements.length; i ) {
arr.push(this.elements [i].value);
}
return arr;
};
//Get the array (ARRAY) of all VALUE in MAP
this.valuesByKey = function(_key ) {
var arr = new Array();
for (i = 0; i < this.elements.length; i ) {
if (this.elements[i].key == _key ) {
arr.push(this.elements[i].value);
}
}
return arr;
};
//Get all items in MAP Array of KEY (ARRAY)
this.keys = function() {
var arr = new Array();
for (i = 0; i < this.elements.length; i ) {
arr.push(this.elements[i].key);
}
return arr;
};
//Get key through value
this.keysByValue = function(_value) {
var arr = new Array();
for (i = 0; i < this.elements.length; i ) {
if(_value == this.elements[i ].value){
arr.push(this.elements[i].key);
}
}
return arr;
};
//Get Array of all KEYs in MAP (ARRAY)
this.keysRemoveDuplicate = function() {
var arr = new Array();
for (i = 0; i < this.elements.length; i ) {
var flag = true;
for(var j=0;j
if(arr[j] == this.elements[i].key){
flag = false;
break;
}
}
if(flag){
arr.push(this.elements[i].key);
}
}
return arr;
};
}