bitMap算法之ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串

/*
*  ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串,
*  用于数据压缩,加密解密,以及bitMap大数据查询,去重
*  作者:caoke
* */

class ckHash{
    //输入密钥
    constructor(secretKey){
        this.secretKey=secretKey;

        this.regexp=new RegExp(secretKey.replace(/\[([^\[]+?)\]/g,'($&)').replace(/[\\\^\:\.\?\+]/g,'\\$&'));

        this.lenArr=[];
        this.dataArr=[];
        secretKey.replace(/\[([^\[]+?)\]/g,(m,p1)=>{
            const arr=[];
            for(let i=0;i<p1.length;i++){
                if(p1[i]==='-'){
                    arr.push('-')
                }else{
                    arr.push(p1[i].charCodeAt(0));
                }
            }
            let length=0;
            for(let i=0;i<arr.length;i++){
                if(arr[i]==='-'){
                    length=length+arr[i+1]-arr[i-1];
                    i++;
                }else{
                    length=length+1;
                }
            }
            this.lenArr.push(length)
            this.dataArr.push(arr)
        })
        this.length=this.lenArr.reduce((x,y)=>x*y)
    }
    //将数字映射成字符串
    toString(number){
        const arr=[];
        for(let i=this.lenArr.length-1;i>0;i--){
            const n1=number%this.lenArr[i];
            arr.unshift(n1)
            number=Math.floor(number/this.lenArr[i]);
        }
        arr.unshift(number)


        const codeArr=[]
        for(let i=0;i<arr.length;i++){
            const dataArr=this.dataArr[i];
            let len= arr[i];

            let code;
            for(let j=0;j<dataArr.length;j++){
                if(dataArr[j]==='-'){
                    if(len<dataArr[j+1]-dataArr[j-1]){
                        code=dataArr[j-1]+len+1;
                        break;
                    }else{
                        len=len-(dataArr[j+1]-dataArr[j-1]);
                        j++;
                    }
                }else if(len===0){
                    code=dataArr[j]
                    break;
                }else{
                    len--;
                }
            }
            codeArr.push(String.fromCharCode(code))
        }
        let index=0;
        return this.secretKey.replace(/\[([^\[]+?)\]/g,(m,p1)=>{
            return codeArr[index++];
        })
        // console.log(codeArr)
        // return this.secretKey.replace(/\[(.)-(.)\]/,String.fromCharCode(this.start+number))
    }
    //将字符串映射成数字
    toNumber(string){
        if(this.regexp.test(string)){
            const arr=[]
            string.replace(this.regexp,function (m,p1) {
                for(let i=1;i<arguments.length-2;i++){
                    arr.push(arguments[i].charCodeAt(0))
                }
            });

            const lenArr=[]
            for(let i=0;i<arr.length;i++){
                const dataArr=this.dataArr[i];
                let len= 0;
                for(let j=0;j<dataArr.length;j++){
                    if(dataArr[j]===arr[i]){
                        break;
                    }else if(dataArr[j]==='-'){
                        if(arr[i]<=dataArr[j+1]){
                            len=len+arr[i]-dataArr[j-1]-1;
                            break;
                        }else{
                            len=len+dataArr[j+1]-dataArr[j-1];
                            j++;
                        }
                    }else{
                        len++;
                    }
                }
                lenArr.push(len)
            }
            let number=0;
            let jz=1;
            for(let i=lenArr.length-1;i>=0;i--){
                number=number+jz*lenArr[i];
                jz=jz*this.lenArr[i]
            }

            return number;

        }else{
            throw string +' 不在匹配范围内';
        }
    }
}
var d=new ckHash("http://www.baidu.com/[1-8dc].[1-8dcv][1-8dcv]ml")
// console.log(d.toString(11))
// console.log(d.toNumber(d.toString(11)))


for(var i=0;i< d.length;i++){
    const str=d.toString(i);
    const num=d.toNumber(str)
    console.log(str)
    console.log(i,num)
}

  

//测试结果 100%通过

/usr/local/bin/node /Users/caoke/17zuoye/go/demotest.js
http://www.baidu.com/1.11ml
0 0
http://www.baidu.com/1.12ml
1 1
http://www.baidu.com/1.13ml
2 2
http://www.baidu.com/1.14ml
3 3
http://www.baidu.com/1.15ml
4 4
http://www.baidu.com/1.16ml
5 5
http://www.baidu.com/1.17ml
6 6
http://www.baidu.com/1.18ml
7 7
http://www.baidu.com/1.1dml
8 8
http://www.baidu.com/1.1cml
9 9
http://www.baidu.com/1.1vml
10 10
http://www.baidu.com/1.21ml
11 11
http://www.baidu.com/1.22ml
12 12
http://www.baidu.com/1.23ml
13 13
http://www.baidu.com/1.24ml
14 14
http://www.baidu.com/1.25ml
15 15
http://www.baidu.com/1.26ml
16 16
http://www.baidu.com/1.27ml
17 17
http://www.baidu.com/1.28ml
18 18
http://www.baidu.com/1.2dml
19 19
http://www.baidu.com/1.2cml
20 20
http://www.baidu.com/1.2vml
21 21
http://www.baidu.com/1.31ml
22 22
http://www.baidu.com/1.32ml
23 23
http://www.baidu.com/1.33ml
24 24
http://www.baidu.com/1.34ml
25 25
此处省略1000条
Process finished with exit code 0

  

猜你喜欢

转载自www.cnblogs.com/caoke/p/10793885.html
今日推荐