富贵资源网 Design By www.hznty.com

字典(Dictionary)的javascript实现
编程思路:

  • 使用了裸对象datastore来进行元素存储;
  • 实现了两种得到字典长度的方法,一种为变量跟踪,一种为实时计算。

代码:

function(){
  "use strict";

  function Dictionary(){
    this._size = 0;
    this.datastore = Object.create(null);
  }

  Dictionary.prototype.isEmpty = function(){
    return this._size === 0;
  };

  Dictionary.prototype.size = function(){
    return this._size;
  };

  Dictionary.prototype.clear = function(){
    for(var key in this.datastore){
      delete this.datastore[key];
    }
    this._size = 0;
  };

  Dictionary.prototype.add = function(key, value){
    this.datastore[key] = value;
    this._size++;
  };

  Dictionary.prototype.find = function(key){
    return this.datastore[key];
  };

  Dictionary.prototype.count = function(){
    var n = 0;
    for(var key in this.datastore){
      n++;
    }
    return n;
  };

  Dictionary.prototype.remove = function(key){
    delete this.datastore[key];
    this._size--;
  };

  Dictionary.prototype.showAll = function(){
    for(var key in this.datastore){
      console.log(key + "->" + this.datastore[key]);
    }
  };

  module.exports = Dictionary;
})();

散列(hashtable)的javascript实现
编程思路:

  • 以链表来解决实现开链法来解决碰撞,并使用自己写的单链表库LinkedList(详见jb51之前的https://www.jb51.net/article/86394.htm);
  • 用裸对象来存储;
  • ValuePair简单封装键值对;
  • 以模块模式组织代码;

代码:

valuePair.js

(function(){
  "use strict";

  function ValuePair(key, value){
    this.key = key;
    this.value = value;
  }

  ValuePair.prototype.toString = function(){
    return "[" + this.key + ":" + this.value + "]";
  };

  module.exports = ValuePair;

})();

hashtable.js

(function(){

  "use strict";

  var ValuePair = require("./lib/ValuePair");
  var LinkedList = require("./LinkedList");

  function Hashtable(){
    this.table = Object.create(null);
    this._size = 0;
  }

  Hashtable.prototype.isEmpty = function(){
    return this._size === 0;
  };

  Hashtable.prototype.size = function(){
    return this._size;
  };

  Hashtable.prototype.remove = function(key){
    var index = hashCode(key);

    if(this.table[index] == null){
      return false;
    }else{
      var currNode = this.table[index].getHead();
      while(currNode.next){
        currNode = currNode.next;
        if(currNode.element.key == key){
          this.table[index].remove(currNode.element);
          this._size--;
          return true;
        }
      }
      return false;
    }
  };

  Hashtable.prototype.get = function(key){
    var index = hashCode(key);

    if(this.table[index] == null){
      return null;
    }else{
      var currNode = this.table[index].getHead();
      while(currNode.next){
        currNode = currNode.next;
        if(currNode.element.key == key){
          return currNode.element;
        }
      }
      return null;
    }
  };

  Hashtable.prototype.put = function(key, value){
    var index = hashCode(key);

    if(this.table[index] == null){
      this.table[index] = new LinkedList();
    }

    var currNode = this.table[index].getHead();
    while(currNode.next){            //key若已经存在,修改value值为新值
      currNode = currNode.next;
      if(currNode.element.key == key){
        currNode.element.value = value;
        break;
      }
    }

    if(currNode.next == null && currNode.element.value != value){         //key不存在,加入新值.注意边界值
      this.table[index].add(new ValuePair(key,value));
      this._size++;
    }

    return this;
  };

  Hashtable.prototype.display = function(){
    for(var key in this.table){
      var currNode = this.table[key].getHead();
      while(currNode.next){
        currNode = currNode.next;
        console.log(currNode.element.toString());
      }
    }
  };


  /*********************** Utility Functions ********************************/

  function hashCode(key) {        //霍纳算法,质数取37
    var hashValue = 6011;
    for (var i = 0; i < key.length; i++) {
      hashValue = hashValue * 37 + key.charCodeAt(i);
    }
    return hashValue % 1019;
  }

  module.exports = Hashtable;

})();

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。