<repo_name>InigoRomero/CPP08<file_sep>/ex01/span.cpp
# include "Span.hpp"

Span::Span()
:
_length(0),
_numbers(0)
{}

Span::Span(unsigned int n)
:
_length(n),
_numbers(std::vector<int>())
{}

Span::Span(Span const &copy)
:
	_length(copy._length)
{
	this->_numbers.clear();
	this->_numbers = copy._numbers;
}

Span::~Span(){}

Span &Span::operator=(const Span& op)
{
    this->_length = op._length;
    this->_numbers = op._numbers;
    return (*this);
}

void Span::addNumber(int n)
{
    if (this->_numbers.size() < this->_length)
        this->_numbers.push_back(n);
    else
        throw VectorIsFull();
}

size_t Span::shortestSpan()
{
    size_t size = this->_numbers.size();
	if (size <= 1)
		throw Span::ShortSpanException();
    std::sort(this->_numbers.begin(), this->_numbers.end());
    size_t x = (size_t)this->_numbers[1] - (size_t)this->_numbers[0];
    for (size_t n = 0; n < size; n++)
        if (x > ((size_t)this->_numbers[n + 1] - (size_t)this->_numbers[n]))
            x = (size_t)this->_numbers[n + 1] - (size_t)this->_numbers[n];
    return (x);
}

long Span::longestSpan()
{
    long x;
    std::vector<int>::iterator min;
	std::vector<int>::iterator max;
    size_t size = this->_numbers.size();

	if (size <= 1)
		throw Span::LongSpanException();
	min = std::min_element(this->_numbers.begin(), this->_numbers.end());
	max = std::max_element(this->_numbers.begin(), this->_numbers.end());
	x = (long)(*min) - (long)(*max);
    x = x < 0 ? (x * -1) : x;
    return (x);
}


//exceptions
const char* Span::VectorIsFull::what() const throw()
{
	return "Exception: Vector is FULL";
}

const char* Span::ShortSpanException::what() const throw()
{
    return "Exception: ShortSpanException";
}

const char* Span::LongSpanException::what() const throw()
{
    return "Exception: LongSpanException";
}
<file_sep>/ex02/mutantstack.hpp
#ifndef MUTANTSTACK_HPP
# define MUTANTSTACK_HPP

# include <stack>
# include <list>
# include <algorithm>

template< typename T > 
class MutantStack : public std::stack<T>
{
    public:
        typedef typename std::stack<T>::container_type::iterator iterator;
        typedef typename std::stack<T>::container_type::const_iterator const_iterator;
        typedef typename std::stack<T>::container_type::reverse_iterator reverse_iterator;
        typedef typename std::stack<T>::container_type::const_reverse_iterator const_reverse_iterator;

        MutantStack() : std::stack<T>() {};
        MutantStack(const MutantStack& copy) : std::stack<T>(copy){};
        virtual ~MutantStack() {};

        MutantStack &operator=(const MutantStack &op)
        {
            if (this == &op)
                return (*this);
            std::stack<T>::operator=(op);
            return (*this);
        };
	
        iterator begin() { return (std::stack<T>::c.begin()); }
        const_iterator begin() const { return (std::stack<T>::c.begin()); }
        iterator end() { return (std::stack<T>::c.end()); }
        const_iterator end() const { return (std::stack<T>::c.end()); }
        reverse_iterator rbegin() { return (std::stack<T>::c.rbegin()); }
        const_reverse_iterator rbegin() const { return (std::stack<T>::c.rbegin()); }
        reverse_iterator rend() { return (std::stack<T>::c.rend()); }
        const_reverse_iterator rend() const { return (std::stack<T>::c.rend()); } 
};

# endif<file_sep>/ex01/Span.hpp
#ifndef SPAN_HPP
# define SPAN_HPP

# include <iostream>
# include <iterator>
# include <vector>
# include <algorithm>

class Span;

class Span
{
    private:
        Span();
        unsigned int _length;
        std::vector<int> _numbers;
    public:
        Span(unsigned int n);
        Span(const Span& copy);
        virtual ~Span();

        //exceptions
        class ShortSpanException: public std::exception {
            virtual const char* what() const throw();
        };
        class LongSpanException: public std::exception {
            virtual const char* what() const throw();
        };
        class VectorIsFull: public std::exception {
            virtual const char* what() const throw();
        };

        Span &operator=(Span const &op);
        void addNumber(int n);
        template < class Iterator >
        void        addNumber(Iterator begin, Iterator end)
        {
            if (this->_numbers.size() + std::distance(begin, end) > this->_length)
                throw(VectorIsFull());
            else
                std::copy(begin, end, std::back_inserter(this->_numbers));
            std::sort(this->_numbers.begin(), this->_numbers.end());
        }
        size_t shortestSpan();
        long longestSpan();
};

#endif<file_sep>/README.md
# CPP08
Templated containers, iterators, algorithms
<file_sep>/ex00/main.cpp
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include "easyfind.hpp"


