d07 ex02 a peu pres ok

This commit is contained in:
hugogogo
2022-03-11 01:54:16 +01:00
parent a38d7184e3
commit 9197577889
10 changed files with 379 additions and 1 deletions

View File

@@ -0,0 +1,50 @@
#ifndef ARRAY_HPP
# define ARRAY_HPP
#include <iostream>
template < typename T >
class Array {
public:
Array(unsigned int n = 0u)
: _size(n)
, _arr(new T[n]) {};
~Array() { delete [] _arr; };
Array(Array const & src) : _arr(new T[0]) { *this = src; };
Array &operator=(Array const & rhs) {
if (this == &rhs)
return (*this);
delete[] _arr;
_arr = new T[rhs._size];
_size = rhs._size;
for (unsigned int it = 0; it < rhs._size && it < _size; it++)
_arr[it] = rhs._arr[it];
return (*this);
};
// seen on luke's code :
// https://isocpp.org/wiki/faq/const-correctness#const-overloading
T & operator[](size_t i) {
if (i < 0 || i >= _size)
throw ArrayOutOfRangeException();
return (_arr[i]);
};
const T & operator[](size_t i) const {
if (i < 0 || i >= _size)
throw ArrayOutOfRangeException();
return (_arr[i]);
};
size_t size() const { return (_size); };
private:
class ArrayOutOfRangeException : public std::exception {
const char *what() const throw() {
return ("out of range array index");};
};
size_t _size;
T * _arr;
};
#endif

15
d07/ex02/headers/Fill.hpp Normal file
View File

@@ -0,0 +1,15 @@
#ifndef FILL_HPP
# define FILL_HPP
# include <iostream>
# include <string>
# include "Iter.hpp"
template< typename T, typename U >
void Fill(T & arr, size_t len, U const & e) {
for (size_t i = 0; i < len; i++)
arr[i] = e;
}
#endif

11
d07/ex02/headers/Iter.hpp Normal file
View File

@@ -0,0 +1,11 @@
#ifndef ITER_HPP
# define ITER_HPP
template< typename T, typename F >
void Iter(T & arr, size_t len, F & f)
{
for (size_t i = 0; i < len; i++)
f(arr[i]);
}
#endif

View File

@@ -0,0 +1,12 @@
#ifndef PRINT_HPP
# define PRINT_HPP
# include <iostream>
# include <string>
template< typename T >
void Print(T const & e) {
std::cout << "[" << e << "]";
}
#endif

25
d07/ex02/headers/colors.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef COLORS_H
# define COLORS_H
# define GRAY "\e[0;30m"
# define RED "\e[0;31m"
# define GREEN "\e[0;32m"
# define YELLOW "\e[0;33m"
# define BLUE "\e[0;34m"
# define PURPLE "\e[0;35m"
# define CYAN "\e[0;36m"
# define WHITE "\e[0;37m"
# define B_GRAY "\e[1;30m"
# define B_RED "\e[1;31m"
# define B_GREEN "\e[1;32m"
# define B_YELLOW "\e[1;33m"
# define B_BLUE "\e[1;34m"
# define B_PURPLE "\e[1;35m"
# define B_CYAN "\e[1;36m"
# define B_WHITE "\e[1;37m"
# define RESET "\e[0m"
#endif