1
0

」 feat: finished ex02

This commit is contained in:
2025-06-04 14:05:48 +02:00
parent 6b00e95da9
commit 60e3d482e4
6 changed files with 270 additions and 0 deletions

60
ex02/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/06/04 13:35:49 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = PmergeMe
CC = clang++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
ifeq ($(VERBOSE),true)
FLAGS += -D VERBOSE
endif
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

BIN
ex02/PmergeMe Executable file

Binary file not shown.

140
ex02/PmergeMe.cpp Normal file
View File

@ -0,0 +1,140 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PmergeMe.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/04 13:28:23 by adjoly #+# #+# */
/* Updated: 2025/06/04 14:05:14 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <PmergeMe.hpp>
#include <algorithm>
#include <cstddef>
#include <ctime>
#include <set>
#include <sstream>
std::vector<int> parseInput(int argc, char **argv) {
std::set<int> seen;
std::vector<int> result;
for (int i = 1; i < argc; ++i) {
std::istringstream iss(argv[i]);
int value;
if (!(iss >> value) || value < 0) {
throw std::runtime_error("Invalid input: " + std::string(argv[i]));
}
if (!seen.insert(value).second) {
throw std::runtime_error("Duplicate value: " +
std::string(argv[i]));
}
result.push_back(value);
}
return result;
}
template <typename Container> void printContainer(const Container &container) {
for (auto it = range(container)) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
// Measure time and run sorting for vector and deque
void sortAndTime(std::vector<int> &vec, std::deque<int> &deq) {
auto vecStart = std::clock();
_fordJohnsonSort(vec);
auto vecEnd = std::clock();
auto deqStart = std::clock();
_fordJohnsonSort(deq);
auto deqEnd = std::clock();
std::cout << "After: ";
printContainer(vec);
double vecDuration = 1000000.0 * (vecEnd - vecStart) / CLOCKS_PER_SEC;
double deqDuration = 1000000.0 * (deqEnd - deqStart) / CLOCKS_PER_SEC;
std::cout << "Time to process a range of " << vec.size()
<< " elements with std::vector : " << vecDuration << " us"
<< std::endl;
std::cout << "Time to process a range of " << deq.size()
<< " elements with std::deque : " << deqDuration << " us"
<< std::endl;
}
std::vector<size_t> _generateJacobsthalOrder(size_t n) {
std::vector<size_t> sequence;
std::vector<bool> inserted(n, false);
// Generate the sequence
size_t j0 = 0, j1 = 1;
while (j1 < n) {
sequence.push_back(j1);
inserted[j1] = true;
size_t next = j1 + 2 * j0;
j0 = j1;
j1 = next;
}
for (size_t i = 0; i < n; ++i) {
if (!inserted[i])
sequence.push_back(i);
}
return sequence;
}
template <typename Container> void _fordJohnsonSort(Container &container) {
if (container.size() <= 1)
return;
Container mainChain;
Container pendingElements;
for (auto it = container.begin(); it != container.end();) {
int first = *it;
++it;
if (it != container.end()) {
int second = *it;
if (first > second) {
mainChain.push_back(first);
pendingElements.push_back(second);
} else {
mainChain.push_back(second);
pendingElements.push_back(first);
}
++it;
} else {
mainChain.push_back(first);
}
}
_fordJohnsonSort(mainChain);
std::vector<size_t> order = _generateJacobsthalOrder(pendingElements.size());
for (size_t i = 0; i < order.size(); ++i) {
_binaryInsert(mainChain, pendingElements[order[i]]);
}
container = mainChain;
}
template <typename Container> void _binaryInsert(Container &sorted, int value) {
typename Container::iterator itL = sorted.begin();
typename Container::iterator itH = sorted.end();
while (itL < itH) {
typename Container::iterator itMid = itL + (itH - itL) / 2;
if (*itMid < value)
itL = itMid + 1;
else
itH = itMid;
}
sorted.insert(itL, value);
}

36
ex02/PmergeMe.hpp Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PmergeMe.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/04 13:27:20 by adjoly #+# #+# */
/* Updated: 2025/06/04 13:45:07 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <deque>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#define auto __auto_type
#define range(x) \
x.begin(); \
it != x.end(); \
it++
std::vector<int> parseInput(int argc, char **argv);
template <typename Container> void printContainer(const Container &container);
void sortAndTime(std::vector<int> &vec, std::deque<int> &deq);
template <typename Container> void _fordJohnsonSort(Container &container);
template <typename Container> void _binaryInsert(Container &sorted, int value);

BIN
ex02/RPN Executable file

Binary file not shown.

34
ex02/main.cpp Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/04 13:26:50 by adjoly #+# #+# */
/* Updated: 2025/06/04 13:44:29 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <PmergeMe.hpp>
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Error: No input provided" << std::endl;
return 1;
}
try {
std::vector<int> inputVector = parseInput(argc, argv);
std::deque<int> inputDeque(inputVector.begin(), inputVector.end());
std::cout << "Before: ";
printContainer(inputVector);
sortAndTime(inputVector, inputDeque);
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}