﻿/*
hashTable的javascript实现
*/
function hashTable() {

    this.__hash = {};


    //添加key-value键值对
    this.add = function(key, value) {
        if (typeof (key) != "undefined") {
            //if it not contains in hashtable 
            if (!this.contains(key)) {
                this.__hash[key] = typeof (value) == "undefined" ? null : value;
                return true;
            }
            else {
                return false;
            }
        }
    };

    //删除key-value键值对
    this.remove = function(key) {
        delete this.__hash[key];
    };

    this.count = function() {
        var i = 0;
        for (var obj in this.__hash) {
            i++;
        }
        return i;
    };

    //取得指定键值
    this.items = function(key) {
        return this.__hash[key];
    };


    //检查是否包含指定键
    this.contains = function(key) {
        return typeof (this.__hash[key]) != "undefined";
    };

    //清空哈希表
    this.clear = function() {
        for (var obj in this.__hash) {
            delete this.__hash[k];
        }
    };
}



/*测试replace函数
var s = "a1{0}2{0}3";
s = replace(s,"\\{0\\}","***");
alert(s);
*/
