| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* Span.cpp :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2026/01/13 18:18:53 by bchanot #+# #+# */
- /* Updated: 2026/01/16 14:56:24 by bchanot ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "Span.hpp"
- #include <climits>
- /*
- ** ------------------------------- CONSTRUCTOR --------------------------------
- */
- Span::Span( unsigned int const & size ) : _values(0), _max(size)
- {
- this->_values.reserve(size);
- }
- Span::Span( const Span & src ) : _values(0), _max(src._max)
- {
- std::vector<int>::const_iterator it;
-
- this->_values.reserve(src._max);
- for (it = std::begin(src._values); it < std::end(src._values); it++) {
- this->_values.emplace_back(*it);
- }
- }
- /*
- ** -------------------------------- DESTRUCTOR --------------------------------
- */
- Span::~Span()
- {
- }
- /*
- ** --------------------------------- 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->_values.size() >= this->_max)
- throw isFullException() ;
- this->_values.emplace_back(add);
- }
- void Span::fullFillSpan(void) {
- srand(time(0));
- for (size_t i = this->_values.size(); i < this->_max; i++) {
- this->addNumber(rand() % INT_MAX / 1000);
- }
- }
- void Span::fillSpan(size_t n) {
- srand(time(0));
- size_t j;
- j = 0;
- for (size_t i = this->_values.size(); j < n && i < this->_max; i++, j++) {
- this->addNumber(rand() % INT_MAX / 1000);
- }
- }
- void Span::rangeFillSpan(size_t start, size_t end) {
- srand(time(0));
-
- while (start < end) {
- try {
- this->addNumber(start);
- } catch (std::exception &e) {
- std::cout << e.what() << std::endl;
- break ;
- }
- start++;
- }
- }
- unsigned int Span::shortestSpan(void) const {
- std::vector<int> const check = this->_values;
- std::vector<int>::const_iterator iti;
- std::vector<int>::const_iterator itj;
- int sub;
- sub = INT_MAX;
- for (iti = std::begin(this->_values); iti < std::end(this->_values); iti++) {
- for (itj = std::begin(check); itj < std::end(check); itj++) {
- int sub_check = *iti - *itj;
- if (sub_check > 0 && sub_check < sub) {
- sub = sub_check;
- }
- }
- }
- return sub;
- }
- unsigned int Span::longestSpan(void) const {
- int max;
- int min;
- std::vector<int>::const_iterator it;
- it = std::begin(this->_values);
- min = max = *it;
- for (; it < std::end(this->_values); it++) {
- if (*it > max) {
- max = *it;
- } else if (*it < min) {
- min = *it;
- }
- }
- return (max - min);
- }
- /*
- ** --------------------------------- ACCESSOR ---------------------------------
- */
- unsigned int Span::getSize(void) const {
- return this->_values.size();
- }
- /* ************************************************************************** */
|