首页 > 其他分享 >Map底层实现

Map底层实现

时间:2022-09-19 16:23:02浏览次数:71  
标签:Map set map 实现 quene next key store 底层

Map底层实现

function MyHashMap() {
  this.init();
}
MyHashMap.prototype.init = function () {
  var store = new Array(8);
  for (var i = 0; i < store.length; i++) {
    store[i] = new Object();
    store[i].next = null;
  }
  this.store = store;
}
MyHashMap.prototype.hash = function (i) {
  return i % this.store.length;
}
MyHashMap.prototype.set = function (key, val) {
  var index = this.hash(key);
  var quene = this.store[index];
  while (quene.next) {
    if (quene.next.key === key) {
      quene.next.val = val;
      return;
    } else {
      quene = quene.next;
    }
  }
  quene.next = {
    key: key,
    val: val,
    next: null
  }
}
MyHashMap.prototype.get = function (key) {
  var index = this.hash(key);
  var quene = this.store[index];
  while (quene) {
    if (quene.key === key) {
      return quene.val;
    } else {
      quene = quene.next;
    }
  }
  return undefined;
}
MyHashMap.prototype.has = function (key) {
  return !!this.get(key)
}

var map = new MyHashMap();
map.set(1,'a');
map.set(2,'b');
map.set(3,'c');
map.set(4,'d');
map.set(5,'e');
map.set(6,'f');
map.set(7,'g');
map.set(8,'h');
map.set(9,'e');

console.log(map);
console.log(map.get(1))function MyHashMap() {
  this.init();
}
MyHashMap.prototype.init = function () {
  var store = new Array(8);
  for (var i = 0; i < store.length; i++) {
    store[i] = new Object();
    store[i].next = null;
  }
  this.store = store;
}
MyHashMap.prototype.hash = function (i) {
  return i % this.store.length;
}
MyHashMap.prototype.set = function (key, val) {
  var index = this.hash(key);
  var quene = this.store[index];
  while (quene.next) {
    if (quene.next.key === key) {
      quene.next.val = val;
      return;
    } else {
      quene = quene.next;
    }
  }
  quene.next = {
    key: key,
    val: val,
    next: null
  }
}
MyHashMap.prototype.get = function (key) {
  var index = this.hash(key);
  var quene = this.store[index];
  while (quene) {
    if (quene.key === key) {
      return quene.val;
    } else {
      quene = quene.next;
    }
  }
  return undefined;
}
MyHashMap.prototype.has = function (key) {
  return !!this.get(key)
}

var map = new MyHashMap();
map.set(1,'a');
map.set(2,'b');
map.set(3,'c');
map.set(4,'d');
map.set(5,'e');
map.set(6,'f');
map.set(7,'g');
map.set(8,'h');
map.set(9,'e');

console.log(map);
console.log(map.get(1))
console.log(map.get(8))

标签:Map,set,map,实现,quene,next,key,store,底层
From: https://www.cnblogs.com/brujie/p/16708057.html

相关文章

  • redis底层数据结构
    跳跃列表是一种数据结构。它允许快速查询一个有序连续元素的数据链表。跳跃列表的平均查找和插入时间复杂度都是O(logn),优于普通队列的O(n)。为什么要使用跳表?数组......
  • vue-baidu-map 搜索定位
    1、引入vue-baidu-map插件npminstallvue-bai-map--save2、注册(我这里是采用全局注册main.js)ak是你自己申请的3、html4、数据定义5、JS......
  • Map底层实现、Map-Object性能对比
    Map底层实现、Map-Object性能对比Map是一个键值对的集合,和Object类似。Map做为构造函数,可以通过全局对象获取到。需要通过new操作创建实例对象,直接调用会报错。M......
  • And RMQ 势能线段树-用剪枝+单点修改实现区间修改
    https://codeforces.com/gym/103107/problem/AA.AndRMQtimelimitpertest3secondsmemorylimitpertest512megabytesinputstandardinputoutputstan......
  • Delphi实现HTMLWebBrowser实现HTML界面
    HTML的界面有以下特点:图文混排,格式灵活,可以包含Flash、声音和视频等,实现图文声像的多媒体界面,而且易于建立和维护。另外,HTML的显示环境一般机器上都具备,通常不需要安装额外......
  • 直播带货源码,附近的人功能是如何实现的
    直播带货源码,附近的人功能是如何实现的 usingUnityEngine;usingSystem.Collections;usingUnityEngine.Video;usingSystem.Collections.Generic;publicclass  ......
  • 如何开发直播软件,卡片式界面实现
    如何开发直播软件,卡片式界面实现1、添加recyclerview的布局fragment_all_dishes.xml <?xmlversion="1.0"encoding="utf-8"?><FrameLayoutxmlns:android="http://sch......
  • 通过外网服务器搭建frp服务实现本地内网穿透
    背景:疫情期间在家远程办公,一般公司办公室开发测试环境是内网服务,为了远程使用公司的内网服务,那就需要做内网穿透,本文已阿里云服务搭建,其它云服务也是一样的。1、下载部署......
  • 使用canvas实现简易代码雨
    分享一个使用canvas生成的一个简易代码雨特效首先HTML文件如下...<body><canvasid="bg"></canvas></body>...Javascript代码如下constcvs=document.getElemen......
  • Spring源码分析-Bean实现
    实现SpringBean功能定义扫描路径创建ApplicationContext类packagecom.smile.spring;publicclassApplicationContext{}采用配置类加注解实现配置功能创建......