first push stack maybe down

This commit is contained in:
kinou-p 2022-10-19 17:31:20 +02:00
commit 5f0dfe405e
9 changed files with 414 additions and 0 deletions

39
Makefile Normal file
View File

@ -0,0 +1,39 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/10/12 19:45:18 by apommier #+# #+# #
# Updated: 2022/10/19 14:25:28 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = a.out
SRCS = tests/stack_test.cpp\
tests/main.cpp
OBJS = ${SRCS:.cpp=.o}
CC = c++
CFLAGS = -g -Wall -Wextra -Werror -std=c++98
RM = rm -rf
.cpp.o:
$(CC) ${CFLAGS} -c $< -o $(<:.cpp=.o)
${NAME}: ${OBJS}
${CC} ${LIB} ${OBJS} -o ${NAME}
all: ${NAME}
clean:
@${RM} ${OBJS}
fclean: clean
@${RM} ${NAME}
re: fclean all
.PHONY: all clean fclean re

16
containers/map.hpp Normal file
View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:25 by apommier #+# #+# */
/* Updated: 2022/10/18 10:17:57 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAP_HPP
# define MAP_HPP
#endif

16
containers/set.hpp Normal file
View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:29 by apommier #+# #+# */
/* Updated: 2022/10/18 10:18:12 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SET_HPP
# define SET_HPP
#endif

81
containers/stack.hpp Normal file
View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:30 by apommier #+# #+# */
/* Updated: 2022/10/19 17:22:33 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STACK_HPP
# define STACK_HPP
# include <vector>
namespace ft
{
template <class T, class Container = std::vector<T> >
class stack
{
public:
//---------MEMBER TYPE---------
typedef std::size_t size_type;
typedef T value_type;
typedef Container container_type;
private:
container_type c;
public:
//---------COPLIEN FORM FUNCTION---------
explicit stack (const container_type& ctnr = container_type()) : c(ctnr){}//default constructor
stack<T, Container>(const stack &other )//copy constructor
{
*this = other;
}
~stack<T, Container>(void) {}//destructor
stack &operator=( const stack &other )//assigment operator
{
if (this != &other)
this->c = other.c;
return *this;
}
//---------MEMBER FUNCTION----------
bool empty() const
{
return (c.empty());
}
size_type size() const
{
return (c.size());
}
value_type& top()
{
return (c.back());
}
const value_type& top() const
{
return (c.back());
}
void push (const value_type& val)
{
return (c.push_back());
}
void pop()
{
return (c.pop_back());
}
};
}
#endif

51
containers/vector.hpp Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:32 by apommier #+# #+# */
/* Updated: 2022/10/19 17:30:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VECTOR_HPP
# define VECTOR_HPP
namespace ft
{
template< class T, class Allocator = std::allocator<T> > class vector;
class vector
{
public:
//---------MEMBER TYPE---------
typedef value_type;
typedef allocator_type;
typedef reference;
typedef const_reference;
typedef pointer;
typedef const_pointer;
typedef iterator;
typedef const_iterator;
typedef reverse_iterator;
typedef const_reverse_iterator;
typedef difference_type;
typedef size_type;
//---------COPLIEN FORM FUNCTION---------
explicit vector (const allocator_type& alloc = allocator_type());//default constructor
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());//fill constructor
template <class InputIterator>
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());//range constructor
vector (const vector& x);//copy constructor
~vector();
vector& operator= (const vector& x);//assignation operator
//---------MEMBER FUNCTION----------
private:
};
}
#endif

25
ft_containers.hpp Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_containers.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:33 by apommier #+# #+# */
/* Updated: 2022/10/19 17:22:39 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_CONTAINERS_HPP
# define FT_CONTAINERS_HPP
# include <iostream>
//namespace ft {
# include "./containers/map.hpp"
# include "./containers/set.hpp"
# include "./containers/stack.hpp"
# include "./containers/vector.hpp"
//}
#endif

128
main.cpp Normal file
View File

@ -0,0 +1,128 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/12 19:46:35 by apommier #+# #+# */
/* Updated: 2022/10/12 19:46:56 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <string>
#include <deque>
#if 1 //CREATE A REAL STL EXAMPLE
#include <map>
#include <stack>
#include <vector>
namespace ft = std;
#else
#include <map.hpp>
#include <stack.hpp>
#include <vector.hpp>
#endif
#include <stdlib.h>
#define MAX_RAM 4294967296
#define BUFFER_SIZE 4096
struct Buffer
{
int idx;
char buff[BUFFER_SIZE];
};
#define COUNT (MAX_RAM / (int)sizeof(Buffer))
template<typename T>
class MutantStack : public ft::stack<T>
{
public:
MutantStack() {}
MutantStack(const MutantStack<T>& src) { *this = src; }
MutantStack<T>& operator=(const MutantStack<T>& rhs)
{
this->c = rhs.c;
return *this;
}
~MutantStack() {}
typedef typename ft::stack<T>::container_type::iterator iterator;
iterator begin() { return this->c.begin(); }
iterator end() { return this->c.end(); }
};
int main(int argc, char** argv) {
if (argc != 2)
{
std::cerr << "Usage: ./test seed" << std::endl;
std::cerr << "Provide a seed please" << std::endl;
std::cerr << "Count value:" << COUNT << std::endl;
return 1;
}
const int seed = atoi(argv[1]);
srand(seed);
ft::vector<std::string> vector_str;
ft::vector<int> vector_int;
ft::stack<int> stack_int;
ft::vector<Buffer> vector_buffer;
ft::stack<Buffer, std::deque<Buffer> > stack_deq_buffer;
ft::map<int, int> map_int;
for (int i = 0; i < COUNT; i++)
{
vector_buffer.push_back(Buffer());
}
for (int i = 0; i < COUNT; i++)
{
const int idx = rand() % COUNT;
vector_buffer[idx].idx = 5;
}
ft::vector<Buffer>().swap(vector_buffer);
try
{
for (int i = 0; i < COUNT; i++)
{
const int idx = rand() % COUNT;
vector_buffer.at(idx);
std::cerr << "Error: THIS VECTOR SHOULD BE EMPTY!!" <<std::endl;
}
}
catch(const std::exception& e)
{
//NORMAL ! :P
}
for (int i = 0; i < COUNT; ++i)
{
map_int.insert(ft::make_pair(rand(), rand()));
}
int sum = 0;
for (int i = 0; i < 10000; i++)
{
int access = rand();
sum += map_int[access];
}
std::cout << "should be constant with the same seed: " << sum << std::endl;
{
ft::map<int, int> copy = map_int;
}
MutantStack<char> iterable_stack;
for (char letter = 'a'; letter <= 'z'; letter++)
iterable_stack.push(letter);
for (MutantStack<char>::iterator it = iterable_stack.begin(); it != iterable_stack.end(); it++)
{
std::cout << *it;
}
std::cout << std::endl;
return (0);
}

41
tests/main.cpp Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/19 14:04:37 by apommier #+# #+# */
/* Updated: 2022/10/19 14:38:42 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_containers.hpp"
void stack_tester()
{
ft::stack<int> first;
std::cout << "------empty------\n";
std::cout << first.empty() << std::endl;
std::cout << "------size------\n";
std::cout << first.size() << std::endl;
}
void vector_tester()
{
}
int main()
{
std::cout << "------TESTER START-------\n";
std::cout << "------STACK-------\n";
stack_tester();
std::cout << "------VECTOR-------\n";
vector_tester();
// std::cout << "------MAP-------\n";
// map_tester();
// std::cout << "------SET-------\n";
// set_tester();
std::cout << "------End-------\n";
return (0);
}

17
tests/stack_test.cpp Normal file
View File

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_test.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/19 14:04:13 by apommier #+# #+# */
/* Updated: 2022/10/19 14:29:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
// void tester()
// {
// //ft::stack first()
// return;
// }