|
|
@@ -0,0 +1,157 @@
|
|
|
+/* ************************************************************************** */
|
|
|
+/* */
|
|
|
+/* ::: :::::::: */
|
|
|
+/* main.cpp :+: :+: :+: */
|
|
|
+/* +:+ +:+ +:+ */
|
|
|
+/* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
|
|
|
+/* +#+#+#+#+#+ +#+ */
|
|
|
+/* Created: 2026/01/13 17:54:18 by bchanot #+# #+# */
|
|
|
+/* Updated: 2026/01/25 04:38:31 by bchanot ### ########.fr */
|
|
|
+/* */
|
|
|
+/* ************************************************************************** */
|
|
|
+
|
|
|
+#include <vector>
|
|
|
+#include <deque>
|
|
|
+#include <cstring>
|
|
|
+#include <iostream>
|
|
|
+#include <stdint.h>
|
|
|
+#include <string>
|
|
|
+#include <chrono>
|
|
|
+
|
|
|
+struct Pairs {
|
|
|
+ int const max;
|
|
|
+ int const min;
|
|
|
+
|
|
|
+ Pairs(int const &a, int const &b) : max(a), min(b) {}
|
|
|
+};
|
|
|
+
|
|
|
+int parse_list(const char * str) {
|
|
|
+ std::string s(str);
|
|
|
+ size_t i;
|
|
|
+ int num;
|
|
|
+
|
|
|
+ if (s.empty())
|
|
|
+ throw std::invalid_argument("Empty string");
|
|
|
+
|
|
|
+ for (i = 0; i < s.size(); i++)
|
|
|
+ if (!std::isdigit(s[i]))
|
|
|
+ throw std::invalid_argument("Not a number");
|
|
|
+
|
|
|
+ num = atoi(str);
|
|
|
+
|
|
|
+ if (num < 0)
|
|
|
+ throw std::invalid_argument("Negative number");
|
|
|
+ return (num);
|
|
|
+}
|
|
|
+
|
|
|
+void ft_get_list(char **av, int ac, std::vector<int> &list) {
|
|
|
+ int i;
|
|
|
+
|
|
|
+ i = 0;
|
|
|
+ while (i < ac) {
|
|
|
+ list.emplace_back(parse_list(av[i]));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+template < typename U, typename T >
|
|
|
+U ft_merge_insert(U list) {
|
|
|
+
|
|
|
+ typedef typename U::iterator Uit;
|
|
|
+ typedef typename T::iterator Tit;
|
|
|
+
|
|
|
+ T pairs;
|
|
|
+ Tit jt;
|
|
|
+ U big;
|
|
|
+ U res;
|
|
|
+ Uit it;
|
|
|
+ int orphan;
|
|
|
+ bool hasOrphan;
|
|
|
+
|
|
|
+ for (it = list.begin(); it + 1 < list.end(); it += 2)
|
|
|
+ pairs.emplace_back(std::max(*it, *(it + 1)), std::min(*it, *(it + 1)));
|
|
|
+
|
|
|
+ hasOrphan = (list.size() % 2 != 0);
|
|
|
+ if (hasOrphan)
|
|
|
+ orphan = list.back();
|
|
|
+
|
|
|
+ if (pairs.size() > 1)
|
|
|
+ {
|
|
|
+ for (jt = pairs.begin(); jt != pairs.end(); jt++)
|
|
|
+ big.emplace_back((*jt).max);
|
|
|
+ res = ft_merge_insert<U, T>(big);
|
|
|
+
|
|
|
+ for (jt = pairs.begin(); jt != pairs.end(); jt++)
|
|
|
+ res.insert(std::lower_bound(res.begin(), res.end(), (*jt).min), (*jt).min);
|
|
|
+ }
|
|
|
+ else if (pairs.size() == 1) {
|
|
|
+ res.emplace_back(pairs[0].min);
|
|
|
+ res.emplace_back(pairs[0].max);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hasOrphan)
|
|
|
+ res.insert(std::lower_bound(res.begin(), res.end(), orphan), orphan);
|
|
|
+ return (res);
|
|
|
+}
|
|
|
+
|
|
|
+void ft_launch(std::vector<int> &list) {
|
|
|
+ std::chrono::duration<signed long, std::micro> duration;
|
|
|
+ std::chrono::_V2::system_clock::time_point start;
|
|
|
+ std::chrono::_V2::system_clock::time_point end;
|
|
|
+ std::vector<int> sorted_vec;
|
|
|
+ std::deque<int> sorted_deque;
|
|
|
+ std::deque<int> list_deque;
|
|
|
+ size_t i;
|
|
|
+
|
|
|
+
|
|
|
+ std::cout << "Before :\t" ;
|
|
|
+ for (i = 0; i < list.size() && i < 5; i++) {
|
|
|
+ std::cout << list[i] << " ";
|
|
|
+ }
|
|
|
+ if (i == 5)
|
|
|
+ std::cout << "[...]";
|
|
|
+ std::cout << std::endl;
|
|
|
+
|
|
|
+ start = std::chrono::high_resolution_clock::now();
|
|
|
+ sorted_vec = ft_merge_insert<std::vector<int>, std::vector<Pairs>>(list);
|
|
|
+ end = std::chrono::high_resolution_clock::now();
|
|
|
+
|
|
|
+ std::cout << "After :\t\t" ;
|
|
|
+ for (i = 0; i < sorted_vec.size() && i < 5; i++) {
|
|
|
+ std::cout << sorted_vec[i] << " ";
|
|
|
+ }
|
|
|
+ if (i == 5)
|
|
|
+ std::cout << "[...]";
|
|
|
+ std::cout << std::endl;
|
|
|
+
|
|
|
+ duration = std::chrono::duration_cast < std::chrono::microseconds > (end - start);
|
|
|
+ std::cout << "Time to process a range of " << list.size() << " elements with std::vector : " << duration.count() << " us" << std::endl;
|
|
|
+
|
|
|
+ for (i = 0; i < list.size(); i++) {
|
|
|
+ list_deque.emplace_back(list[i]);
|
|
|
+ }
|
|
|
+ start = std::chrono::high_resolution_clock::now();
|
|
|
+ sorted_deque = ft_merge_insert<std::deque<int>, std::deque<Pairs>>(list_deque);
|
|
|
+ end = std::chrono::high_resolution_clock::now();
|
|
|
+
|
|
|
+ duration = std::chrono::duration_cast < std::chrono::microseconds > (end - start);
|
|
|
+ std::cout << "Time to process a range of " << list.size() << " elements with std::deque : " << duration.count() << " us" << std::endl;
|
|
|
+}
|
|
|
+
|
|
|
+int main(int ac, char **av)
|
|
|
+{
|
|
|
+ std::vector<int> list;
|
|
|
+
|
|
|
+ if (ac < 2) {
|
|
|
+ std::cout << "Error : No number specified" << std::endl;
|
|
|
+ throw std::exception();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ft_get_list(++av, --ac, list);
|
|
|
+ ft_launch(list);
|
|
|
+ } catch (std::exception &e) {
|
|
|
+ std::cout << "Error : " << e.what() << std::endl;
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|