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/14 13:32:20 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "Span.hpp"
  13. #include <climits>
  14. /*
  15. ** ------------------------------- CONSTRUCTOR --------------------------------
  16. */
  17. Span::Span( unsigned int const & size ) : _value(NULL), _max(size), _size(0)
  18. {
  19. this->_value = new int[size];
  20. }
  21. Span::Span( const Span & src ) : _value(NULL), _max(src._max), _size(src._size)
  22. {
  23. this->_value = new int[src._max];
  24. for (size_t i = 0; i < this->_max; i++) {
  25. this->_value[i] = src._value[i];
  26. }
  27. }
  28. /*
  29. ** -------------------------------- DESTRUCTOR --------------------------------
  30. */
  31. Span::~Span()
  32. {
  33. delete [] this->_value;
  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->_size >= this->_max)
  58. throw isFullException() ;
  59. this->_value[this->_size] = add;
  60. this->_size++;
  61. }
  62. void Span::fullFillSpan(void) {
  63. srand(time(0));
  64. size_t size;
  65. size = this->_size;
  66. for (size_t i = size; i < this->_max; i++) {
  67. this->addNumber(rand() % INT_MAX / 1000);
  68. // std::cout << this->_value[i] << std::endl;
  69. }
  70. }
  71. void Span::fillSpan(size_t n) {
  72. srand(time(0));
  73. size_t size;
  74. size_t j;
  75. j = 0;
  76. size = this->_size;
  77. for (size_t i = size; j < n && i < this->_max; i++, j++) {
  78. this->addNumber(rand() % INT_MAX / 1000);
  79. // std::cout << this->_value[i] << std::endl;
  80. }
  81. }
  82. void Span::rangeFillSpan(size_t start, size_t end) {
  83. srand(time(0));
  84. size_t size;
  85. size_t j;
  86. while (start < end) {
  87. this->addNumber(start);
  88. start++;
  89. // std::cout << this->_value[i] << std::endl;
  90. }
  91. }
  92. unsigned int Span::shortestSpan(void) const {
  93. int *check;
  94. int sub;
  95. check = this->_value;
  96. sub = INT_MAX;
  97. for (size_t i = 0; i < this->_size; i++) {
  98. for (size_t j = 0; j < this->_size; j++) {
  99. int sub_check = this->_value[i] - check[j];
  100. if (sub_check > 0 && sub_check < sub) {
  101. sub = sub_check;
  102. }
  103. }
  104. }
  105. return sub;
  106. }
  107. unsigned int Span::longestSpan(void) const {
  108. int max;
  109. int min;
  110. min = max = this->_value[0];
  111. for (size_t i = 1; i < this->_size; i++) {
  112. if (this->_value[i] > max) {
  113. max = this->_value[i];
  114. } else if (this->_value[i] < min) {
  115. min = this->_value[i];
  116. }
  117. }
  118. return (max - min);
  119. }
  120. /*
  121. ** --------------------------------- ACCESSOR ---------------------------------
  122. */
  123. unsigned int Span::getSize(void) const {
  124. return this->_size;
  125. }
  126. /* ************************************************************************** */