feat(09): unsing template to have the same function for each containers
This commit is contained in:
parent
3eff988cc4
commit
dd8c8debde
1 changed files with 56 additions and 0 deletions
|
|
@ -34,6 +34,62 @@
|
||||||
#define CLR_GOLD "\033[38;5;220m"
|
#define CLR_GOLD "\033[38;5;220m"
|
||||||
#define CLR_GREY "\033[38;5;240m"
|
#define CLR_GREY "\033[38;5;240m"
|
||||||
|
|
||||||
|
template <typename t>
|
||||||
|
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 <typename t>
|
||||||
|
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 <typename t>
|
||||||
|
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 <typename t>
|
||||||
|
t convert(int argc, char **argv) {
|
||||||
|
t container;
|
||||||
|
for (size_t i = 1 ; i < static_cast<size_t>(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
|
#ifndef DEBUG
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue