bastien преди 1 седмица
родител
ревизия
85664eab01
променени са 5 файла, в които са добавени 207 реда и са изтрити 0 реда
  1. BIN
      Module_09/ex01/RPN
  2. 50 0
      Module_09/ex02/Makefile
  3. BIN
      Module_09/ex02/RPN
  4. BIN
      Module_09/ex02/sort
  5. 157 0
      Module_09/ex02/srcs/main.cpp

BIN
Module_09/ex01/RPN


+ 50 - 0
Module_09/ex02/Makefile

@@ -0,0 +1,50 @@
+# **************************************************************************** #
+#                                                                              #
+#                                                         :::      ::::::::    #
+#    Makefile                                           :+:      :+:    :+:    #
+#                                                     +:+ +:+         +:+      #
+#    By: bchanot <bchanot@42.fr>                    +#+  +:+       +#+         #
+#                                                 +#+#+#+#+#+   +#+            #
+#    Created: 2016/07/24 00:00:08 by bchanot           #+#    #+#              #
+#    Updated: 2026/01/23 17:45:19 by bchanot          ###   ########.fr        #
+#                                                                              #
+# **************************************************************************** #
+
+NAME = sort
+MAKE_LIBS = make --no-print-directory
+SRCS_DIR = srcs/
+OBJS_DIR = .objects/
+CC = g++ -Wall -Wextra -Werror -g
+INC = -I./includes
+FILES = main
+SRCS = $(FILES)
+OBJS = $(addprefix $(OBJS_DIR), $(addsuffix .o, $(SRCS)))
+RED = \033[1;31m
+BLUE = \033[1;34m
+CYAN = \033[0;36m
+GREEN = \033[1;32m
+YELLOW = \033[1;33m
+EOC = \033[0m
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+	@echo -e "$(GREEN)Objects created.$(EOC)"
+	@$(CC) $(OBJS) $(INC) -o $(NAME)
+	@echo -e "$(GREEN)Compilation complete.$(EOC)"
+
+$(OBJS_DIR)%.o: $(SRCS_DIR)%.cpp
+	@mkdir -p $(dir $@)
+	$(CC) -c $< $(INC) -o $@
+
+clean:
+	@echo -e "$(RED)Deleting objects.$(EOC)"
+	@/bin/rm -rf $(OBJS_DIR)
+
+fclean: clean
+	@echo -e "$(RED)Deleting binary.$(EOC)"
+	@/bin/rm -rf $(NAME)
+
+re: fclean all
+
+.PHONY: all clean fclean re

BIN
Module_09/ex02/RPN


BIN
Module_09/ex02/sort


+ 157 - 0
Module_09/ex02/srcs/main.cpp

@@ -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;
+}