/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Span.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bchanot +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/13 18:18:53 by bchanot #+# #+# */ /* Updated: 2026/01/14 13:32:20 by bchanot ### ########.fr */ /* */ /* ************************************************************************** */ #include "Span.hpp" #include /* ** ------------------------------- CONSTRUCTOR -------------------------------- */ Span::Span( unsigned int const & size ) : _value(NULL), _max(size), _size(0) { this->_value = new int[size]; } Span::Span( const Span & src ) : _value(NULL), _max(src._max), _size(src._size) { this->_value = new int[src._max]; for (size_t i = 0; i < this->_max; i++) { this->_value[i] = src._value[i]; } } /* ** -------------------------------- DESTRUCTOR -------------------------------- */ Span::~Span() { delete [] this->_value; } /* ** --------------------------------- OVERLOAD --------------------------------- */ Span & Span::operator=( Span const & rhs ) { (void)rhs; //if ( this != &rhs ) //{ //this->_value = rhs.getValue(); //} return *this; } std::ostream & operator<<( std::ostream & o, Span const & i ) { (void)i; //o << "Value = " << i.getValue(); return o; } /* ** --------------------------------- METHODS ---------------------------------- */ void Span::addNumber(int const & add) { if (this->_size >= this->_max) throw isFullException() ; this->_value[this->_size] = add; this->_size++; } void Span::fullFillSpan(void) { srand(time(0)); size_t size; size = this->_size; for (size_t i = size; i < this->_max; i++) { this->addNumber(rand() % INT_MAX / 1000); // std::cout << this->_value[i] << std::endl; } } void Span::fillSpan(size_t n) { srand(time(0)); size_t size; size_t j; j = 0; size = this->_size; for (size_t i = size; j < n && i < this->_max; i++, j++) { this->addNumber(rand() % INT_MAX / 1000); // std::cout << this->_value[i] << std::endl; } } void Span::rangeFillSpan(size_t start, size_t end) { srand(time(0)); size_t size; size_t j; while (start < end) { this->addNumber(start); start++; // std::cout << this->_value[i] << std::endl; } } unsigned int Span::shortestSpan(void) const { int *check; int sub; check = this->_value; sub = INT_MAX; for (size_t i = 0; i < this->_size; i++) { for (size_t j = 0; j < this->_size; j++) { int sub_check = this->_value[i] - check[j]; if (sub_check > 0 && sub_check < sub) { sub = sub_check; } } } return sub; } unsigned int Span::longestSpan(void) const { int max; int min; min = max = this->_value[0]; for (size_t i = 1; i < this->_size; i++) { if (this->_value[i] > max) { max = this->_value[i]; } else if (this->_value[i] < min) { min = this->_value[i]; } } return (max - min); } /* ** --------------------------------- ACCESSOR --------------------------------- */ unsigned int Span::getSize(void) const { return this->_size; } /* ************************************************************************** */