Skip to content

Commit d466042

Browse files
committed
Extracting the compare function outside a struct.
1 parent 68dde6c commit d466042

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

greedy_algorithms/job_sequencing_algorithm.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ struct Job {
8787
int profit; // Profit earned if job is completed before deadline
8888
}; // namespace greedy_algorithmsstruct Job
8989

90-
// Custom sorting helper struct which is used for sorting
91-
// all jobs according to profit
92-
9390
/**
9491
* @brief Utility function that finds Custom sorting helper
9592
* struct which is used for sorting all jobs according to profit
@@ -98,11 +95,7 @@ struct Job {
9895
* @param b second job struct
9996
* @returns true if the proft of a less than profit of b, else false.
10097
*/
101-
struct jobProfit {
102-
bool operator()(Job const& a, Job const& b) {
103-
return (a.profit < b.profit);
104-
}
105-
};
98+
bool jobComparator(Job const &a, Job const &b) { return (a.profit < b.profit); }
10699

107100
/**
108101
* @brief function that get the maximum profit from jobs
@@ -118,7 +111,8 @@ std::vector<char> getJobScheduling(Job arr[], int n) {
118111
std::sort(arr, arr + n, [](Job a, Job b) { return a.dead < b.dead; });
119112

120113
// set a custom priority queue
121-
std::priority_queue<Job, std::vector<Job>, jobProfit> pq;
114+
std::priority_queue<Job, std::vector<Job>, decltype(&jobComparator)> pq(
115+
jobComparator);
122116

123117
for (int i = n - 1; i >= 0; i--) {
124118
int slot_available;
@@ -194,4 +188,4 @@ void tests() {
194188
int main() {
195189
tests();
196190
return 0;
197-
}
191+
}

0 commit comments

Comments
 (0)