LZW算法 golang实现

来源:互联网 发布:新日铁住金软件上海 编辑:程序博客网 时间:2024/05/20 01:39
// LZW Data Compression in Golangpackage mainimport "fmt"func compressLZW(testStr string) []int {        code := 256    dictionary := make(map[string]int)    for i := 0; i < 256; i++ {        dictionary[string(i)] = i    }    currChar := ""    result := make([]int, 0)        for _, c := range []byte(testStr) {         phrase := currChar + string(c)        if _, isTrue := dictionary[phrase]; isTrue {                    currChar = phrase        } else {            result = append(result, dictionary[currChar])            dictionary[phrase] = code            code++            currChar = string(c)        }    }    if currChar != "" {        result = append(result, dictionary[currChar])    }    return result}func decompressLZW(compressed []int) string {        code := 256    dictionary := make(map[int]string)    for i := 0; i < 256; i++ {        dictionary[i] = string(i)    }    currChar := string(compressed[0])    result := currChar    for _, element := range compressed[1:] {        var word string        if x, ok := dictionary[element]; ok {            word = x        } else if element == code {            word = currChar + currChar[:1]        } else {            panic(fmt.Sprintf("Bad compressed element: %d", element))        }        result += word        dictionary[code] = currChar + word[:1]        code++        currChar = word    }    return result}func main() {    fmt.Print("Enter any string :")    var testStr string    fmt.Scanln(&testStr)    compressed := compressLZW(testStr)    fmt.Println("\nAfter Compression :", compressed)    uncompression := decompressLZW(compressed)    fmt.Println("\nAfter Uncompression :", uncompression)}

参考
http://www.golangprograms.com/golang-program-for-implementation-lzw-data-compression-and-uncompression.html

原创粉丝点击