diff --git a/ex02/src/PmergeMe.hpp b/ex02/src/PmergeMe.hpp index defd93a..548d7c9 100644 --- a/ex02/src/PmergeMe.hpp +++ b/ex02/src/PmergeMe.hpp @@ -64,6 +64,8 @@ void insert_sort(const std::pair* pairs, size_t len, T& array) template void PmergeMe(T& array) { + if (array.size() < 2) + return; std::size_t len = array.size() / 2 + array.size() % 2; std::pair* pairs = create_pairs(array, len); sort_pair(pairs, len); diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index f7de1fd..6f01395 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -1,4 +1,5 @@ #include "PmergeMe.hpp" +#include #include #include #include @@ -17,20 +18,23 @@ void display(const T& array) int main(int ac, char** av) { { - std::vector array(ac - 1); + std::vector array; for (int i = 1; i != ac; i++) { for (int j = 0; av[i][j] != '\0'; j++) { - if (!std::isdigit(av[i][j])) + if (j == 0 || av[i][j] == ' ') + array.push_back(atoi(av[i] + j + (j != 0))); + if (!std::isdigit(av[i][j]) and av[i][j] != ' ') { std::cout << "Error" << std::endl; return 1; } } - array[i - 1] = atoi(av[i]); } + if (array.size() > 0) + array.pop_back(); std::cout << "Before: "; display(array); @@ -48,13 +52,23 @@ int main(int ac, char** av) std::cout << "Time to process a range of " << array.size() <<" elements with std::vector : " << ((double) (time_vec_stop - time_vec_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl; } { - std::deque array(ac - 1); - for (int i = 1; i != ac; i++) - array[i - 1] = atoi(av[i]); + std::deque array; - std::cout << "Before: "; - display(array); - std::cout << std::endl; + for (int i = 1; i != ac; i++) + { + for (int j = 0; av[i][j] != '\0'; j++) + { + if (j == 0 || av[i][j] == ' ') + array.push_back(atoi(av[i] + j + (j != 0))); + else if (!std::isdigit(av[i][j])) + { + std::cout << "Error" << std::endl; + return 1; + } + } + } + if (array.size() > 0) + array.pop_back(); const clock_t time_deque_start = clock();