Span.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* Span.cpp :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2026/01/13 18:18:53 by bchanot #+# #+# */
  9. /* Updated: 2026/01/16 14:56:24 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "Span.hpp"
  13. #include <climits>
  14. /*
  15. ** ------------------------------- CONSTRUCTOR --------------------------------
  16. */
  17. Span::Span( unsigned int const & size ) : _values(0), _max(size)
  18. {
  19. this->_values.reserve(size);
  20. }
  21. Span::Span( const Span & src ) : _values(0), _max(src._max)
  22. {
  23. std::vector<int>::const_iterator it;
  24. this->_values.reserve(src._max);
  25. for (it = std::begin(src._values); it < std::end(src._values); it++) {
  26. this->_values.emplace_back(*it);
  27. }
  28. }
  29. /*
  30. ** -------------------------------- DESTRUCTOR --------------------------------
  31. */
  32. Span::~Span()
  33. {
  34. }
  35. /*
  36. ** --------------------------------- OVERLOAD ---------------------------------
  37. */
  38. Span & Span::operator=( Span const & rhs )
  39. {
  40. (void)rhs;
  41. //if ( this != &rhs )
  42. //{
  43. //this->_value = rhs.getValue();
  44. //}
  45. return *this;
  46. }
  47. std::ostream & operator<<( std::ostream & o, Span const & i )
  48. {
  49. (void)i;
  50. //o << "Value = " << i.getValue();
  51. return o;
  52. }
  53. /*
  54. ** --------------------------------- METHODS ----------------------------------
  55. */
  56. void Span::addNumber(int const & add) {
  57. if (this->_values.size() >= this->_max)
  58. throw isFullException() ;
  59. this->_values.emplace_back(add);
  60. }
  61. void Span::fullFillSpan(void) {
  62. srand(time(0));
  63. for (size_t i = this->_values.size(); i < this->_max; i++) {
  64. this->addNumber(rand() % INT_MAX / 1000);
  65. }
  66. }
  67. void Span::fillSpan(size_t n) {
  68. srand(time(0));
  69. size_t j;
  70. j = 0;
  71. for (size_t i = this->_values.size(); j < n && i < this->_max; i++, j++) {
  72. this->addNumber(rand() % INT_MAX / 1000);
  73. }
  74. }
  75. void Span::rangeFillSpan(size_t start, size_t end) {
  76. srand(time(0));
  77. while (start < end) {
  78. try {
  79. this->addNumber(start);
  80. } catch (std::exception &e) {
  81. std::cout << e.what() << std::endl;
  82. break ;
  83. }
  84. start++;
  85. }
  86. }
  87. unsigned int Span::shortestSpan(void) const {
  88. std::vector<int> const check = this->_values;
  89. std::vector<int>::const_iterator iti;
  90. std::vector<int>::const_iterator itj;
  91. int sub;
  92. sub = INT_MAX;
  93. for (iti = std::begin(this->_values); iti < std::end(this->_values); iti++) {
  94. for (itj = std::begin(check); itj < std::end(check); itj++) {
  95. int sub_check = *iti - *itj;
  96. if (sub_check > 0 && sub_check < sub) {
  97. sub = sub_check;
  98. }
  99. }
  100. }
  101. return sub;
  102. }
  103. unsigned int Span::longestSpan(void) const {
  104. int max;
  105. int min;
  106. std::vector<int>::const_iterator it;
  107. it = std::begin(this->_values);
  108. min = max = *it;
  109. for (; it < std::end(this->_values); it++) {
  110. if (*it > max) {
  111. max = *it;
  112. } else if (*it < min) {
  113. min = *it;
  114. }
  115. }
  116. return (max - min);
  117. }
  118. /*
  119. ** --------------------------------- ACCESSOR ---------------------------------
  120. */
  121. unsigned int Span::getSize(void) const {
  122. return this->_values.size();
  123. }
  124. /* ************************************************************************** */