| 18 | |
| 19 | // apply move-to-front encoding, reading from standard input and writing to standard output |
| 20 | public static void encode() { |
| 21 | char[] index2Char = init(); |
| 22 | while (!BinaryStdIn.isEmpty()) { |
| 23 | char c = BinaryStdIn.readChar(8); |
| 24 | |
| 25 | int idx = 0; |
| 26 | for (; idx < R; idx++) |
| 27 | if (index2Char[idx] == c) break; |
| 28 | for (int i = idx; i > 0; i--) |
| 29 | index2Char[i] = index2Char[i - 1]; |
| 30 | index2Char[0] = c; |
| 31 | |
| 32 | BinaryStdOut.write(idx, 8); |
| 33 | } |
| 34 | BinaryStdOut.close(); |
| 35 | } |
| 36 | |
| 37 | // apply move-to-front decoding, reading from standard input and writing to standard output |
| 38 | public static void decode() { |