有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java LZW压缩是一个修改过的版本

我有一个LZW算法-

private void start(int maxNumBits) throws IOException{
    System.out.println("Beginning");
    /** Compress a string to a list of output symbols. */
    // Build the dictionary.
    for (int i = 0; i < 256; i++)
        dict.put("" + (char)i, i);
    int i;
    String w = "";
    int bitsRead = 0;
    int bitsOutput = 0;
    int trieLength = 0;
    float lastCr = 0f;
    while((i = reader.read()) != EOF){
        bitsRead += 8;
        float currentCr = (float)bitsRead / (float)bitsOutput;
        if(bytesRead % 1024 == 0)
            System.out.println(currentCr);
        String wi = w + (char)i;
        if (dict.containsKey(wi) && ((currentCr >= lastCr) || (trieLength < maxNumBits))){
            w = wi;
            trieLength += 8;
        }
        else {
            fos.write(dict.get(w));
            bitsOutput += 8;
            // Add wi to the dictionary.
            dict.put(wi, mapSize++);
            w = "" + (char)i;
            trieLength = 0;
        }
        lastCr = currentCr;
    }
    // Output the code for w.
    if (!w.equals("")){
        fos.write(dict.get(w));
        bitsOutput += 8;
    }
}

其中maxNumBits应该是trie的最大大小。假设异常是在传递maxNumBits参数的主类中捕获的。假设dictHashMapreaderFileInputStreamfosFileOutputStream

在我的版本中,如果trie变满(即,trieLength > maxNumBits),压缩将继续,直到当前压缩比(currentCr)小于上一个压缩比(lastCr

我在一个~8mb的文件上运行了这个程序,更改trie长度对累积压缩比没有任何影响。这是密码吗

if(dict.containsKey(wi) && ((currentCr >= lastCr)||(trieLength < maxNumBits)))

是否符合所述要求

谢谢你的帮助

山姆

编辑-爱德华,谢谢你对格式的帮助


共 (1) 个答案

  1. # 1 楼答案

    事实证明,在检查下一次迭代之前,没有检查trieLength,这意味着当它变满时,没有生成新的trie