diff --git a/ex02/src/PmergeMe.hpp b/ex02/src/PmergeMe.hpp index 3e9626b..defd93a 100644 --- a/ex02/src/PmergeMe.hpp +++ b/ex02/src/PmergeMe.hpp @@ -56,7 +56,7 @@ void insert_sort(const std::pair* pairs, size_t len, T& array) array.push_back(pairs[i].first); if (pairs[i].first == pairs[i].second) continue; - ssize_t index = binary_search(array, 0, array.size() - 1, pairs[i].second); + size_t index = binary_search(array, 0, array.size() - 1, pairs[i].second); array.insert(array.begin() + index, pairs[i].second); } } diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 8003c8e..f7de1fd 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -1,24 +1,67 @@ #include "PmergeMe.hpp" +#include #include #include +#include +#include #include #include +template +void display(const T& array) +{ + for (size_t i = 0; i != array.size(); i++) + std::cout << array[i] << " "; +} + int main(int ac, char** av) { - std::vector input(ac - 1); - std::cout << "Before: "; - for (int i = 1; i != ac; i++) { - input[i - 1] = atoi(av[i]); - std::cout << av[i] << " "; + std::vector array(ac - 1); + + for (int i = 1; i != ac; i++) + { + for (int j = 0; av[i][j] != '\0'; j++) + { + if (!std::isdigit(av[i][j])) + { + std::cout << "Error" << std::endl; + return 1; + } + } + array[i - 1] = atoi(av[i]); + } + + std::cout << "Before: "; + display(array); + std::cout << std::endl; + + const clock_t time_vec_start = clock(); + + PmergeMe(array); + + const clock_t time_vec_stop = clock(); + + std::cout << "After: "; + display(array); + std::cout << std::endl; + 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::cout << std::endl; - PmergeMe(input); - std::cout << "After: "; - for (size_t i = 0; i != input.size(); i++) { - std::cout << input[i] << " "; + std::deque array(ac - 1); + for (int i = 1; i != ac; i++) + array[i - 1] = atoi(av[i]); + + std::cout << "Before: "; + display(array); + std::cout << std::endl; + + const clock_t time_deque_start = clock(); + + PmergeMe(array); + + const clock_t time_deque_stop = clock(); + + std::cout << "Time to process a range of " << array.size() <<" elements with std::vector : " << ((double) (time_deque_stop - time_deque_start) / CLOCKS_PER_SEC) * 1000000 << " us" << std::endl; } - std::cout << std::endl; } \ No newline at end of file