first the compressor, it has a compress function "Lzw.compress(string)" that returns a list of chars and numbers, and a decompress function "Lzw.decompress(compressed_list) " that takes list of chars and numbers and converts it to the original string
encoder
the encoder is used to transform the list into a string with the encode function "Encoder.encode(list)" and decoded with the decode function "Encoder.decode(string)"
you can take a look at test.src and run it to see it working.
REFERENCE:
best way to explain how this works is to look at an example: i will use the string "adad" as input. first step we compress the input using LZW algorithm, i basically copied the code from here: https://rosettacode.org/wiki/LZW_compression#Ruby this will generate a list like this:Capture d’écran du 2022-12-11 13-25-12.png2.2 KB then it will pass the list to the encoder so it will transform this array into a string, first step of the encoding process is to convert all strings to its char code. Capture d’écran du 2022-12-11 13-30-20.png1.67 KB then convert all numbers into binary. image.png3.76 KB and then define what the CELL_SIZE will be, the CELL_SIZE is the len of the binary of biggest value of the list, in this case 256 which in binary is 100000000 of len 9. then we will convert all binaries in the list to len 9 by adding 0s to the start of it. Capture d’écran du 2022-12-11 13-36-06.png4.56 KB then we define what the FAT_ADDED value will be, we define it by taking the len of CHAR_SET size in binary, which is 7 in this case image.png23.4 KB and then taking the len of the bits stream we will encode, in this case 9 * 3 which is 27, if the bit stream len is not divisible by the char_set size then we will set FAT_ADDED the quantity of "0" we will need to concatenate at the end of the bits stream so to make it divisible. in this case only 1, and we add 1 "0" at the end of the bits stream.image.png3.75 KB then we divide it by the char_set size. Capture d’écran du 2022-12-11 13-47-34.png4.94 KB and then we add the CELL_SIZE and FAT_ADDED value at the start of the stream. image.png6.96 KB after this we just convert all of the values to int, then look at the char_set to get the char we should use. image.png7.08 KB after this process we get the encoded list as string output image.png1.02 KB the decoder is just the inverse of all of this process, it will take the string as input and output a list of chars and ints. Capture d’écran du 2022-12-11 13-53-20.png1.62 KB and with this list we can pass it to decompress and get the original value again.