int main(void)
{
    //vector
    std::vector<int> vect(10); 
    int value = 2; 
    std::fill(vect.begin(), vect.end(), value);
    std::vector<int>::iterator found = easyfind(vect, 2);
    if (found == vect.end())
        std::cout << "[+]Vector[+]   Eror 404 Dont found" << std::endl;
    else
        std::cout << "[+]Vector[+]   Yeah " << *found << " is inside me in " << found - vect.begin() << std::endl;
    if (easyfind(vect, 666) == vect.end())
        std::cout << "[+]Vector[+]   Eror 404 Dont found" << std::endl;

    //map
    std::map<int, int> map; 
    map.insert(std::pair<int,int>( 2, 30 )); 
    map.insert(std::pair<int,int>( 1, 42 )); 
    map.insert(std::pair<int,int>( 3, 42 )); 
    std::map<int, int>::iterator foundMap = easyfind(map, 42);
    if (foundMap == map.end())
        std::cout << "[+]Map[+]      Eror 404 Dont found" << std::endl;
    else
        std::cout << "[+]Map[+]      Yeah " << foundMap->second << " is inside me in " << foundMap->first << std::endl;
    if (easyfind(map, 666) == map.end())
        std::cout << "[+]Map[+]      Eror 404 Dont found" << std::endl;
	
    //multimap
    std::multimap<int, int> multimap; 
    multimap.insert(std::pair<int,int>( 2, 30 )); 
    multimap.insert(std::pair<int,int>( 33, 9 )); 
    std::multimap<int, int>::iterator foundMP = easyfind(multimap, 9);
    if (foundMP == multimap.end())
        std::cout << "[+]MultiMap[+] Eror 404 Dont found" << std::endl;
    else
        std::cout << "[+]MultiMap[+] Yeah " << foundMP->second  <<" is inside me in " << foundMP->first << std::endl;
    if (easyfind(multimap, 666) == multimap.end())
        std::cout << "[+]MultiMap[+] Eror 404 Dont found" << std::endl;

	return (0);
}<file_sep>/ex01/main.cpp
# include "Span.hpp"
#include <iostream>
#include <list>
#include <vector>
/*
int main()
{
    Span sp = Span(5);
    sp.addNumber(5);
    sp.addNumber(3);
    sp.addNumber(17);
    sp.addNumber(9);
    sp.addNumber(11);
    try{
        sp.addNumber(424);
    }
	catch(std::exception const &e)
	{
		std::cerr << "[+] " << e.what() << std::endl;
	}
	//corr
    std::cout << sp.shortestSpan() << std::endl;
    std::cout << sp.longestSpan() << std::endl;
}*/

int main()
{
    // FULL SPAN
    Span sp_full = Span(2);

    sp_full.addNumber(5);
    sp_full.addNumber(8);
    std::cerr << "[+] ADD when is full [+]"  << std::endl;
    try
    {
        sp_full.addNumber(9);     
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }
    
    Span sp_short = Span(5);
    sp_short.addNumber(5);
    sp_short.addNumber(90);
    sp_short.addNumber(17);
    sp_short.addNumber(-8925);
    sp_short.addNumber(11);
	std::cerr << "[+] Shortest diference [+]"  << std::endl;
    std::cout << sp_short.shortestSpan() << std::endl;

    Span sp_short_hard = Span(2);
    sp_short_hard.addNumber(2147483647);
    sp_short_hard.addNumber(-2147483648);
	std::cerr << "[+] Shortest biggest posible [+]"  << std::endl;
    std::cout << sp_short_hard.shortestSpan() << std::endl;

    Span sp_empty = Span(80);
    std::cerr << "[+] Empty exception 0 numbers[+]"  << std::endl;
    try
    {
        std::cout << sp_empty.shortestSpan() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }

    sp_empty.addNumber(5);
	std::cerr << "[+] Empty exception with one Number [+]"  << std::endl;
    try
    {
        std::cout << sp_empty.shortestSpan() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }

    Span sp_long = Span(4);
    sp_long.addNumber(8);
    sp_long.addNumber(-3);
    sp_long.addNumber(80);
    sp_long.addNumber(-8);
	std::cerr << "[+] Long Span [+]"  << std::endl;
    try
    {
        std::cout << sp_long.longestSpan() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }

    Span sp_long_hard = Span(4);
    sp_long_hard.addNumber(2147483647);
    sp_long_hard.addNumber(-2147483648);
	std::cerr << "[+] Long Span Long numbers [+]"  << std::endl;
    try
    {
        std::cout << sp_long_hard.longestSpan() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }
	std::cerr << "[+] empty Error Long [+]"  << std::endl;
    try
    {
        std::cout << sp_empty.longestSpan() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }
	std::cerr << "[+] 10mil numbers plus [+]"  << std::endl;
    Span sp_long_long = Span(100000);
    std::vector<int> range(100000, 10);
    range[6666] = 40;
    sp_long_long.addNumber(range.begin(), range.end());

    try
    {
        std::cout << sp_long_long.longestSpan() << std::endl;
    }
        catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }

    try
    {
        std::cout << sp_long_long.shortestSpan() << std::endl;
    }
    catch(const std::exception& e)
    {
        std::cerr << "Error : " << e.what() <<  std::endl;
    }

	return (0);
}<file_sep>/ex00/easyfind.hpp
#ifndef EASYFIND_HPP
# define EASYFIND_HPP

# include <exception>
# include <cctype>
# include <map>
// easy find T 
template<typename T>
typename T::iterator easyfind(T &ints, int n)
{
	return (std::find(ints.begin(), ints.end(), n));
}

//overload 
template<typename key_type, typename value_type>
typename std::map<key_type, value_type>::iterator easyfind(std::map<key_type, value_type> &ints, int n)
{
	typename std::map<key_type, value_type>::iterator it = ints.begin();
	for (; it != ints.end(); ++it)
		if (it->second == n)
			return (it);
	return (ints.end());
}

template<typename key_type, typename value_type>
typename std::multimap<key_type, value_type>::iterator easyfind(std::multimap<key_type, value_type> &ints, int n)
{
	typename std::multimap<key_type, value_type>::iterator it = ints.begin();
	for (; it != ints.end(); ++it)
		if (it->second == n)
			return (it);
	return (ints.end());
}

#endif