-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspan.cpp
More file actions
80 lines (63 loc) · 2.52 KB
/
Copy pathspan.cpp
File metadata and controls
80 lines (63 loc) · 2.52 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* span.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpoveda- <me@izenynn.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/20 01:35:56 by dpoveda- #+# #+# */
/* Updated: 2022/02/25 13:42:40 by dpoveda- ### ########.fr */
/* */
/* ************************************************************************** */
#include "span.hpp"
#include <algorithm>
Span::Span() {}
Span::Span(unsigned int n) : _size(n) {}
Span::Span(const Span& other) : _vector(other._vector), _size(other._size) {
*this = other;
}
Span::~Span() {}
Span& Span::operator=(const Span& other) {
if (this == &other) return *this;
this->_size = other._size;
this->_vector = other._vector;
return *this;
}
void Span::addNumber(int n) {
if (this->_vector.size() == this->_size) {
throw Span::SpanIsFullException();
}
this->_vector.push_back(n);
}
void Span::addRange(IT begin, IT end) {
if (this->_vector.size() + std::distance(begin, end) > this->_size) {
throw Span::SpanIsFullException();
}
this->_vector.insert(this->_vector.end(), begin, end);
}
unsigned int Span::shortestSpan() const {
if (this->_size < 2 || this->_vector.size() < 2) throw Span::SizeTooSmallException();
std::vector<int> copy(this->_vector);
std::sort(copy.begin(), copy.end());
int ret = *(copy.begin() + 1) - *copy.begin();
for (IT it = copy.begin(); it != copy.end() - 1; ++it) {
if(*(it + 1) - *it < ret) {
ret = *(it + 1) - *it;
}
}
return ret;
}
unsigned int Span::longestSpan() const {
if (this->_size < 2 || this->_vector.size() < 2) throw Span::SizeTooSmallException();
return *std::max_element(this->_vector.begin(), this->_vector.end())
- *std::min_element(this->_vector.begin(), this->_vector.end());
}
std::size_t Span::size() const {
return this->_size;
}
const char* Span::SpanIsFullException::what() const throw() {
return "Can't add another number, span is full";
}
const char* Span::SizeTooSmallException::what() const throw() {
return "Size of span is less or equal to 1, no span to find";
}