JSで簡単で実用的なHashMapを実現しました.

19797 ワード

jsにはmapという特性がありますが、とても使いやすいです.何気なくgirudbaogeブロガーの文章(http://blog.csdn.net/guirudaoge/article/details/8433559)の中でその実現方式を見て、よく書けていると思います.ここで敬意を表します.同時にプロジェクトの中で修正して、共有します.
原文のコードは:
function HashMap(){
    var size = 0;// Map  
    var entry = new Object();//   

    // Map  put  
    this.put = function(key, value) {
        if (!this.containsKey(key)) {
            size++;
            entry[key] = value;
        }
    }

    // Map get  
    this.get = function(key) {
        return this.containsKey(key) ? entry[key] : null;
    }

    // Map  remove
    this.remove = function(key) {
        if (this.containsKey(key) && (delete entry[key])) {
            size--;
        }
    }

    //     Key
    this.containsKey = function(key) {
        return (key in entry);
    }
    //     Value
    this.containsValue = function(value) {
        for ( var prop in entry) {
            if (isObjectValueEqual(entry[prop], value)) {
                return true;
            }
        }
        return false;
    }

    //    Value
    this.values = function() {
        var values = new Array();
        for ( var prop in entry) {
            values.push(entry[prop]);
        }
        return values;
    }

    //     Key
    this.keys = function() {
        var keys = new Array();
        for ( var prop in entry) {
            keys.push(prop);
        }
        return keys;
    }

    // Map size
    this.size = function() {
        return size;
    }

    //   Map
    this.clear = function() {
        size = 0;
        entry = new Object();
    }
}
実は普通の需要に対して、これはもういいです.変更後のコード:
/* 
 *hashmap 1.0.0
 * description:this haspmap is a data structure which is used in the js file and it can be store data whatever you want.
 *
 */

var HashMap=function(){

    var size = 0;// Map  
    var entry = new Object();//   

    this.common=new common(); //  common      

    // Map  put  
    this.put = function(key, value) {
        if (!this.containsKey(key)) {
            size++;
            entry[key] = value;
        }
    }

    // Map get  
    this.get = function(key) {
        return this.containsKey(key) ? entry[key] : null;
    }

    // Map  remove
    this.remove = function(key) {
        if (this.containsKey(key) && (delete entry[key])) {
            size--;
        }
    }

    //     Key
    this.containsKey = function(key) {
        return (key in entry);
    }
    //     Value
    this.containsValue = function(value) {
        for ( var prop in entry) {
            if (isObjectValueEqual(entry[prop], value)) {
                return true;
            }
        }
        return false;
    }

    //    Value
    this.values = function() {
        var values = new Array();
        for ( var prop in entry) {
            values.push(entry[prop]);
        }
        return values;
    }

    //     Key
    this.keys = function() {
        var keys = new Array();
        for ( var prop in entry) {
            keys.push(prop);
        }
        return keys;
    }

    // Map size
    this.size = function() {
        return size;
    }

    //   Map
    this.clear = function() {
        size = 0;
        entry = new Object();
    }

    //   key By value
    this.getKeyByValue = function(value) {
        for ( var prop in entry) {
            if (this.common.isObjectValueEqual(entry[prop], value)) {
                console.log("getKeyByValue is ok");
                return prop;
            }
        }
        return null;
    }

    //            keys
    this.specialKeys = function(containsID) {
        var keys = new Array();
        var object_container = new Array();
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            console.log(this.common.isPropertyInObject(values[i], containsID));
            if (this.common.isPropertyInObject(values[i], containsID)) {
                object_container.push(values[i]);
            }
        }
        console.log("object_container.length", object_container.length);
        for (var j = 0; j < object_container.length; j++) {
            keys.push(this.getKeyByValue(object_container[j]));
        }
        return keys;
    }

    //                 
    this.findWeekObjectInHash = function(obj) {
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (this.common.isObjectValueEqualIgnoreSequence(obj, values[i])) {
                return true;
            }
        }
        return false;
    }

    //            keys
    this.specialKeysSequence = function(containsID, start_end) {
        var keys = new Array();
        var object_container = new Array();
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (this.common.isPropertyValueInAndEqualObjectPropertyValue(values[i],
                    start_end, containsID)) {
                object_container.push(values[i]);
            }
        }
        for (var j = 0; j < object_container.length; j++) {
            keys.push(this.getKeyByValue(object_container[j]));
        }
        return keys;
    }

    //                  hashmap  value(    )
    this.findObjectByPropety = function(ID) {
        var value = this.values();
        for (var i = 0; i < value.length; i++) {
            if (this.common.isPropertyInObject(value[i], ID)) {
                return value[i];
            }
        }
        return null;
    }
}
その中の対象は以下の通りです.
var common=function(){

    //           
    this.isObjectValueEqual=function (a, b) {
        var aProps = Object.getOwnPropertyNames(a);
        var bProps = Object.getOwnPropertyNames(b);
        if (aProps.length != bProps.length) {
            return false;
        }
        for (var i = 0; i < aProps.length; i++) {
            var propName = aProps[i];
            if (a[propName] !== b[propName]) {
                return false;
            }
        }
        return true;
    }

    /*
     * @description:           (         ,      @example:A.x=1,A.y=2 A.x=2,A.y=1    
     * 
     */
    this.isObjectValueEqualIgnoreSequence=function (a, b) {
        var flag = true;
        var aProps = Object.getOwnPropertyNames(a);
        var bProps = Object.getOwnPropertyNames(b);
        if (aProps.length != bProps.length) {
            return false;
        }
        for (var i = 0; i < aProps.length; i++) {
            if (!isPropertyInObject(b, a[aProps[i]])) {
                flag = false;
            }
        }
        return flag;
    }

    //                 
    this.isPropertyInObject=function (object, value) {
        for ( var i in object) {
            if (object[i] == value) {
                return true;
            }
        }
        return false;
    }

    //                        
    this.isPropertyValueInAndEqualObjectPropertyValue=function (object, prop, value) {
        if (prop in object) {
            if (object[prop] == value) {
                return true;
            }
        }
        return false;
    }

    //                   
    this.FindPropertyValueInObjectWithOtherPropertyValue=function (object, prop1,
            prop2, value) {
        if (prop1 in object) {
            if (object[prop1] == value) {
                return object[prop2];
            }
        }
        return false;
    }
}
参考ブログ:http://blog.csdn.net/guirudaoge/article/details/8433559