zoukankan      html  css  js  c++  java
  • js下的map数据对象构建


    mapUtil = function(){
    this.map = new Array();
    };

    mapUtil.prototype = {
    //Update or Insert
    add : function(key, value){
    for (var i = 0; i < this.map.length; i++)
    {
    if ( this.map[i].key+"" === key+"" )
    {
    this.map[i].value = value;
    return;
    }
    }

    this.map[this.map.length] = new struct(key, value);
    },
    put : function(key, value){
    for (var i = 0; i < this.map.length; i++)
    {
    if ( this.map[i].key+"" === key+"" )
    {
    this.map[i].value = value;
    return;
    }
    }

    this.map[this.map.length] = new struct(key, value);
    },
    //Query
    get : function(key){
    for (var i = 0; i < this.map.length; i++)
    {
    if ( this.map[i].key+"" == key +"")
    {
    return this.map[i].value;
    }
    }

    return undefined;
    },
    getKeyByIndex : function(index){
    if(index >= 0 && index < this.map.length){
    return this.map[index].key;
    }

    return null;
    },
    getByIndex : function(index){
    if(index >= 0 && index < this.map.length){
    return this.map[index].value;
    }

    return null;
    },
    getIndexByKey:function(key){
    for (var i = 0; i < this.map.length; i++)
    {
    if ( this.map[i].key+"" === key +"")
    {
    return i;
    }
    }
    },
    //Delete
    remove : function(key){
    var v;
    var len = this.map.length;
    for (var i = 0; i < len; ++i)
    {
    v = this.map.pop();
    if ( v.key === key ){
    continue;
    }

    this.map.unshift(v);
    }
    },
    removeByIndex : function(index){
    var v;
    var len = this.map.length;
    for (var i = 0; i < len; i++)
    {
    v = this.map.shift();
    if ( i === index ){
    continue;
    }

    this.map.push(v);
    }
    },

    //clear
    clear : function(){
    this.map.splice(0, this.map.length);
    },
    removeAll : function(){
    this.map.splice(0, this.map.length);
    },

    size: function(){
    return this.map.length;
    },

    isEmpty : function(){
    return (this.map.length <= 0);
    },
    toString:function(){
    var v="";
    for (var i = 0; i < this.map.length; i++){
    v+=this.map[i].key+":"+this.map[i].value+";"
    }
    return v;
    }
    };

    //模拟<key, value>数据结构
    function struct(key, value){
    this.key = key;
    this.value = value;
    };

  • 相关阅读:
    线索二叉树的构建和遍历------小甲鱼数据结构和算法
    小甲鱼数据结构和算法-----二叉树的构建和前序遍历
    python爬虫爬取煎蛋网妹子图片
    C语言实现汉诺塔问题
    C语言实现中缀表达式转后缀表达式
    深度优先算法--判断迷宫的一个起点能否到达一个终点
    python 爬取36K新闻
    栈的操作实现逆波兰表达式的计算
    python 实现汉诺塔问题
    【POJ 3258】River Hopscotch
  • 原文地址:https://www.cnblogs.com/emmalai/p/5541808.html
Copyright © 2011-2022 走看看