diff --git a/include/lzw.h b/include/lzw.h new file mode 100644 index 0000000000000000000000000000000000000000..a878020ffee47ca76b215fde4799894d3cc52fd4 --- /dev/null +++ b/include/lzw.h @@ -0,0 +1,8 @@ +#include <iostream> +#include <string> + +class LZW{ + public: + string pack(string &input); + string unpack(int input[]); +}; \ No newline at end of file diff --git a/src/lzw.cpp b/src/lzw.cpp index cc902508f6ddc776226838a229ab0c30cc6bc985..417754059092908baf42cddbdf568a8ea64b09af 100644 --- a/src/lzw.cpp +++ b/src/lzw.cpp @@ -1,30 +1,86 @@ // Implementado por Eduardo Machado e Victor Perszel -#include <iostram> -#include <string> +#include "lzw.h" using namespace std; -class LZW{ - private: - int dictionarySize = 256; +string LZW::pack(string &input){ + map<string, int> dictionary; + string output = ""; + string nextChar, currentChar = ""; + int i, cont=0, dictionarySize = 256; - public: - compact(const string &input){ - DICT dictionary[]; + //inicializa o dicionário + for(i = 0; i < 256; i++){ + dictionary[string(1, i)] = i; + } - //inicializa o dicionário - for (i=0; i<dictionarySize; i++){ - dictionary.str[0]=i; + for(i = 0; i < input.size(); i++){ + nextChar = input[i]; + if(dictionary[currentChar + nextChar] != 0){ + currentChar = currentChar + nextChar; + }else{ + output += dictionary[currentChar] + " "; + dictionary[currentChar+nextChar] = dictionarySize; + currentChar = nextChar; + dictionarySize++; + cont++; + } + } + output += dictionary[currentChar] + " "; + cont++; + + /* CONVERSAO + int *intOutput; + int temp = n = 0; + intOutput = new int[dictionarySize-1]; + + for(i = 0; i < cont; i++){ + if(output[i] != " "){ + temp++; + } else { + for (j = temp-1; j >= 0; j--){ + intOutput[n] += output[j] * pow(10,j); } + n++; + temp = 0; + } + } + */ + return intOutput; +} - string nextChar, currentChar = ""; - int i; - for(i = 0; i < input.size(); i++){ - nextChar = input[i]; +string LZW::unpack(int input[]){ + map<int, string> dictionary; + string currentChar, nextChar, output = ""; + int i, dictionarySize = 256; + int currentWord, nextWord; - } + //inicializa o dicionário + for (i = 0; i < 256; i++){ + dictionary[i] = string(1, i); + } + + nextWord = input[0]; + output += dictionary[nextWord]; + for(i = 0; i < sizeof(input)/sizeof(int); i++){ + currentWord = nextWord; + nextWord = input[i+1]; + if(dictionary[nextWord] != ""){ + output += dictionary[nextWord]; + currentChar = dictionary[currentWord]; + nextChar = dictionary[nextWord][1]; + dictionary[dictionarySize] = currentChar + nextChar; + dictionarySize++; + }else{ + currentChar = dictionary[currentWord]; + nextChar = dictionary[currentWord][1]; + output += currentChar + nextChar; + dictionary[dictionarySize] = currentChar + nextChar; + dictionarySize++; } + } + return output; } \ No newline at end of file