学习Redis你必须了解的数据结构——HashMap实现

本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接博客园蜗牛 cnblogs.com\tdws .

首先提供一种获取hashCode的方法,是一种比较受欢迎的方式,该方法参照了一位园友的文章,链接在尾部给出:

 var djb2Code = function (str) {
var hash = 5381;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
}
return hash;
}

接下来我们用js实现hashmap, hashmap是一种键值对的数据结构。意味着你可以通过key快速找到你所需要查找的值。我使用数组加上LinkedList来实现hashmap,这种方式也被称为解决hashcode冲突的分离链接法。hashmap通常具备以下几种方法:put,get,remove。put是写入和修改数据,在put数据时,首先获取key的hashcode,作为数组的索引。而数组索引对应的值则是一个linkedlist,并且linkedlist所存储的节点值,同时包含着所需存储的key和value。这样以便解决当hashcode重复冲突时,在链表中根据key名称来get查找值。 关于hashmap更多的原理,我推荐这篇文章 http://www.admin10000.com/document/3322.html

下面直接给出实现,其中使用到LinkedList数据结构的源码,在我的这篇分享当中:http://www.cnblogs.com/tdws/p/6033209.html

   var djb2Code = function (str) {
var hash = 5381;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
}
return hash;
} function HashMap() {
var map = [];
var keyValPair = function (key, value) {
this.key = key;
this.value = value;
}
this.put = function (key, value) {
var position = djb2Code(key);
if (map[position] == undefined) {
map[position] = new LinkedList();
}
map[position].append(new keyValPair(key, value));
},
this.get = function (key) {
var position = djb2Code(key);
if (map[position] != undefined) {
var current = map[position].getHead();
while (current.next) {
if (current.element.key === key) { //严格判断
return current.element.value;
}
current = current.next;
}
if (current.element.key === key) {//如果只有head节点,则不会进while. 还有尾节点,不会进while,这个判断必不可少
return current.element.value;
}
}
return undefined;
},
this.remove = function (key) {
var position = djb2Code(key);
if (map[position] != undefined) {
var current = map[position].getHead();
while (current.next) {
if (current.element.key === key) {
map[position].remove(current.element);
if (map[position].isEmpty()) {
map[position] == undefined;
}
return true;
}
current = current.next;
}
if (current.element.key === key) {
map[position].remove(current.element);
if (map[position].isEmpty()) {
map[position] == undefined;
}
return true;
}
}
}
} //链表
function LinkedList() {
var Node = function (element) {        //新元素构造
this.element = element;
this.next = null;
};
var length = 0;
var head = null; this.append = function (element) {
var node = new Node(element);        //构造新的元素节点
var current;
if (head === null) {             //头节点为空时 当前结点作为头节点
head = node;
} else {
current = head;
while (current.next) {          //遍历,直到节点的next为null时停止循环,当前节点为尾节点
current = current.next;
}
current.next = node;            //将尾节点指向新的元素,新元素作为尾节点
}
length++;                    //更新链表长度
};
this.removeAt = function (position) {
if (position > -1 && position < length) {
var current = head;
var index = 0;
var previous;
if (position == 0) {
head = current.next;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
length--;
return current.element;
} else {
return null;
}
};
this.insert = function (position, element) {
if (position > -1 && position <= length) {        //校验边界
var node = new Node(element);
current = head;
var index = 0;
var previous;
if (position == 0) {                    //作为头节点,将新节点的next指向原有的头节点。
node.next = current;
head = node;                        //新节点赋值给头节点
} else {
while (index++ < position) {
previous = current;
current = current.next;
}                                //遍历结束得到当前position所在的current节点,和上一个节点
previous.next = node;                    //上一个节点的next指向新节点 新节点指向当前结点,可以参照上图来看
node.next = current;
}
length++;
return true;
} else {
return false;
} };
this.toString = function () {
var current = head;
var string = '';
while (current) {
string += ',' + current.element;
current = current.next;
}
return string;
};
this.indexOf = function (element) {
var current = head;
var index = -1;
while (current) {
if (element === current.element) {            //从头节点开始遍历
return index;
}
index++;
current = current.next;
}
return -1;
};
this.getLength = function () {
return length;
};
this.getHead = function () {
return head;
};
this.isEmpty = function () {
return length == 0;
}
}

学习Redis你必须了解的数据结构——HashMap实现

参考文章:js获取hashcode  : http://www.cnblogs.com/pigtail/p/3342977.html

如果我的点滴分享对你有点滴帮助,欢迎点击下方红色按钮,我将长期输出分享。

上一篇:malware analysis、Sandbox Principles、Design && Implementation


下一篇:无需写try/catch,也能正常处理异常 (转)