-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpan.cpp
More file actions
105 lines (90 loc) · 2.37 KB
/
Span.cpp
File metadata and controls
105 lines (90 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Span.hpp"
#include "Defines.hpp"
#include <algorithm>
#include <iterator>
#include <numeric>
Span::Span () : _maxSize (0) {}
Span::Span (unsigned int maxSize) : _maxSize (maxSize)
{
this->_vector.reserve (maxSize);
}
void
Span::populate (std::vector<int>::iterator begin,
std::vector<int>::iterator end)
{
unsigned int distance = std::distance (begin, end);
if (distance > (_maxSize - this->_vector.size ()))
throw Span::rangeTooHighException ();
this->_vector.insert (this->_vector.end (), begin, end);
}
void
Span::addNumber (int n)
{
if (this->_vector.size () == this->_maxSize)
throw Span::isFullException ();
this->_vector.push_back (n);
}
unsigned int
Span::shortestSpan ()
{
if (this->_vector.size () < 2)
throw Span::notEnoughNumbersException ();
std::sort (this->_vector.begin (), this->_vector.end ());
std::vector<int> diference (this->_vector.size ());
std::adjacent_difference (this->_vector.begin (), this->_vector.end (),
diference.begin ());
return *std::min_element (diference.begin () + 1, diference.end ());
}
unsigned int
Span::longestSpan ()
{
if (this->_vector.size () < 2)
throw Span::notEnoughNumbersException ();
unsigned int min, max;
min = *std::min_element (this->_vector.begin (), this->_vector.end ());
max = *std::max_element (this->_vector.begin (), this->_vector.end ());
return max - min;
}
Span::Span (const Span &other)
: _maxSize (other._maxSize), _vector (other._vector)
{
}
std::ostream &
Span::operator<< (std::ostream &os)
{
std::vector<int>::iterator it = this->_vector.begin ();
os << YELLOW "Vector data: " RESET;
for (; it != this->_vector.end (); it++)
{
os << "[" << *it << "]";
}
std::cout << std::endl;
return os;
}
Span &
Span::operator= (const Span &other)
{
if (this != &other)
{
unsigned int &maxSize = const_cast<unsigned int &> (this->_maxSize);
this->_vector = other._vector;
maxSize = other._maxSize;
}
return *this;
}
Span::~Span () {}
const char *
Span::isFullException::what () const throw ()
{
return RED "The container is full" RESET;
}
const char *
Span::rangeTooHighException::what () const throw ()
{
return RED "Range too high to fit in vector's size" RESET;
}
const char *
Span::notEnoughNumbersException::what () const throw ()
{
return RED "Not enough numbers in Span" RESET;
}