Javascript中的字典和散列
function Dictionary() {
var items={};
this.set=function (key,value) {
items[key]=value;
};
this.remove=function (key) {
if(this.has(key)){
delete items[key];
return true;
}
return false;
};
this.has=function (key) {
return key in items;
};
this.get=function(key){
return this.has(key)?items[key]:undefined;
};
this.clear=function(){
};
this.size=function(){
};
this.keys=function () {
};
this.values=function () {
var values=[];
for (var k in items) {
if (this.has(k)) {
values.push(items[k]);
}
}
return values;
};
this.getItems=function(){
return items;
};
}
// 解决散列值重复的三种方法:分离链接、线性探查、双散列法。
// 1、分离链接法为散列表的每一个位置创建一个链表并将元素存储在里面
//分离链接的哈希表
//被注释的为普通的哈希表
function HashTable() {
var table=[];
var loseHashCode=function(key){
var hash=0;
for(var i=0;i<key.length;i++){
hash+=key.charCodeAt(i);
}
return hash%37;
};
var ValuePair=function (key,value) {
this.key=key;
this.value=value;
this.toString=function () {
return "["+this.key+"-"+this.value+"]";
}
}
this.put=function(key,value){
var position=loseHashCode(key);
// console.log(position,"-",key);
// table[position]=value;
if(table[position]==undefined){
//每一个位置都是一个链表
table[position]=new LinkedList();
}
table[position].append(new ValuePair(key,value));
};
this.remove=function(key){
//table[loseHashCode(key)]=undefined;
var position=loseHashCode(key);
if(table[position]!==undefined){
var current=table[position].getHead();
while(current.next){
if(current.element.key===key){
table[position].remove(current.element);
if(table[position].isEmpty()){
table[position]=undefined;
}
return true;
}
current=current.next;
}
if(current.element.key===key){
table[position].remove(element);
if(table[position].isEmpty()){
table[position]=undefined;
}
return true;
}
}
return false;
};
this.get=function(key){
//return table[loseHashCode(key)];
var position=loseHashCode(key);
if (table[position]!==undefined) {
var current=table[position].getHead();
while (current.next) {
if (current.element.key===key) {
return current.element.value;
}
current=current.next;
}
if (current.element.key===key) {
return current.element.value;
}
}
return undefined;
};
this.print=function(){
for (var i = 0; i < table.length; i++) {
if(table[i]!=undefined){
console.log(i,":",table[i]);
}
}
};
}
//2、线性探查
// 当想向表中某个位置加入一个新的元素时,如果索引为index的位置已经被占据了,
// 就尝试index+1的位置。如果index+1的位置也被占据了,就尝试index+2的位置
// 以此类推
function LDHashTable() {
// ... 省略重复的代码
this.put=function (key,value) {
if(table[position]==undefined){
table[position]=new KeyValuePair(key,value);
}else{
var index=++position;
while (table[index]!=undefined) {
index++
}
table[position]=new KeyValuePair(key,value);
}
}
this.get=function (key) {
var position=loseHashCode(key);
if(table[position]!==undefined){
if(table[position].key===key){
return table[position].value;
}else{
var index=++index;
// 原书中是 table[index]===undefined || table[index].key!==key
while (table[index]!==undefined && table[index].key!==key) {
index++;
}
if(table[index] && table[index].key===key){
return table[index].value;
}
}
}
}
}
// 更好的散列函数
var djb2HashCode=function (key) {
var hash=5381;//一个质数,大多数情况下为5381
for (var i = 0; i < key.length; i++) {
hash=hash*33+key.charCodeAt(i);
}
return hash%1013;//1013为比散列表大小要大的一个质数
}
Javascript中的字典和散列的更多相关文章
- JavaScript中创建字典对象(dictionary)实例
这篇文章主要介绍了JavaScript中创建字典对象(dictionary)实例,本文直接给出了实现的源码,并给出了使用示例,需要的朋友可以参考下 对于JavaScript来说,其自身的Array对象 ...
- Python中的hashable(散列)
Python文档中的解释: 一个对象是可散列的,那么在它的生命周期中它的hash 值是不变的. 可散列的对象需要2个方法:__hash__()方法和__eq__()方法.两个可散列的对象相等,那么它们 ...
- javascript中的字典
1.概念 字典是一种以键值对的形式存储的数据结构,就系那个电话本中的名字和电话号码一样.要找到一个电话首先要找到名字,再根据名字找到电话号码.这里的键就是指用来查找的东西,值就是查找得到的结果. Ja ...
- JavaScript中常见数据结构
数据结构 栈:一种遵从先进后出 (LIFO) 原则的有序集合:新添加的或待删除的元素都保存在栈的末尾,称作栈顶,另一端为栈底.在栈里,新元素都靠近栈顶,旧元素都接近栈底. 队列:与上相反,一种遵循先进 ...
- JavaScript数据结构——集合、字典和散列表
集合.字典和散列表都可以存储不重复的值. 在集合中,我们感兴趣的是每个值本身,并把它当作主要元素.在字典和散列表中,我们用 [键,值] 的形式来存储数据. 集合(Set 类):[值,值]对,是一组由无 ...
- JavaScript数据结构——字典和散列表的实现
在前一篇文章中,我们介绍了如何在JavaScript中实现集合.字典和集合的主要区别就在于,集合中数据是以[值,值]的形式保存的,我们只关心值本身:而在字典和散列表中数据是以[键,值]的形式保存的,键 ...
- DotNet加密方式解析--散列加密
没时间扯淡类,赶紧上车吧. 在现代社会中,信息安全对于每一个人都是至关重要的,例如我们的银行账户安全.支付宝和微信账户安全.以及邮箱等等,说到信息安全,那就必须得提到加密技术,至于加密的一些相关概念, ...
- 【Java集合学习】HashMap源码之“拉链法”散列冲突的解决
1.HashMap的概念 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射. HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io ...
- java 散列
原文:https://www.cnblogs.com/younghao/p/8333795.html 为什么要设计散列这种数据结构呢?在现实世界中,实体之间可能存在着映射关系(key-value),比 ...
随机推荐
- 基于C#和Asp.NET MVC开发GPS部标监控平台
基于交通部796标准开发部标监控平台,选择开发语言和技术也是团队要思考的因素,其实这由团队自己擅长的技术来决定,如果擅长C#和Asp.NET, 当然开发效率就高很多.当然了技术选型一定要选用当前主流的 ...
- 南非的5DT数据手套使用说明
数据手套-5 Ultra/数据手套-14 Ultra 5DT 数据手套-Ultra数据手套的设计目的是为了满足现代动作捕捉和动画制作等专业人士的严格要求.它提供了舒适,易于使用,小型要素和多种应用的驱 ...
- Leetcode: Android Unlock Patterns
Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...
- ZOJ 2412 Farm Irrigation
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- 【VirtualBox】 Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host
win10 VirtualBox_5.0.24.8355_Win 安装后导入.ova 文件后 虚拟机不能正常启动 ===> 解决: “打开网络和共享中心” “更多适配器设置” 选择 对应的网络适 ...
- mysql免安装方法
在官网上下载了个mysql-installer-community-5.6.27.0.msi,需要安装环境,检测未通过,很麻烦.之后网上查了资料之后直接下载了个压缩包mysql-5.6.27-winx ...
- workbench中safe update
1.在workbench中表格显示为readonly ,更新时提示Error Code: 1175. You are using safe update mode and you tried to u ...
- h5动画效果总结
一些常用的h5效果,自己总结的,用的时候直接拿,方便快捷! 1.悬浮时放大: .one{transition:All 0.4s ease-in-out;-webkit-transition:All 0 ...
- Objective-C文章中的生词
Objective-C http://rypress.com/tutorials/objective-c/index C Basics http://rypress.com/tutorials/ ...
- IIS Express 配置外部访问
IIS Express是Visual Stuido自带的微型Web服务器,简单易用. IIS Express默认只允许本机访问,通过Visual Studio调试Web程序时,我们有时需要通过外部访问 ...