javascript HashMap 线性探索 解决冲突

* HashMap.js

/**
 * Created by Mch on 8/27/18.
 */
function KeyValuePair(key, value) {
    this.key = key;
    this.value = value;
}

KeyValuePair.prototype.toString = function() {
    return '['+ this.key +'-'+ this.value +']';
};

function HashMap() {
    this.map = [];
}

// 2 hash functions
var hash = {
    loselose: function(key) {
        var hash = 0;
        for (var i = 0; i < key.length; i++) {
            hash += key.charCodeAt(i);
        }
        return hash % 37;
    },
    djb2: function(key) {
        var hash = 5381;
        for (var i = 0; i < key.length; i++) {
            hash = hash * 33 + key.charCodeAt(i);
        }
        return hash % 1013;
    }
};

// pick a hash function
HashMap.hashCode = hash.loselose;

// 线性探索
HashMap.prototype = {
    put: function(key, value) {
        var position = HashMap.hashCode(key);

        if (this.map[position] === undefined) {
            this.map[position] = new KeyValuePair(key, value);
        } else {
            var index = ++position;
            while (this.map[index] !== undefined) {
                index++;

                // Array index out of bounds! realloc
                if (index >= this.map.length) {
                    var a = new Array(this.map.length*2);
                    console.debug("realloc - " + a.length);
                    this.map.forEach(function(e, i) {
                        a[i] = e;
                    });
                    delete this.map;
                    this.map = a;
                }
            }
            this.map[index] = new KeyValuePair(key, value);
        }
    },
    get: function(key) {
        var position = HashMap.hashCode(key);
        if (this.map[position] !== undefined) {
            if (this.map[position].key === key) {
                return this.map[position].value;
            } else {
                var index = ++position;
                while (index < this.map.length &&
                  (this.map[index] === undefined || this.map[index].key !== key))
                {
                    index++;
                }
                if (index >= this.map.length || this.map[index] === undefined) {
                    return undefined;
                }
                if (this.map[index].key === key) {
                    return this.map[index].value;
                }
            }
        }
        return undefined;
    },
    remove: function(key) {
        var position = HashMap.hashCode(key);
        if (this.map[position] === undefined) {
            return false;
        }
        if (this.map[position].key === key) {
            this.map[position] = undefined;
        } else {
            var index = ++position;
            while (this.map[index] === undefined ||
            this.map[index].key !== key) {
                index++;
            }
            if (this.map[index].key === key) {
                this.map[index] = undefined;
            }
        }
        return true;
    },

    print: function() {
        for (var i = 0; i < this.map.length; ++i) {
            if (this.map[i] !== undefined) {
                console.log(i + ": " + this.map[i]);
            }
        }
    }
};

exports.HashMap = HashMap;

* TestHashMap.js

/**
 * Created by Mch on 8/27/18.
 */
var HashMap = require('./Collection/HashMap').HashMap;

function TestHashMap() {}

TestHashMap.main = function() {
    var hash = new HashMap();

    hash.put('Gandalf', '[email protected]');
    hash.put('John', '[email protected]');
    hash.put('Tyrion', '[email protected]');
    hash.put('Aaron', '[email protected]');
    hash.put('Donnie', '[email protected]');
    hash.put('Ana', '[email protected]');
    hash.put('Jonathan', '[email protected]');
    hash.put('Jamie', '[email protected]');
    hash.put('Sue', '[email protected]');
    hash.put('Mindy', '[email protected]');
    hash.put('Paul', '[email protected]');
    hash.put('Nathan', '[email protected]');

    hash.print();

    console.log(hash.get('Sue'));

    hash.remove('Sue');
    console.log(hash.get('Sue'));
};

TestHashMap.main();

5: [[email protected]]
6: [[email protected]]
7: [[email protected]]
10: [[email protected]]
13: [[email protected]]
14: [[email protected]]
16: [[email protected]]
17: [[email protected]]
19: [[email protected]]
29: [[email protected]]
32: [[email protected]]
33: [[email protected]]
[email protected]
undefined

* 分离链接法

https://blog.csdn.net/fareast_mzh/article/details/82085749

猜你喜欢

转载自blog.csdn.net/fareast_mzh/article/details/82120675