@Test public void testDecoding() { final List<LZ78Codeword> encoded = new LinkedList<>(); encoded.add(new LZ78Codeword(0, 'a')); encoded.add(new LZ78Codeword(0, 'b')); encoded.add(new LZ78Codeword(2, 'c')); encoded.add(new LZ78Codeword(3, 'a')); encoded.add(new LZ78Codeword(2, 'a')); encoded.add(new LZ78Codeword(4, 'a')); encoded.add(new LZ78Codeword(6, 'b')); final String expected = "abbcbcababcaabcaab"; final String actual = new String(LZ78.decode(encoded)); assertEquals(expected, actual); }
public static char[] decode(final List<LZ78Codeword> input) { final List<String> table = new LinkedList<>(); final StringBuilder buffer = new StringBuilder(); for (final LZ78Codeword codeword : input) { final String wordToAdd; if (codeword.getI() == 0) { log.debug("Appending non-existing word: {}", codeword.getX()); wordToAdd = String.valueOf(codeword.getX()); } else { final String wordFromTable = table.get(codeword.getI() - 1); if (codeword.getX() != 0) { log.debug("Appending existing word: {} + terminal `{}`", wordFromTable, codeword.getX()); wordToAdd = wordFromTable + codeword.getX(); } else { log.debug("Appending existing word: {}", wordFromTable); wordToAdd = wordFromTable; } } buffer.append(wordToAdd); table.add(wordToAdd); log.debug("Table of words extended: {}", wordToAdd); } return buffer.toString().toCharArray(); }
LZ78 { public static char[] decode(final List<LZ78Codeword> input) { final List<String> table = new LinkedList<>(); final StringBuilder buffer = new StringBuilder(); for (final LZ78Codeword codeword : input) { final String wordToAdd; if (codeword.getI() == 0) { log.debug("Appending non-existing word: {}", codeword.getX()); wordToAdd = String.valueOf(codeword.getX()); } else { final String wordFromTable = table.get(codeword.getI() - 1); if (codeword.getX() != 0) { log.debug("Appending existing word: {} + terminal `{}`", wordFromTable, codeword.getX()); wordToAdd = wordFromTable + codeword.getX(); } else { log.debug("Appending existing word: {}", wordFromTable); wordToAdd = wordFromTable; } } buffer.append(wordToAdd); table.add(wordToAdd); log.debug("Table of words extended: {}", wordToAdd); } return buffer.toString().toCharArray(); } }
LZ78 { public static char[] decode(final List<LZ78Codeword> input) { final List<String> table = new LinkedList<>(); final StringBuilder buffer = new StringBuilder(); for (final LZ78Codeword codeword : input) { final String wordToAdd; if (codeword.getI() == 0) { log.debug("Appending non-existing word: {}", codeword.getX()); wordToAdd = String.valueOf(codeword.getX()); } else { final String wordFromTable = table.get(codeword.getI() - 1); if (codeword.getX() != 0) { log.debug("Appending existing word: {} + terminal `{}`", wordFromTable, codeword.getX()); wordToAdd = wordFromTable + codeword.getX(); } else { log.debug("Appending existing word: {}", wordFromTable); wordToAdd = wordFromTable; } } buffer.append(wordToAdd); table.add(wordToAdd); log.debug("Table of words extended: {}", wordToAdd); } return buffer.toString().toCharArray(); } private  LZ78(); }
LZ78 { public static char[] decode(final List<LZ78Codeword> input) { final List<String> table = new LinkedList<>(); final StringBuilder buffer = new StringBuilder(); for (final LZ78Codeword codeword : input) { final String wordToAdd; if (codeword.getI() == 0) { log.debug("Appending non-existing word: {}", codeword.getX()); wordToAdd = String.valueOf(codeword.getX()); } else { final String wordFromTable = table.get(codeword.getI() - 1); if (codeword.getX() != 0) { log.debug("Appending existing word: {} + terminal `{}`", wordFromTable, codeword.getX()); wordToAdd = wordFromTable + codeword.getX(); } else { log.debug("Appending existing word: {}", wordFromTable); wordToAdd = wordFromTable; } } buffer.append(wordToAdd); table.add(wordToAdd); log.debug("Table of words extended: {}", wordToAdd); } return buffer.toString().toCharArray(); } private  LZ78(); static List<LZ78Codeword> encode(final char[] input); static char[] decode(final List<LZ78Codeword> input); }
LZ78 { public static char[] decode(final List<LZ78Codeword> input) { final List<String> table = new LinkedList<>(); final StringBuilder buffer = new StringBuilder(); for (final LZ78Codeword codeword : input) { final String wordToAdd; if (codeword.getI() == 0) { log.debug("Appending non-existing word: {}", codeword.getX()); wordToAdd = String.valueOf(codeword.getX()); } else { final String wordFromTable = table.get(codeword.getI() - 1); if (codeword.getX() != 0) { log.debug("Appending existing word: {} + terminal `{}`", wordFromTable, codeword.getX()); wordToAdd = wordFromTable + codeword.getX(); } else { log.debug("Appending existing word: {}", wordFromTable); wordToAdd = wordFromTable; } } buffer.append(wordToAdd); table.add(wordToAdd); log.debug("Table of words extended: {}", wordToAdd); } return buffer.toString().toCharArray(); } private  LZ78(); static List<LZ78Codeword> encode(final char[] input); static char[] decode(final List<LZ78Codeword> input);  }