• Javascript中的Hashtable实现


    有时候需要在脚本中使用Hashtable类似的数据结构用来存储一些数据,简单的实现一些常用的方法。

    代码如下,做一下记录:

    var Hashtable=function(){ this.Clear();}
    Hashtable.prototype = {
        Add: function(key, value) { //实现Add 方法
            if (!key) return;
            if (typeof (value) === 'undefined') return;
            if (!this.ContainsKey(key)) this.Count++;
            this.Items[key] = value;
        },
        Clear: function() {                //实现Clear 方法
            this.Items = null;
            this.Items = new Object();
        },
        ContainsKey: function(key) {        //实现ContainsKey,检测表中是否包含特定的键
            if (!key) return false;
            if (typeof (this.Items[key]) == 'undefined')
                return false;
            return true;
        },
        ContainsValue: function(value) {    //实现ContainsValue,检测表中是否包含特定的值
            if (!value) return false;
            for (var key in this.Items) {
                if (this.Items[key] == value)
                    return true;
            }
            return false;
        },
        Count: 0,        //键或值总数
        GetValue: function(key) {    //实现GetValue方法,通过键值获取value;                
            if (!this.ContainsKey(key)) return null;
            return this.Items[key];
        },
        GetValueAt: function(index) {    //实现GetValueAt方法,通过索引值获取value;
            if (isNaN(index)) return null;
            if (index < 0 || index > this.Count) return null;
            var i = 0;
            for (var key in this.Items) {
                if (i == index) return this.Items[key];
                i++;
            }
            return null;
        },
        Keys: function() {  //实现Keys方法,获取hash表的所有key 返回Array
            var arr = new Array();
            for (var key in this.Items)
                arr.push(key);
            return arr;
        },
        Remove: function(key) {    //实现Remove方法,删除某个特定的键
            if (!key) return;
            delete this.Items[key];
            this.Count--;
            return;
        },
        RemoveAt: function(index) {    //实现Remove方法,删除某个特定的索引值
            if (isNaN(index)) return null;
            if (index < 0 || index > this.Count) return null;
            var i = 0;
            for (var key in this.Items) {
                if (i == index) { this.Remove(key); return; }
                i++;
            }
            return null;
        },
        RemoveRange: function(startindex, endindex) {    //RemoveRange 删除索引值在某个范围的值
            if (isNaN(startindex) || isNaN(endindex)) return null;
            if (startindex < 0) startindex = 0;
            if (endindex > this.Count - 1) endindex = this.Count - 1;
            for (var i = startindex; i < this.Count; i++)
                this.RemoveAt(i);
        },
        Values: function() {    //实现Values方法,获取hash表的所有键的值 返回Array
            var arr = new Array();
            for (var key in this.Items)
                arr.push(this.Items[key]);
            return arr;
        }
    }
  • 相关阅读:
    Jena学习笔记(2)——利用数据库保存本体
    在Jena框架下基于MySQL数据库实现本体的存取操作
    推荐系统数据稀疏性问题
    基于协同过滤的推荐系统
    机器学习相关——协同过滤
    学习进度条十五(第16周)
    梦断代码阅读笔记三
    梦断代码阅读笔记二
    数组最大值
    梦断代码阅读笔记一
  • 原文地址:https://www.cnblogs.com/bluesky4485/p/1832112.html
Copyright © 2020-2023  润新知