diff --git a/cpp09/ex02/PmergeMe.hpp b/cpp09/ex02/PmergeMe.hpp index 98cfdd3..100a276 100644 --- a/cpp09/ex02/PmergeMe.hpp +++ b/cpp09/ex02/PmergeMe.hpp @@ -34,6 +34,62 @@ #define CLR_GOLD "\033[38;5;220m" #define CLR_GREY "\033[38;5;240m" +template +typename t::iterator binaryInsert(t& container, int value) { + typename t::iterator it = std::lower_bound(container.begin(), container.end(), value); + container.insert(it, value); + return it; +} + +template +void print_container(t& container, bool sorted) { + std::cout << std::endl << CLR_YELLOW << (sorted ? "Before: " : "After: "); + for (size_t i = 0; i < container.size(); ++i) { + std::cout << CLR_GOLD << container[i] << " "; + } + std::cout << CLR_RESET << std::endl; +} + +template +void FordJohnson(t &container) { + if (container.size() < 2) + return; + t main; + t tmp; + + for (size_t i = 0; i + 1 < container.size(); i += 2) { + int a = container[i]; + int b = container[i + 1]; + if (a > b) + std::swap(a, b); + main.push_back(b); + tmp.push_back(a); + } + if (container.size() % 2 != 0) { + tmp.push_back(container.back()); + } + std::sort(main.begin(), main.end()); + for (size_t i = 0; i < tmp.size(); ++i) { + binaryInsert(main, tmp[i]); + } + container = main; +} + +template +t convert(int argc, char **argv) { + t container; + for (size_t i = 1 ; i < static_cast(argc); i++) { + errno = 0; + int value = std::strtol(argv[i], 0, 10); + if (errno == ERANGE || value < 0) { + std::cerr << CLR_RED << "Error: I don't accept number over INT_MAX" << CLR_RESET << std::endl; + std::exit(1); + } + container.push_back(value); + } + return container; +} + #ifndef DEBUG #define DEBUG 0 #endif