zoukankan      html  css  js  c++  java
  • 使用javaScript实现散列表的线性探查法

    class ValuePair{
        constructor(key,value){
            this.key = key;
            this.value = value;
        }
    }
    function defaultToString(item){
        if(item == null){
            return 'null';
        }
        if(item == undefined){
            return 'undefined';
        }
        if(typeof item =='string' || item instanceof String){
            return `${item}`;
        }
        return item.toString();
    }
    
    class LinearExploration{
        constructor(toStrFn = defaultToString){
            this.toStrFn = toStrFn;
            this.table = {};
        }
        hashCode(key){
            return this.loseloseHashCode(key);
        }
        loseloseHashCode(key){
            if(typeof key == 'number'){
                return key;
            }
            let str = this.toStrFn(key);
            let addr = 0;
            for(let i = 0; i < str.length; i++){
                addr += str.charCodeAt(i);
            }
            return addr;
        }
        put(key,value){
            if(key != null && value != null){
                let position = this.hashCode(key);
                if(this.table[position]==null){
                    this.table[position] = new ValuePair(key,value);
                    return true;
                }
                //从下一个位置开始
                let index = position+1;
                //寻找空余位置
                while(this.table[index]!=null){
                    index++;
                }
                this.table[index] = new ValuePair(key,value);
                return true; 
            }
            return 'undefine';
        }
        get(key){
            let position =this.hashCode(key);
            if(this.table[position].key === key){
                return this.table[position].value;
            }
            let index = position+1;
            while(this.table[index]!=null && this.table[index].key != key){
                index++;
            }
            if(this.table[index] != null && this.table[index].key === key){
                return this.table[index].value;
            }
        }
        remove(key){
            const position = this.hashCode(key);
            if(this.table[position]!=null){
                if(this.table[position].key === key){
                    delete this.table[position];
                    this.verifyRemoveSideEffect(key,position);
                    return true;
                }
                let index = position+1;
                while(this.table[index] != null && this.table[index].key != key){
                    index++;
                }
                if(this.table[index] != null && this.table[index].key === key){
                    delete this.table[index];
                    this.verifyRemoveSideEffect(key,index);
                    return true;
                }
    
            }
            return false;
        }
        verifyRemoveSideEffect(key,removedPosition){
            const hash = this.hash(key);
            let index = removedPosition+1;
            while(this.table[index] != null){
                let posHash = this.hashCode(index);
                if(posHash <= hash || posHash < removedPosition){
                    this.table[removedPosition] = this.table[index];
                    delete this.table[index];
                   removedPosition = index;
                }
                index++;
            }
        }
    }
  • 相关阅读:
    SharePoint:扩展DVWP 第11部分:在工作流中使用更多的表单字段
    Guava学习笔记:Google Guava 类库简介
    每天一个linux命令(59):rcp命令
    每天一个linux命令(60):scp命令
    【转载】程序员要勇于说不
    深入理解Java:内省(Introspector)
    Guava学习笔记:Optional优雅的使用null
    深入理解Java:注解(Annotation)基本概念
    深入理解Java:SimpleDateFormat安全的时间格式化
    每天一个linux命令(58):telnet命令
  • 原文地址:https://www.cnblogs.com/MySweetheart/p/13260543.html
Copyright © 2011-2022 走看看