-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstrainedAdditiveCost.cpp
101 lines (92 loc) · 3.93 KB
/
ConstrainedAdditiveCost.cpp
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
//
// ConstrainedAdditiveCost.cpp
//
// Pathfinder, an optimal path finding program for graphs with
// multi-weighted edges under specified constraints.
//
// Copyright (c) 2013 Bradley Denby.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see {http://www.gnu.org/licenses/}.
//
#include "ConstrainedAdditiveCost.h"
/*
* The ConstrainedAdditiveCost constructor passes the cost and units
* arguments to the Cost(const double cost, const std::string& units)
* parent constructor and sets the __constraint field to constraint. It
* uses fast instiation for only one trip to memory.
*/
ConstrainedAdditiveCost::ConstrainedAdditiveCost(const double cost, const std::string& units, const double constraint) : Cost(cost, units), __constraint(constraint) {}
ConstrainedAdditiveCost::ConstrainedAdditiveCost(const ConstrainedAdditiveCost& cost) : Cost(cost.getCost(), cost.getUnits()), __constraint(cost.getConstraint()) {}
/*
* There is nothing to delete in the ConstrainedAdditiveCost class.
*/
ConstrainedAdditiveCost::~ConstrainedAdditiveCost() {}
/*
* Returns the double __constraint of the ConstrainedAdditiveCost
* object. Cannot modify the ConstrainedAdditiveCost object.
*/
double ConstrainedAdditiveCost::getConstraint() const {
return __constraint;
}
const Cost* ConstrainedAdditiveCost::clone() const {
if (this != INVALID_COST) {
return new ConstrainedAdditiveCost(*this);
}
else {
return this;
}
}
bool ConstrainedAdditiveCost::operator==(const ConstrainedAdditiveCost& cost) const {
return ((this->getCost() == cost.getCost())
&& (this->getUnits() == cost.getUnits())
&& (this->getConstraint() == cost.getConstraint()));
}
/*
* The overloaded operator * returns a new ConstrainedAdditiveCost
* object constructed according to the following rules:
* IF this __cost + argument __cost < __constraint
* new __cost = this __cost + argument __cost
* OTHERWISE
* return INVALID_COST
* This overloaded operator * assumes two things:
* this __units == argument __units
* this __constraint == argument __constraint
* No safety checks are carried out for performance reasons. As
* dictated by the trailing const, the overloaded operator * cannot
* modifiy the ConstrainedAdditiveCost object.
*/
const Cost* ConstrainedAdditiveCost::operator*(const Cost& cost) const {
const Cost* result = INVALID_COST;
double totalCost = this->getCost() + cost.getCost();
/* Below: not a conditional, just a boolean; takes advantage of
* short-circuiting in boolean expressions.
* Equivalent if/then expression:
* if(this != INVALID_COST && &cost != INVALID_COST && totalCost < __constraint) {
* product = new ConstrainedAdditiveCost(totalCost, this->getUnits(), __constraint);
* }
*/
(void)((this != INVALID_COST && &cost != INVALID_COST && totalCost < __constraint)
&& (result = new ConstrainedAdditiveCost(totalCost, this->getUnits(), __constraint)));
return result;
}
/*
* Returns a formatted string that represents the
* ConstrainedAdditiveCost object calling toString. Cannot modify the
* ConstrainedAdditiveCost object.
*/
std::string ConstrainedAdditiveCost::toString() const {
std::stringstream ss;
ss << std::fixed << std::showpoint << std::setprecision(4) << this->getCost() << " " << this->getUnits() << " of " << __constraint;
return ss.str();
}