From b1f3b5839f064ca047a02f3a1f0cb97d1edb3442 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 11:51:42 +0200 Subject: [PATCH 1/7] Fixed compilation parameters for multi-threading --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd131c2..9ac461b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(TGT "projet_lzw") set(${TGT}_VERSION_MAJOR 0) set(${TGT}_VERSION_MINOR 1) -set(CXX_COVERAGE_COMPILE_FLAGS "-pedantic -Wall -Wextra -Wold-style-cast -Woverloaded-virtual -Wfloat-equal -Wwrite-strings -Wpointer-arith -Wcast-qual -Wcast-align -Wconversion -Wsign-conversion -Wshadow -Weffc++ -Wredundant-decls -Wdouble-promotion -Winit-self -Wswitch-default -Wswitch-enum -Wundef -Winline -Wunused -Wnon-virtual-dtor") +set(CXX_COVERAGE_COMPILE_FLAGS "-pedantic -Wall -Wextra -Wold-style-cast -Woverloaded-virtual -Wfloat-equal -Wwrite-strings -Wpointer-arith -Wcast-qual -Wcast-align -Wconversion -Wsign-conversion -Wshadow -Weffc++ -Wredundant-decls -Wdouble-promotion -Winit-self -Wswitch-default -Wswitch-enum -Wundef -Winline -Wunused -Wnon-virtual-dtor -pthread") set(CMAKE_CXX_FLAGS_DEBUG "${CXX_COVERAGE_COMPILE_FLAGS} -DDebug -g -pg") set(CMAKE_CXX_FLAGS_RELEASE "${CXX_COVERAGE_COMPILE_FLAGS} -O3") From a4f722f6fb88c6014a57dcc85624b45e00a1e7bb Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 11:54:02 +0200 Subject: [PATCH 2/7] parallel compression kinda working, but ultimately segfault --- .gitignore | 5 ++ src/common.cc | 12 ++--- src/common.hh | 7 ++- src/compress.cc | 123 +++++++++++++++++++++++++++++++----------------- src/compress.hh | 7 ++- src/main.cc | 74 +++++++++++++++++------------ 6 files changed, 142 insertions(+), 86 deletions(-) diff --git a/.gitignore b/.gitignore index 792db02..e4c99a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ *~ gmon\.out +/cmake-build-debug/Makefile + +cmake-build-debug/ + +\.idea/ diff --git a/src/common.cc b/src/common.cc index 05496cb..d223f5f 100644 --- a/src/common.cc +++ b/src/common.cc @@ -6,8 +6,8 @@ #include "common.hh" using uint8_t = std::uint8_t; -using uint32_t = std::uint32_t; -using dic_t = std::map, uint32_t>; +using uint16_t = std::uint16_t; +using dic_t = std::map, uint16_t>; /** * Cette fonction a pour double usage la recherche d’une chaine de caractères @@ -27,10 +27,10 @@ using dic_t = std::map, uint32_t>; * \param c Caractère suivant la chaine de caractères \p nr_chaine * \return std::pair */ -const std::pair -dico(std::map, uint32_t> &t_dictionary, - uint32_t t_nr_chaine, uint8_t t_c) { - if (t_nr_chaine == 0xFFFFFFFF) +const std::pair +dico(std::map, uint16_t> &t_dictionary, + uint16_t t_nr_chaine, uint8_t t_c) { + if (t_nr_chaine == 0xFFFF) return std::make_pair(true, t_c); auto &e = t_dictionary[std::make_pair(t_nr_chaine, t_c)]; return (e) ? std::make_pair(true, e) diff --git a/src/common.hh b/src/common.hh index 7cfd222..cc1b367 100644 --- a/src/common.hh +++ b/src/common.hh @@ -10,9 +10,8 @@ #include /// \brief Recherche ou ajout de chaine dans le dictionnaire -const std::pair -dico(std::map, std::uint32_t> - &t_dictionary, - std::uint32_t t_nr_chaine, std::uint8_t t_c); +const std::pair +dico(std::map, std::uint16_t> &, + uint16_t, uint8_t); #endif /* LZW_SRC_COMMON_H_ */ diff --git a/src/compress.cc b/src/compress.cc index fe43f1d..d3a040a 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -3,14 +3,22 @@ * \brief Implementation of compression */ +#ifdef Debug +constexpr bool debug_mode = true; +#else +constexpr bool debug_mode = false; +#endif + #include "compress.hh" #include "utf8.hh" #include #include +#include +#include -using dict_t = std::map, uint32_t>; +using dict_t = std::map, uint16_t>; using ustring = std::basic_string; // chaine non encodée -using uvec = std::vector; // chaine encodée +using uvec = std::vector; // chaine encodée using std::printf; /** @@ -24,39 +32,40 @@ using std::printf; * \param t_dictionary Dictionnaire de compression * \return std::vector */ -const uvec lzw_compress(const ustring &t_text, dict_t &t_dictionary) { +void lzw_compress(const std::vector &t_text, uvec &t_res) { + dict_t dictionary{}; std::puts("Compressing..."); - uvec res{}; - uint32_t w = 0xFFFFFFFF; - uint32_t len = 0; + // uvec res{}; + uint16_t w = 0xFFFF; + uint16_t len = 0; constexpr size_t DICT_MAX = 7936; /* 12 bits */ -#ifdef Debug size_t progress = 0; -#endif for (const auto &c : t_text) { ++len; -#ifdef Debug - printf("\rprogress: %zu / %zu", ++progress, t_text.size()); -#endif + if constexpr (debug_mode) { + printf("\rprogress: %zu / %zu", ++progress, t_text.size()); + } - if (/* len > LENGTH_MAX || */ t_dictionary.size() >= DICT_MAX) { - res.push_back(static_cast(w)); - w = c; + if (/* len > LENGTH_MAX || */ dictionary.size() >= DICT_MAX) { + t_res.push_back(static_cast(w)); + w = static_cast(c); len = 0; - } else if (const auto &[exists, pos] = dico(t_dictionary, w, c); exists) { + } else if (const auto &[exists, pos] = + dico(dictionary, w, static_cast(c)); + exists) { w = pos; } else { - res.push_back(static_cast(w)); - w = c; + t_res.push_back(static_cast(w)); + w = static_cast(c); len = 0; } } printf("\n"); - return res; + // return res; } /** @@ -73,41 +82,69 @@ const uvec lzw_compress(const ustring &t_text, dict_t &t_dictionary) { void compress(const std::string &t_in_file, const char *t_out_file) { // Fichier d’entrée std::ifstream input_file{t_in_file}; + if(!input_file.is_open()) { + std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 2 + << ": could not open output file \"" << t_in_file << "\". Aborting...\n"; + exit(1); + } // Fichier de sortie - FILE *out = (t_out_file) - ? fopen(t_out_file, "wb") - : fopen(std::string{t_out_file, ".lzw"}.c_str(), "wb"); + const char *filename = + (t_out_file) ? t_out_file : "output.lzw"; + FILE *out = fopen(filename, "wb"); + if(!out) { + std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 4 + << ": could not open output file \"" << filename << "\". Aborting...\n"; + input_file.close(); + exit(1); + } - input_file.seekg(0, std::ios::end); - // string contenant le fichier d’entrée - ustring str(static_cast(input_file.tellg()), - static_cast(0)); - input_file.seekg(0, std::ios::beg); + // input_file.seekg(0, std::ios::end); + // // string contenant le fichier d’entrée + // ustring str(static_cast(input_file.tellg()), + // static_cast(0)); + // input_file.seekg(0, std::ios::beg); - // assignation du contenu du fichier à str - str.assign((std::istreambuf_iterator(input_file)), - std::istreambuf_iterator()); + // // assignation du contenu du fichier à str + // str.assign((std::istreambuf_iterator(input_file)), + // std::istreambuf_iterator()); - printf("Size of input file: %zu\n", str.size()); + // printf("Size of input file: %zu\n", str.size()); - dict_t dictionary{}; + // dict_t dictionary{}; - const auto comp_str{lzw_compress(str, dictionary)}; + // const auto comp_str{lzw_compress(str, dictionary)}; - printf("\n############################################\n"); - printf(" Compressed!\n"); - printf("############################################\n\n"); - printf("Size of compressed string: %zu\n", comp_str.size()); - printf("Size of dictionary: %zu\n", dictionary.size()); - printf("Compression ratio: %.10f\n", - static_cast(str.size() / comp_str.size())); + // thread pool + std::vector> threads{}; - for(const auto c : comp_str) - write_utf8(out, c); + // char chunk[32768]; + std::vector chunk{}; + chunk.reserve(32768); + while (input_file.read(chunk.data(), 32768)) { + threads.push_back(std::make_pair(std::thread{}, uvec{})); + threads.back().first = + std::thread{lzw_compress, chunk, ref(threads.back().second)}; + if (threads.size() >= 8) { + for (auto &elem : threads) { + elem.first.join(); + } + for (auto &elem : threads) { + for (const auto c : elem.second) { + write_utf8(out, c); + } + } + threads.clear(); + } + } + + if(input_file.tellg() != std::ios::end) { + std::puts("Leftovers..."); + } + + // for (const auto c : comp_str) + // write_utf8(out, c); fclose(out); input_file.close(); - - return; } diff --git a/src/compress.hh b/src/compress.hh index 848a634..30de860 100644 --- a/src/compress.hh +++ b/src/compress.hh @@ -8,12 +8,11 @@ #include "common.hh" #include +#include /// \brief Compression d'une chaine de caractères -const std::vector -lzw_compress(const std::basic_string &t_text, - std::map, std::uint32_t> - &t_dictionary); +void lzw_compress(const std::vector &t_text, + std::vector &t_rec); /// \brief Wrapper de \ref lzw_compress void compress(const std::string &t_in_file, const char *t_out_file); diff --git a/src/main.cc b/src/main.cc index 7fefe6c..c9641e6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,6 +6,12 @@ * */ +#ifdef Debug +constexpr bool debug_mode = true; +#else +constexpr bool debug_mode = false; +#endif + #include "compress.hh" #include "getopt.h" @@ -48,10 +54,11 @@ void help() { } int main(int argc, char *argv[]) { -#ifdef Debug - for (int i = 0; i < argc; ++i) - printf("argv[%d] = %s\n", i, argv[i]); -#endif + + if constexpr (debug_mode) { + for (int i = 0; i < argc; ++i) + printf("argv[%d] = %s\n", i, argv[i]); + } std::string input_path{}; std::string output_path{}; @@ -71,54 +78,55 @@ int main(int argc, char *argv[]) { break; switch (c) { case 0: { -#ifdef Debug - printf("\noption %s", long_options[option_index].name); - if (optarg) { - printf(" with arg %s\n", optarg); + if constexpr (debug_mode) { + printf("\noption %s", long_options[option_index].name); + if (optarg) { + printf(" with arg %s\n", optarg); + } } -#endif break; } case 'h': { -#ifdef Debug - printf("From main - option --help passed\n"); -#endif + if constexpr (debug_mode) { + printf("From main - option --help passed\n"); + } help(); return 0; } case 'i': { -#ifdef Debug - printf("From main - option --input with value '%s'\n", optarg); -#endif + if constexpr (debug_mode) { + printf("From main - option --input with value '%s'\n", optarg); + } input_path = optarg; break; } case 'o': { -#ifdef Debug - printf("From main - option --output with value '%s'\n", optarg); -#endif + if constexpr (debug_mode) { + printf("From main - option --output with value '%s'\n", optarg); + } output_path = optarg; break; } case 'c': { -#ifdef Debug - printf("From main - option --compress\n"); -#endif + if constexpr (debug_mode) { + printf("From main - option --compress\n"); + } compressing = true; break; } case 'u': { -#ifdef Debug - printf("From main - option --uncompress\n"); -#endif + if constexpr (debug_mode) { + printf("From main - option --uncompress\n"); + } compressing = false; break; } - case '?': { + case '?': + default: { puts("Error: unknown parameter."); -#ifdef Debug - printf("From main - option -?\n"); -#endif + if constexpr (debug_mode) { + printf("From main - option -?\n"); + } help(); return 1; } @@ -140,7 +148,15 @@ int main(int argc, char *argv[]) { - bit-packing, limiter la taille du dictionnaire pour un certain nombre de bits. */ - compress(input_path, output_path.c_str()); + if constexpr (debug_mode) { + puts("Beginning compression"); + } + if(output_path.empty()) { + compress(input_path, nullptr); + } else { + compress(input_path, output_path.c_str()); + } + // compress(input_path, output_path.c_str()); } else { puts("Not yet implemented :("); /* From d7ea7c8866ce443c727c2e06f896d5d20d72939a Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 11:59:09 +0200 Subject: [PATCH 3/7] Fixed issue with threads leftovers --- .gitignore | 1 + src/compress.cc | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e4c99a6..c5a5fba 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ gmon\.out cmake-build-debug/ \.idea/ +*.lzw diff --git a/src/compress.cc b/src/compress.cc index d3a040a..0278200 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -116,18 +116,18 @@ void compress(const std::string &t_in_file, const char *t_out_file) { // const auto comp_str{lzw_compress(str, dictionary)}; // thread pool - std::vector> threads{}; + std::vector, uvec>> threads{}; // char chunk[32768]; std::vector chunk{}; chunk.reserve(32768); while (input_file.read(chunk.data(), 32768)) { - threads.push_back(std::make_pair(std::thread{}, uvec{})); - threads.back().first = - std::thread{lzw_compress, chunk, ref(threads.back().second)}; + threads.push_back(std::make_pair(nullptr, uvec{})); + threads.back().first = std::make_unique( + std::thread{lzw_compress, chunk, ref(threads.back().second)}); if (threads.size() >= 8) { for (auto &elem : threads) { - elem.first.join(); + (*elem.first).join(); } for (auto &elem : threads) { for (const auto c : elem.second) { @@ -138,6 +138,18 @@ void compress(const std::string &t_in_file, const char *t_out_file) { } } + if(threads.size() != 0) { + for (auto &elem : threads) { + (*elem.first).join(); + } + for (auto &elem : threads) { + for (const auto c : elem.second) { + write_utf8(out, c); + } + } + threads.clear(); + } + if(input_file.tellg() != std::ios::end) { std::puts("Leftovers..."); } From b7f96f0557e8e7d1e296d097ff74c3627247aecc Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 13:49:39 +0200 Subject: [PATCH 4/7] Fixed empty chunk issue, still unexpected errors --- src/common.cc | 16 +++--- src/common.hh | 6 +-- src/compress.cc | 136 +++++++++++++++++++++--------------------------- src/compress.hh | 5 +- src/main.cc | 6 +-- 5 files changed, 76 insertions(+), 93 deletions(-) diff --git a/src/common.cc b/src/common.cc index d223f5f..19477e6 100644 --- a/src/common.cc +++ b/src/common.cc @@ -6,8 +6,8 @@ #include "common.hh" using uint8_t = std::uint8_t; -using uint16_t = std::uint16_t; -using dic_t = std::map, uint16_t>; +using uint32_t = std::uint32_t; +using dic_t = std::map, uint32_t>; /** * Cette fonction a pour double usage la recherche d’une chaine de caractères @@ -18,18 +18,18 @@ using dic_t = std::map, uint16_t>; * caractère se référant au dernier caractère de la chaine courante. Si le * numéro de chaine est -1, alors il s’agit du premier caractère de la chaine, * et la valeur renvoyée sera la valeur ASCII du caractère. La fonction renvoie - * une paire bool/uint16_t, la valeur booléene indiquant si une nouvelle fut - * ajoutée dans le dictionnaire ou non, et le uint16_t indiquant la valeur + * une paire bool/uint32_t, la valeur booléene indiquant si une nouvelle fut + * ajoutée dans le dictionnaire ou non, et le uint32_t indiquant la valeur * numérique de la chaîne dans le dictionnaire. * * \param dictionary Dictionnaire * \param nr_chaine Numéro de la chaine précédant le caractères \p c dans \p dictionary * \param c Caractère suivant la chaine de caractères \p nr_chaine - * \return std::pair + * \return std::pair */ -const std::pair -dico(std::map, uint16_t> &t_dictionary, - uint16_t t_nr_chaine, uint8_t t_c) { +const std::pair +dico(std::map, uint32_t> &t_dictionary, + uint32_t t_nr_chaine, uint8_t t_c) { if (t_nr_chaine == 0xFFFF) return std::make_pair(true, t_c); auto &e = t_dictionary[std::make_pair(t_nr_chaine, t_c)]; diff --git a/src/common.hh b/src/common.hh index cc1b367..1406ab8 100644 --- a/src/common.hh +++ b/src/common.hh @@ -10,8 +10,8 @@ #include /// \brief Recherche ou ajout de chaine dans le dictionnaire -const std::pair -dico(std::map, std::uint16_t> &, - uint16_t, uint8_t); +const std::pair +dico(std::map, std::uint32_t> &, + uint32_t, uint8_t); #endif /* LZW_SRC_COMMON_H_ */ diff --git a/src/compress.cc b/src/compress.cc index 0278200..b5406fb 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -16,56 +16,55 @@ constexpr bool debug_mode = false; #include #include -using dict_t = std::map, uint16_t>; -using ustring = std::basic_string; // chaine non encodée -using uvec = std::vector; // chaine encodée +using dict_t = std::map, uint32_t>; +using ustring = std::basic_string; // chaîne non encodée +using uvec = std::vector; // chaîne encodée using std::printf; +void join_and_write( + std::vector, uvec>> &t_threads, + FILE *t_out) { + for (auto &elem : t_threads) { + (*elem.first).join(); + } + for (auto &elem : t_threads) { + for (const auto c : elem.second) { + write_utf8(t_out, c); + } + } + t_threads.clear(); +} + /** - * La chaine de caractère \p text est lue caractère par caractère, et est et + * La chaîne de caractère \p t_text est lue caractère par caractère, et est et * selon la valeur de retour de la fonction \ref dico (permettant dans le même * temps la création du dictionnaire), on rajoute un mot ou pas dans le vecteur * de caractères UTF-8 représentant des mots de chars compressés. La fonction - * renvoie ledit vecteur de uint32_t. + * renvoie ledit vecteur de uint32_t via le paramètre \p t_res. * - * \param t_text Chaine de caractères uint8_t représentant le fichier d'entrée - * \param t_dictionary Dictionnaire de compression - * \return std::vector + * \param[in] t_text Chaîne de caractères uint8_t représentant le fichier + * d'entrée \param[out] t_res Chaîne de caractères de sortie */ void lzw_compress(const std::vector &t_text, uvec &t_res) { dict_t dictionary{}; std::puts("Compressing..."); - // uvec res{}; - uint16_t w = 0xFFFF; - uint16_t len = 0; + uint32_t w = 0xFFFF; constexpr size_t DICT_MAX = 7936; /* 12 bits */ - size_t progress = 0; - for (const auto &c : t_text) { - ++len; - - if constexpr (debug_mode) { - printf("\rprogress: %zu / %zu", ++progress, t_text.size()); - } - - if (/* len > LENGTH_MAX || */ dictionary.size() >= DICT_MAX) { - t_res.push_back(static_cast(w)); - w = static_cast(c); - len = 0; + if (dictionary.size() >= DICT_MAX) { + t_res.push_back(static_cast(w)); + w = static_cast(c); } else if (const auto &[exists, pos] = dico(dictionary, w, static_cast(c)); exists) { w = pos; } else { - t_res.push_back(static_cast(w)); + t_res.push_back(static_cast(w)); w = static_cast(c); - len = 0; } } - printf("\n"); - // return res; } /** @@ -82,81 +81,66 @@ void lzw_compress(const std::vector &t_text, uvec &t_res) { void compress(const std::string &t_in_file, const char *t_out_file) { // Fichier d’entrée std::ifstream input_file{t_in_file}; - if(!input_file.is_open()) { + if (!input_file.is_open()) { std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 2 - << ": could not open output file \"" << t_in_file << "\". Aborting...\n"; + << ": could not open output file \"" << t_in_file + << "\". Aborting...\n"; exit(1); } // Fichier de sortie - const char *filename = - (t_out_file) ? t_out_file : "output.lzw"; + const char *filename = (t_out_file) ? t_out_file : "output.lzw"; FILE *out = fopen(filename, "wb"); - if(!out) { + if (!out) { std::cerr << "Error at " << __FILE__ << ":" << __LINE__ - 4 - << ": could not open output file \"" << filename << "\". Aborting...\n"; + << ": could not open output file \"" << filename + << "\". Aborting...\n"; input_file.close(); exit(1); } - // input_file.seekg(0, std::ios::end); - // // string contenant le fichier d’entrée - // ustring str(static_cast(input_file.tellg()), - // static_cast(0)); - // input_file.seekg(0, std::ios::beg); - - // // assignation du contenu du fichier à str - // str.assign((std::istreambuf_iterator(input_file)), - // std::istreambuf_iterator()); - - // printf("Size of input file: %zu\n", str.size()); - - // dict_t dictionary{}; - - // const auto comp_str{lzw_compress(str, dictionary)}; - // thread pool std::vector, uvec>> threads{}; // char chunk[32768]; - std::vector chunk{}; - chunk.reserve(32768); - while (input_file.read(chunk.data(), 32768)) { - threads.push_back(std::make_pair(nullptr, uvec{})); + std::vector chunk(32768, 0); + while (input_file.read(chunk.data(), + static_cast(chunk.size()))) { + printf("\n"); + threads.emplace_back(nullptr, uvec{}); threads.back().first = std::make_unique( std::thread{lzw_compress, chunk, ref(threads.back().second)}); if (threads.size() >= 8) { - for (auto &elem : threads) { - (*elem.first).join(); - } - for (auto &elem : threads) { - for (const auto c : elem.second) { - write_utf8(out, c); - } - } - threads.clear(); + join_and_write(threads, out); } } - if(threads.size() != 0) { - for (auto &elem : threads) { - (*elem.first).join(); + if (!threads.empty()) { + join_and_write(threads, out); + } + + if (input_file.tellg() != std::ios::end) { + std::puts("Leftovers, compressing..."); + { + const auto prev_pos = input_file.tellg(); + input_file.seekg(0, std::ios::end); + chunk.reserve(static_cast(input_file.tellg() - prev_pos)); + input_file.seekg(prev_pos, std::ios::beg); + std::istreambuf_iterator itr(input_file); + for (std::streamoff i = 0; i < prev_pos; ++i, ++itr) + ; + chunk.assign((itr), std::istreambuf_iterator()); } - for (auto &elem : threads) { - for (const auto c : elem.second) { - write_utf8(out, c); + uvec ret{}; + lzw_compress(chunk, ret); + for (const auto c : ret) { + if constexpr (debug_mode) { + printf("%c\t", c); } + write_utf8(out, c); } - threads.clear(); } - if(input_file.tellg() != std::ios::end) { - std::puts("Leftovers..."); - } - - // for (const auto c : comp_str) - // write_utf8(out, c); - fclose(out); input_file.close(); } diff --git a/src/compress.hh b/src/compress.hh index 30de860..8c3ddcb 100644 --- a/src/compress.hh +++ b/src/compress.hh @@ -11,10 +11,9 @@ #include /// \brief Compression d'une chaine de caractères -void lzw_compress(const std::vector &t_text, - std::vector &t_rec); +void lzw_compress(const std::vector &, std::vector &); /// \brief Wrapper de \ref lzw_compress -void compress(const std::string &t_in_file, const char *t_out_file); +void compress(const std::string &, const char *); #endif /* LZW_SRC_COMPRESS_H_ */ diff --git a/src/main.cc b/src/main.cc index c9641e6..9afc659 100644 --- a/src/main.cc +++ b/src/main.cc @@ -64,7 +64,7 @@ int main(int argc, char *argv[]) { std::string output_path{}; bool compressing = true; - while (1) { + while (true) { int option_index = 0; static struct option long_options[] = { {"help", no_argument, nullptr, 'h'}, @@ -72,7 +72,7 @@ int main(int argc, char *argv[]) { {"output", required_argument, nullptr, 'o'}, {"compress", no_argument, nullptr, 'c'}, {"uncompress", no_argument, nullptr, 'u'}, - {0, 0, 0, 0}}; + {nullptr, 0, nullptr, 0}}; int c = getopt_long(argc, argv, "hi:o:cu", long_options, &option_index); if (c == -1) break; @@ -133,7 +133,7 @@ int main(int argc, char *argv[]) { } } - if (input_path == "") { + if (input_path.empty()) { puts("Error: no input file specified"); return 2; } From 311898b36300f1341c9ac2bd1145d8e5332ffef3 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 13:56:43 +0200 Subject: [PATCH 5/7] added assertion thread exists --- src/compress.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/compress.cc b/src/compress.cc index b5406fb..2966ac0 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -3,19 +3,20 @@ * \brief Implementation of compression */ +#include "compress.hh" +#include "utf8.hh" +#include +#include +#include +#include +#include + #ifdef Debug constexpr bool debug_mode = true; #else constexpr bool debug_mode = false; #endif -#include "compress.hh" -#include "utf8.hh" -#include -#include -#include -#include - using dict_t = std::map, uint32_t>; using ustring = std::basic_string; // chaîne non encodée using uvec = std::vector; // chaîne encodée @@ -106,10 +107,10 @@ void compress(const std::string &t_in_file, const char *t_out_file) { std::vector chunk(32768, 0); while (input_file.read(chunk.data(), static_cast(chunk.size()))) { - printf("\n"); threads.emplace_back(nullptr, uvec{}); threads.back().first = std::make_unique( std::thread{lzw_compress, chunk, ref(threads.back().second)}); + assert(threads.back().first); if (threads.size() >= 8) { join_and_write(threads, out); } From 494c0a9b6049c2fe1892f5349e2eea1926dd2bda Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 14:00:38 +0200 Subject: [PATCH 6/7] Added needed header --- src/compress.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compress.cc b/src/compress.cc index 2966ac0..924660b 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #ifdef Debug From a8cce9e7e91b888263eb4441816aab16e9ffae45 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 14:37:10 +0200 Subject: [PATCH 7/7] Modified for TravisCI --- src/compress.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compress.cc b/src/compress.cc index 924660b..f928b49 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -9,7 +9,6 @@ #include #include #include -#include #include #ifdef Debug @@ -109,8 +108,8 @@ void compress(const std::string &t_in_file, const char *t_out_file) { while (input_file.read(chunk.data(), static_cast(chunk.size()))) { threads.emplace_back(nullptr, uvec{}); - threads.back().first = std::make_unique( - std::thread{lzw_compress, chunk, ref(threads.back().second)}); + threads.back().first = std::unique_ptr( + new std::thread{lzw_compress, chunk, ref(threads.back().second)}); assert(threads.back().first); if (threads.size() >= 8) { join_and_write(threads, out);