-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdge.cpp
More file actions
31 lines (26 loc) · 911 Bytes
/
Edge.cpp
File metadata and controls
31 lines (26 loc) · 911 Bytes
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
#include <limits>
#include "Edge.h"
Edge::Edge(int v, int w, double weight)
: _v{v},
_w{w},
_weight{weight}
{
if (v < 0) { throw std::out_of_range{"Vertex name must be a nonnegative integer"}; }
if (w < 0) { throw std::out_of_range{"Vertex name must be a nonnegative integer"}; }
if (std::isnan(weight)) { throw std::invalid_argument{"Weight is NaN"}; }
}
Edge::Edge() : _v{-1}, _w{-1}, _weight{std::numeric_limits<double>::infinity()} {}
int Edge::other(int vertex)
{
if (vertex == _v) { return _w; }
else if (vertex == _w) { return _v; }
else { throw std::invalid_argument{"Illegal endpoint"}; }
}
bool Edge::operator<(const Edge& rhs) const noexcept
{
if (_weight < rhs._weight) { return true; }
if (rhs._weight < _weight) { return false; }
if (_v < rhs._v) { return true; }
if (rhs._v < _v) { return false; }
return _w < rhs._w;
}