当前位置: 首页 > news >正文

网站台做计么呢邢台天九建设有限公司网站

网站台做计么呢,邢台天九建设有限公司网站,宁波在线网,企业服务账号关于LRU缓存 LRU - Lease Recently Used 最近使用 如果内存优先#xff0c;只缓存最近使用的#xff0c;删除 ‘沉睡’ 数据 核心 api: get set 分析 使用哈希表来实现, O(1)必须是有序的#xff0c;常用放在前面#xff0c;沉睡放在后面, 即#xff1a;有序#xff0…关于LRU缓存 LRU - Lease Recently Used 最近使用 如果内存优先只缓存最近使用的删除 ‘沉睡’ 数据 核心 api: get set 分析 使用哈希表来实现, O(1)必须是有序的常用放在前面沉睡放在后面, 即有序可排序这样 {} 不符合要求Map是可以排序的按照设置顺序 class LRUCache {private length: numberprivate data: Mapany, any new Map()constructor(length: number) {if (length 1) throw new Error(invalid length)this.length length}set(key: any, value: any) {const data this.dataif (data.has(key)) {data.delete(key)}data.set(key, value)if (data.size this.length) {// 如果超出了容量则删除 Map 最老的元素const delKey data.keys().next().valuedata.delete(delKey)}}get(key: any): any {const data this.dataif(!data.has(key)) return nullconst value data.get(key)data.delete(key)data.set(key, value) // 保持最新的return value} }const lruCache new LRUCache(2) lruCache.set(1, 1) // { 11 } lruCache.set(2, 2) // { 11, 22 } lruCache.get(1) // 1 {22, 11} lruCache.set(3,3) // {11, 33} lruCache.get(2) // null lruCache.set(4,4) // {33, 44} lruCache.get(1) // null lruCache.get(3) // {44, 33} lruCache.get(4) // 4 {33, 44}不用 Map 如何实现 LRU 缓存 注意有序结合 Object Array将Array改造成双向链表 const obj1 {value: 1, key: a} const obj2 {value: 2, key: b} const obj3 {value: 3, key: c}const data [obj1, obj2, obj3] const map {a: obj1, b: obj2, c: obj3}上述数据结构很精妙 interface IListNode {value: anykey: stringprev?: IListNodenext?: IListNode }export default class LRUCache {private length: numberprivate data: { [key: string]: IListNode } {}private dataLength: number 0private listHead: IListNode | null nullprivate listTail: IListNode | null nullconstructor(length: number) {if (length 1) throw new Error(invalid length)}private moveToTail(curNode: IListNode) {const tail this.listTailif (tail curNode) return// 1. 让 pre next 断绝与 curNode的关系const preNode curNode.prevconst nextNode curNode.nextif (prevNode) {if (nextNode) {preNode.next nextNode} else {delete prevNode.next}}if (nextNode) {if (prevNode) {nextNode.prev prevNode} else {delete nextNode.prev}if (this.listHead curNode) this.listHead nextNode}// 2. 让 curNode 断绝与 prev next的关系delete curNode.prevdelete curNode.next// 3. 在list 末尾重新建立 curNode 的新关系if (tail) {tail.next curNodecurNode.prev tail}this.listTail curNode}private tryClean() {while(this.dataLength this.length) {const head ths.listHeadif (!head) throw new Error(head is null)const headNext head.nextif (!headNext) throw new Error(headNext is null)// 1. 断绝head和next的关系delete headNext.prevdelete head.next// 2. 重新赋值 listHeadthis.listHead headNext// 3. 清理 data, 重新计数delete this.data[head.key]this.dataLength - 1}}get(key: string):any {const data this.dataconst curNode data[key]if (!curNode) return nullif (this.listTail curNode) {reutrn curNode.vlaue}// curNode 移动到末尾this.moveToTail(curNode)}set(key: string, value: any) {const data this.dataconst curNode data[key]if (!curNode) {// 新增数据const newNode: IListNode {key, value}// 移动到末尾this.moveToTail(newNode)data[key] newNodethis.dataLength if (this.dataLength 1) this.listHead newNode} else {// 修改现有数据curNode.value value// 移动到末尾this.moveToTail(curNode)}// 长度超过了则需要清理this.tryClean()} }功能性测试 const lruCache new LRUCache(2) lruCache.set(1, 1) // { 11 } lruCache.set(2, 2) // { 11, 22 } lruCache.get(1) // 1 {22, 11} lruCache.set(3,3) // {11, 33} lruCache.get(2) // null lruCache.set(4,4) // {33, 44} lruCache.get(1) // null lruCache.get(3) // {44, 33} lruCache.get(4) // 4 {33, 44}数据结构设计data 和 list 分别存储什么双向链表的操作非常繁琐代码很容易写错 不易调试链表 node 要存储 node.key, 否则需要遍历 data 删除
http://wiki.neutronadmin.com/news/334612/

相关文章:

  • 网站的建设分析及意义房地产最新利好消息
  • 一个服务器可以建多少个网站自己做网站好还是购买网站好
  • 打开网站弹出图片代码整站优化方案
  • 荥阳市网站建设东莞市阳光网首页
  • 做任务赚佣金网站有哪些公司基本资料模板
  • 购物网站 开店北京外包公司排行榜
  • 简约网站设计网络游戏加盟合作
  • 找关键词的网站网络设计开发网站
  • 手机网站首页经典案例可信网站认证代理
  • 嘉兴市建设教育网站锚文本对网站
  • 云南省建设厅网站舉報波兰网站后缀
  • 网站建设 价格低企业品牌建设方案范文
  • 企业网站的优化和推广方法做电路方案设计的网站
  • 建网站一般多少钱幸福里seo文章优化技巧
  • 怎么选择优秀的网站建设公司阿里云网站建设步骤
  • 常州个人做网站广告学徒一般要学多久
  • 广西壮族自治区住房和城乡建设厅网站怎么做酒店网站
  • 三一重工的网站是哪家做的营销型建设
  • wordpress广告点进去报错长春百度搜索排名优化
  • 租用服务器一般是谁帮助维护网站安全4p营销策略分析
  • 网站内容与栏目设计沈阳男科医院哪家好点
  • 西安门户网站建设公司哪家好网站建设中应该返回502还是301
  • php网站开发书籍东莞手机建网站
  • 网站建设制作及推广视频类网站如何做缓存
  • 北京网站建设企业网站交易平台建设
  • 数据管理系统网站模板佛山网站优化什么价格
  • 网站建设公司是什么重庆正云环保建设网站
  • seo网站优化培训怎么做运行怎么卸载wordpress
  • wordpress网站百度收录首页易迈互联网站建设怎么样
  • 邻水网站建设网站模板flash