From 980d168a4f112317eda71579ab4c26864a0b36ce Mon Sep 17 00:00:00 2001 From: ctrindadedev Date: Sun, 28 Sep 2025 17:01:22 -0300 Subject: [PATCH 1/3] add palavras reservadas da linguagem c++ --- src/freq_counter/main.cpp | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/freq_counter/main.cpp b/src/freq_counter/main.cpp index bca384c..d29bfaa 100644 --- a/src/freq_counter/main.cpp +++ b/src/freq_counter/main.cpp @@ -22,12 +22,30 @@ int main(int argc, char** argv) { std::map freqs; std::set keyWords = { - "int", "double", "while", "return", "include", "new", "class", "public", - "private", "protected", "if", "else", "for", "switch", "case", "break", - "continue", "struct", "enum", "namespace", "using", "const", "char", - "void", "unsigned", "long", "short", "bool", "true", "false", "nullptr", "Node", "Interface" - //Adicioanr mais, tendo em vista que o professor pediu que seja compressor de várias linguagens - }; + "alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", "bitor", + "bool", "break", "case", "catch", "char", "char8_t", "char16_t", "char32_t", + "class", "compl", "concept", "const", "consteval", "constexpr", "constinit", + "const_cast", "continue", "co_await", "co_return", "co_yield", "decltype", + "default", "delete", "do", "double", "dynamic_cast", "else", "enum", + "explicit", "export", "extern", "false", "float", "for", "friend", "goto", + "if", "inline", "int", "long", "mutable", "namespace", "new", "noexcept", + "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", + "protected", "public", "register", "reinterpret_cast", "requires", "return", + "short", "signed", "sizeof", "static", "static_assert", "static_cast", + "struct", "switch", "template", "this", "thread_local", "throw", "true", + "try", "typedef", "typeid", "typename", "union", "unsigned", "using", + "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq", + // Identificadores com significado especial + "final", "override", "import", "module", + // Diretivas de pré-processador (muito comuns em .cpp/.h) + "define", "elif", "elifdef", "elifndef", "embed", "endif", "error", + "ifdef", "ifndef", "include", "line", "pragma", "undef", "warning", + // Tipos e classes comuns da (STL) + "std", "string", "vector", "map", "set", "iostream", "fstream", "sstream", + "memory", "shared_ptr", "unique_ptr", "make_shared", "make_unique", + "cout", "cin", "cerr", "endl" +}; //Fonte das palavras reservadas: https://en.cppreference.com/w/cpp/keywords.html e arquivos .h de antigos projetos + std::vector inputFiles; std::string outputFile = ""; From 49c29dd78e2c5df0c46b238d3215203e6728c254 Mon Sep 17 00:00:00 2001 From: ctrindadedev Date: Sun, 28 Sep 2025 19:17:13 -0300 Subject: [PATCH 2/3] add headers das funcoes serializer e deserializer --- include/huffman/huffman_tree.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/huffman/huffman_tree.h b/include/huffman/huffman_tree.h index 160cc4c..663ff19 100644 --- a/include/huffman/huffman_tree.h +++ b/include/huffman/huffman_tree.h @@ -5,6 +5,7 @@ #include #include #include +#include "../../include/huffman/bit_stream.h" // struct HNode { // uint8_t symbol; @@ -26,11 +27,15 @@ class HuffmanTree { HuffmanTree(); void build(const std::map &freqs); // freqs size 256 std::map> getCodes() const; + std::shared_ptr getRoot() const; void serialize(std::ostream &out) const; void deserialize(std::istream &in); + private: std::shared_ptr root_; void buildCodes(std::shared_ptr node, std::vector &path, std::map> &out) const; + void serialize_helper(std::shared_ptr node, BitOutputStream& bit_out) const; + std::shared_ptr deserialize_helper(BitInputStream& bit_in); }; #endif From e3f044dcaef0c586e8549e498b2122dcf3469d7a Mon Sep 17 00:00:00 2001 From: ctrindadedev Date: Sun, 28 Sep 2025 19:18:33 -0300 Subject: [PATCH 3/3] =?UTF-8?q?add=20funcoes=20que=20implemtam=20a=20logic?= =?UTF-8?q?a=20pre-ordem=20e=20faz=20a=20gera=C3=A7=C3=A3o=20dos=20codigos?= =?UTF-8?q?=20binarios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/huffman_tree.cpp | 113 ++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 19 deletions(-) diff --git a/src/lib/huffman_tree.cpp b/src/lib/huffman_tree.cpp index 96f19ef..560d884 100644 --- a/src/lib/huffman_tree.cpp +++ b/src/lib/huffman_tree.cpp @@ -1,31 +1,95 @@ #include "../../include/huffman/huffman_tree.h" +#include "../../include/huffman/bit_stream.h" #include #include +void serialize_helper(std::shared_ptr node, BitOutputStream& bit_out) { + if (node == nullptr) { + return; + } + // Lógica de pré-ordem para visitar a raiz + if (node->isLeaf()) { + bit_out.writeBit(true); + // Escreve o tamanho da string como um único byte, com 8 bits para o tamanho + uint8_t len = node->symbol.length(); + bit_out.writeBits(len, 8); + + for (char c : node->symbol) { + bit_out.writeBits(static_cast(c), 8); + } + //Caso nó interno + } else { + + bit_out.writeBit(false); + // Percorre recursivamente a esquerda e a direita (Assim como o conceito de pré-ordem) + serialize_helper(node->left, bit_out); + serialize_helper(node->right, bit_out); + } +} + +std::shared_ptr deserialize_helper(BitInputStream& bit_in) { + int bit = bit_in.readBit(); + if (bit == -1) { + return nullptr; + } + + // Caso nó-folha + if (bit == 1) { + auto node = std::make_shared(); + + uint8_t len = 0; + for(int i=0; i<8; ++i){ + len = (len << 1) | bit_in.readBit(); + } + + // FAz A leitrua dos caracteres do símbolo + std::string symbol = ""; + for(int i=0; i(ch); + } + node->symbol = symbol; + return node; + } + // Caso nó interno + else { + auto node = std::make_shared(); + node->symbol = ""; // Nós internos não têm símbolo + node->left = deserialize_helper(bit_in); + node->right = deserialize_helper(bit_in); + return node; + } +} + HuffmanTree::HuffmanTree(): root_(nullptr) {} -void HuffmanTree::build(const std::vector &freqs){ +void HuffmanTree::build(const std::map &freqs){ struct QNode { std::shared_ptr node; uint64_t freq; - bool operator<(QNode const& other) const { return freq > other.freq; } // for min-heap + bool operator<(const QNode& other) const { return freq > other.freq; } }; + std::priority_queue pq; - for(int i=0;i<256;i++){ - if(freqs[i]>0){ + for(const auto& pair : freqs){ + if(pair.second > 0){ auto n = std::make_shared(); - n->symbol = static_cast(i); - n->freq = freqs[i]; - pq.push(QNode{n, freqs[i]}); + n->symbol = pair.first; + n->freq = pair.second; + pq.push(QNode{n, n->freq}); } } - if(pq.empty()){ - return; - } + + if(pq.empty()){ return; } + while(pq.size() > 1){ auto a = pq.top(); pq.pop(); auto b = pq.top(); pq.pop(); auto parent = std::make_shared(); + parent->symbol = ""; // Nós internos são vazios para servir apenas como conectores de estruturas parent->left = a.node; parent->right = b.node; parent->freq = a.freq + b.freq; @@ -34,16 +98,17 @@ void HuffmanTree::build(const std::vector &freqs){ root_ = pq.top().node; } -std::map> HuffmanTree::getCodes() const { - std::map> out; +std::map> HuffmanTree::getCodes() const { + std::map> out; if(root_) { std::vector path; - const_cast(this)->buildCodes(root_, path, out); + buildCodes(root_, path, out); } return out; } -void HuffmanTree::buildCodes(std::shared_ptr node, std::vector &path, std::map> &out) const { + +void HuffmanTree::buildCodes(std::shared_ptr node, std::vector &path, std::map> &out) const { if(!node) return; if(node->isLeaf()){ out[node->symbol] = path; @@ -51,16 +116,26 @@ void HuffmanTree::buildCodes(std::shared_ptr node, std::vector &pat } path.push_back(false); buildCodes(node->left, path, out); - path.back() = true; + path.pop_back(); // Desfaz a mudança para o caminho da direita + + path.push_back(true); buildCodes(node->right, path, out); - path.pop_back(); + path.pop_back(); // Limpa o caminho ao retornar da recursão } + void HuffmanTree::serialize(std::ostream &out) const { - // stub: implement pre-order serialization with bits - out << ""; // placeholder + BitOutputStream bit_out(out); + serialize_helper(root_, bit_out); + bit_out.flush(); // Garante que qualquer bit restante no buffer seja escrito } void HuffmanTree::deserialize(std::istream &in) { - // stub: implement de-serialization + BitInputStream bit_in(in); + root_ = deserialize_helper(bit_in); +} + +// Util para o descompressor +std::shared_ptr HuffmanTree::getRoot() const { + return root_; }