Skip to content

Commit dbe8752

Browse files
authored
Simplify code by using standard functions (#544)
1 parent 3074e9c commit dbe8752

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

include/CXXGraph/Partitioning/CoordinatedPartitionState.hpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#pragma once
2424

25+
#include <algorithm>
2526
#include <memory>
2627
#include <mutex>
2728
#include <set>
@@ -136,10 +137,7 @@ void CoordinatedPartitionState<T>::incrementMachineLoad(
136137
const int m, shared<const Edge<T>> e) {
137138
std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
138139
machines_load_edges[m] = machines_load_edges[m] + 1;
139-
int new_value = machines_load_edges.at(m);
140-
if (new_value > MAX_LOAD) {
141-
MAX_LOAD = new_value;
142-
}
140+
MAX_LOAD = std::max(MAX_LOAD, machines_load_edges.at(m));
143141
partition_map[m]->addEdge(e);
144142
}
145143
template <typename T>
@@ -161,14 +159,8 @@ void CoordinatedPartitionState<T>::incrementMachineWeight(
161159
template <typename T>
162160
int CoordinatedPartitionState<T>::getMinLoad() const {
163161
std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
164-
int MIN_LOAD = std::numeric_limits<int>::max();
165-
for (const auto &machines_load_edges_it : machines_load_edges) {
166-
int loadi = machines_load_edges_it;
167-
if (loadi < MIN_LOAD) {
168-
MIN_LOAD = loadi;
169-
}
170-
}
171-
return MIN_LOAD;
162+
return *std::min_element(machines_load_edges.begin(),
163+
machines_load_edges.end());
172164
}
173165
template <typename T>
174166
int CoordinatedPartitionState<T>::getMaxLoad() const {
@@ -177,18 +169,9 @@ int CoordinatedPartitionState<T>::getMaxLoad() const {
177169
template <typename T>
178170
int CoordinatedPartitionState<T>::getMachineWithMinWeight() const {
179171
std::lock_guard<std::mutex> lock(*machines_weight_edges_mutex);
180-
181-
double MIN_LOAD = std::numeric_limits<double>::max();
182-
int machine_id = 0;
183-
for (size_t i = 0; i < machines_weight_edges.size(); ++i) {
184-
double loadi = machines_weight_edges[i];
185-
if (loadi < MIN_LOAD) {
186-
MIN_LOAD = loadi;
187-
machine_id = i;
188-
}
189-
}
190-
191-
return machine_id;
172+
return std::distance(machines_weight_edges.begin(),
173+
std::min_element(machines_weight_edges.begin(),
174+
machines_weight_edges.end()));
192175
}
193176
template <typename T>
194177
int CoordinatedPartitionState<T>::getMachineWithMinWeight(

include/CXXGraph/Partitioning/HDRF.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,8 @@ void HDRF<T>::performStep(shared<const Edge<T>> e,
129129
fv = 1 + (1 - fv);
130130
}
131131
int load = state->getMachineLoad(m);
132-
double bal = (MAX_LOAD - load);
133-
bal /= (epsilon + MAX_LOAD - MIN_LOAD);
134-
if (bal < 0) {
135-
bal = 0;
136-
}
132+
const double bal =
133+
std::max(0.0, (MAX_LOAD - load) / (epsilon + MAX_LOAD - MIN_LOAD));
137134
double SCORE_m = fu + fv + lambda * bal;
138135
if (SCORE_m < 0) {
139136
std::cout << "ERRORE: SCORE_m<0" << std::endl;

0 commit comments

Comments
 (0)