MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / decompress

Method decompress

src/main/java/com/thealgorithms/compression/LZ78.java:113–135  ·  view source on GitHub ↗

Decompresses a list of LZ78 tokens back into the original string. @param compressedData The list of Token objects. Must not be null. @return The original, uncompressed string.

(List<Token> compressedData)

Source from the content-addressed store, hash-verified

111 * @return The original, uncompressed string.
112 */
113 public static String decompress(List<Token> compressedData) {
114 if (compressedData == null || compressedData.isEmpty()) {
115 return "";
116 }
117
118 StringBuilder decompressedText = new StringBuilder();
119 Map<Integer, String> dictionary = new HashMap<>();
120 int nextDictionaryIndex = 1;
121
122 for (Token token : compressedData) {
123 String prefix = (token.index == 0) ? "" : dictionary.get(token.index);
124
125 if (token.nextChar == END_OF_STREAM) {
126 decompressedText.append(prefix);
127 } else {
128 String currentPhrase = prefix + token.nextChar;
129 decompressedText.append(currentPhrase);
130 dictionary.put(nextDictionaryIndex++, currentPhrase);
131 }
132 }
133
134 return decompressedText.toString();
135 }
136}

Callers 15

testStandardExampleMethod · 0.95
testLongerExampleMethod · 0.95
testEmptyStringMethod · 0.95
testAllSameCharactersMethod · 0.95
testSingleCharacterMethod · 0.95
testTwoCharactersMethod · 0.95
testRepeatingPairsMethod · 0.95
testGrowingPatternsMethod · 0.95

Calls 5

isEmptyMethod · 0.65
getMethod · 0.45
appendMethod · 0.45
putMethod · 0.45
toStringMethod · 0.45

Tested by 15

testStandardExampleMethod · 0.76
testLongerExampleMethod · 0.76
testEmptyStringMethod · 0.76
testAllSameCharactersMethod · 0.76
testSingleCharacterMethod · 0.76
testTwoCharactersMethod · 0.76
testRepeatingPairsMethod · 0.76
testGrowingPatternsMethod · 0.76