initial
This commit is contained in:
commit
d0a1b09d9a
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.cache
|
||||||
|
*.d
|
||||||
|
*.o
|
||||||
|
PmergeMe
|
||||||
45
Makefile
Normal file
45
Makefile
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# Makefile :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# Created: 2025/03/21 14:50:00 by whaffman #+# #+# #
|
||||||
|
# Updated: 2025/03/21 14:58:40 by whaffman ######## odam.nl #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
EX = $(shell find . -maxdepth 1 -type d -name 'ex*' -exec basename {} \;)
|
||||||
|
|
||||||
|
all:
|
||||||
|
for ex in $(EX); do \
|
||||||
|
$(MAKE) -C $$ex; \
|
||||||
|
done
|
||||||
|
|
||||||
|
clean:
|
||||||
|
for ex in $(EX); do \
|
||||||
|
$(MAKE) -C $$ex clean; \
|
||||||
|
done
|
||||||
|
|
||||||
|
fclean:
|
||||||
|
for ex in $(EX); do \
|
||||||
|
$(MAKE) -C $$ex fclean; \
|
||||||
|
done
|
||||||
|
|
||||||
|
re:
|
||||||
|
for ex in $(EX); do \
|
||||||
|
$(MAKE) -C $$ex re; \
|
||||||
|
done
|
||||||
|
|
||||||
|
run:
|
||||||
|
@for ex in $(EX); do \
|
||||||
|
echo "Running tests in $$ex:"; \
|
||||||
|
$(MAKE) -sC $$ex fclean; \
|
||||||
|
$(MAKE) -sC $$ex run; \
|
||||||
|
$(MAKE) -sC $$ex fclean; \
|
||||||
|
echo "Finished tests in $$ex"; \
|
||||||
|
echo ""; \
|
||||||
|
done
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re run
|
||||||
41
common.mk
Normal file
41
common.mk
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# :::::::: #
|
||||||
|
# common.mk :+: :+: #
|
||||||
|
# +:+ #
|
||||||
|
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
||||||
|
# +#+ #
|
||||||
|
# Created: 2025/03/21 15:00:16 by whaffman #+# #+# #
|
||||||
|
# Updated: 2025/03/21 15:04:44 by whaffman ######## odam.nl #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
INC = -I./inc
|
||||||
|
VPATH = src
|
||||||
|
SRC = $(notdir $(wildcard src/*.cpp))
|
||||||
|
OBJ = $(SRC:.cpp=.o)
|
||||||
|
CC = c++
|
||||||
|
CFLAGS = -Wall -Wextra -Werror -std=c++23 -MMD
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
-include $(OBJ:.o=.d)
|
||||||
|
|
||||||
|
run: all
|
||||||
|
./$(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CC) $(CFLAGS) $(INC) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJ)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re run
|
||||||
2
ex02/Makefile
Normal file
2
ex02/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
NAME = PmergeMe
|
||||||
|
include ../common.mk
|
||||||
36
ex02/compile_commands.json
Normal file
36
ex02/compile_commands.json
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"arguments": [
|
||||||
|
"/usr/bin/c++",
|
||||||
|
"-Wall",
|
||||||
|
"-Wextra",
|
||||||
|
"-Werror",
|
||||||
|
"-std=c++23",
|
||||||
|
"-I./inc",
|
||||||
|
"-c",
|
||||||
|
"-o",
|
||||||
|
"Jacobstahl.o",
|
||||||
|
"src/Jacobstahl.cpp"
|
||||||
|
],
|
||||||
|
"directory": "/home/willem/projects/CPP09/ex02",
|
||||||
|
"file": "/home/willem/projects/CPP09/ex02/src/Jacobstahl.cpp",
|
||||||
|
"output": "/home/willem/projects/CPP09/ex02/Jacobstahl.o"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"arguments": [
|
||||||
|
"/usr/bin/c++",
|
||||||
|
"-Wall",
|
||||||
|
"-Wextra",
|
||||||
|
"-Werror",
|
||||||
|
"-std=c++23",
|
||||||
|
"-I./inc",
|
||||||
|
"-c",
|
||||||
|
"-o",
|
||||||
|
"main.o",
|
||||||
|
"src/main.cpp"
|
||||||
|
],
|
||||||
|
"directory": "/home/willem/projects/CPP09/ex02",
|
||||||
|
"file": "/home/willem/projects/CPP09/ex02/src/main.cpp",
|
||||||
|
"output": "/home/willem/projects/CPP09/ex02/main.o"
|
||||||
|
}
|
||||||
|
]
|
||||||
20
ex02/inc/Jacobstahl.hpp
Normal file
20
ex02/inc/Jacobstahl.hpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Jacobstahl
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Jacobstahl();
|
||||||
|
Jacobstahl(const Jacobstahl &other);
|
||||||
|
Jacobstahl &operator=(const Jacobstahl &other);
|
||||||
|
~Jacobstahl();
|
||||||
|
|
||||||
|
int get(int n);
|
||||||
|
std::vector<int> getUpTo(int n);
|
||||||
|
int getAfter(int n);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int getRecursive(int n);
|
||||||
|
std::vector<int> _jacobstahl_numbers;
|
||||||
|
};
|
||||||
29
ex02/inc/PmergeMe.hpp
Normal file
29
ex02/inc/PmergeMe.hpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
|
template <typename Container>
|
||||||
|
class PmergeMe
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PmergeMe() = delete;
|
||||||
|
PmergeMe(const Container &container);
|
||||||
|
PmergeMe(const PmergeMe &other);
|
||||||
|
~PmergeMe();
|
||||||
|
|
||||||
|
PmergeMe &operator=(const PmergeMe &other);
|
||||||
|
|
||||||
|
void sort();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<int> _data_vector;
|
||||||
|
std::deque<int> _data_deque;
|
||||||
|
std::vector<int> _jacobstahl_numbers;
|
||||||
|
|
||||||
|
int getJacobstahlNumber(int n);
|
||||||
|
int getJacobstahlNumberRecursive(int n);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
79
ex02/src/Jacobstahl.cpp
Normal file
79
ex02/src/Jacobstahl.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "Jacobstahl.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
Jacobstahl::Jacobstahl() : _jacobstahl_numbers()
|
||||||
|
{
|
||||||
|
_jacobstahl_numbers.resize(2, -1);
|
||||||
|
_jacobstahl_numbers[0] = 0;
|
||||||
|
_jacobstahl_numbers[1] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Jacobstahl::Jacobstahl(const Jacobstahl &other) : _jacobstahl_numbers(other._jacobstahl_numbers) {}
|
||||||
|
|
||||||
|
Jacobstahl &Jacobstahl::operator=(const Jacobstahl &other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
_jacobstahl_numbers = other._jacobstahl_numbers;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Jacobstahl::~Jacobstahl() {}
|
||||||
|
|
||||||
|
int Jacobstahl::get(int n)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
throw std::invalid_argument("n must be non-negative");
|
||||||
|
|
||||||
|
if (n >= static_cast<int>(_jacobstahl_numbers.size()))
|
||||||
|
_jacobstahl_numbers.resize(n + 1, -1);
|
||||||
|
|
||||||
|
return getRecursive(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Jacobstahl::getRecursive(int n)
|
||||||
|
{
|
||||||
|
if (n == 0)
|
||||||
|
return 0;
|
||||||
|
if (n == 1)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (_jacobstahl_numbers.size() > static_cast<size_t>(n) && _jacobstahl_numbers[n] != -1)
|
||||||
|
return _jacobstahl_numbers[n];
|
||||||
|
|
||||||
|
_jacobstahl_numbers[n] = getRecursive(n - 1) + 2 * getRecursive(n - 2);
|
||||||
|
return _jacobstahl_numbers[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> Jacobstahl::getUpTo(int n)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
throw std::invalid_argument("n must be non-negative");
|
||||||
|
|
||||||
|
std::vector<int> result;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int number = get(result.size());
|
||||||
|
if (number > n)
|
||||||
|
break;
|
||||||
|
result.push_back(number);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Jacobstahl::getAfter(int n)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
throw std::invalid_argument("n must be non-negative");
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int number = get(index);
|
||||||
|
if (number > n)
|
||||||
|
return number;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
0
ex02/src/PmergeMe.cpp
Normal file
0
ex02/src/PmergeMe.cpp
Normal file
27
ex02/src/main.cpp
Normal file
27
ex02/src/main.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "Jacobstahl.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Jacobstahl jacobstahl;
|
||||||
|
|
||||||
|
std::cout << "First 10 Jacobstahl numbers:" << std::endl;
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; ++i)
|
||||||
|
{
|
||||||
|
std::cout << "Jacobstahl(" << i << ") = " << jacobstahl.get(i) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<int> jacobstahl_numbers = jacobstahl.getUpTo(100);
|
||||||
|
std::cout << "Jacobstahl numbers up to 100:" << std::endl;
|
||||||
|
for (int num : jacobstahl_numbers)
|
||||||
|
{
|
||||||
|
std::cout << num << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::cout << "Jacobstahl number after 100: " << jacobstahl.getAfter(100) << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user