Skip to content

Commit 3aa80c6

Browse files
committed
Replacing built-in array with std::array
1 parent d466042 commit 3aa80c6

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

.vscode/settings.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
"xtr1common": "cpp",
8787
"xtree": "cpp",
8888
"xutility": "cpp",
89-
"climits": "cpp"
89+
"climits": "cpp",
90+
"ranges": "cpp",
91+
"span": "cpp",
92+
"stacktrace": "cpp",
93+
"regex": "cpp"
9094
}
91-
}
95+
}

greedy_algorithms/job_sequencing_algorithm.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
// Implementation of the greedy algorithm using priority_queue AKA max_heap.
6868
// C++ code for the above approach
6969
#include <algorithm> // for sort function.
70+
#include <array> // for std::array
7071
#include <cassert> // for assert funtion in testing
7172
#include <iostream> // for standard in/out
7273
#include <queue> // for priority queue data structure
@@ -89,7 +90,7 @@ struct Job {
8990

9091
/**
9192
* @brief Utility function that finds Custom sorting helper
92-
* struct which is used for sorting all jobs according to profit
93+
* which is used for sorting all jobs according to profit
9394
*
9495
* @param a first job struct
9596
* @param b second job struct
@@ -106,15 +107,17 @@ bool jobComparator(Job const &a, Job const &b) { return (a.profit < b.profit); }
106107
* that has the maximum profit from jobs
107108
*
108109
*/
109-
std::vector<char> getJobScheduling(Job arr[], int n) {
110+
template <std::size_t N>
111+
std::vector<char> getJobScheduling(std::array<Job, N> &arr) {
110112
std::vector<Job> result;
111-
std::sort(arr, arr + n, [](Job a, Job b) { return a.dead < b.dead; });
113+
std::sort(arr.begin(), arr.end(),
114+
[](Job a, Job b) { return a.dead < b.dead; });
112115

113116
// set a custom priority queue
114117
std::priority_queue<Job, std::vector<Job>, decltype(&jobComparator)> pq(
115118
jobComparator);
116119

117-
for (int i = n - 1; i >= 0; i--) {
120+
for (int i = N - 1; i >= 0; i--) {
118121
int slot_available;
119122

120123
// we count the slots available between two jobs
@@ -160,27 +163,29 @@ std::vector<char> getJobScheduling(Job arr[], int n) {
160163
* @returns void
161164
*/
162165
void tests() {
163-
greedy_algorithms::Job jobs1[] = {
164-
{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}};
165-
int n = sizeof(jobs1) / sizeof(jobs1[0]);
166+
std::array<greedy_algorithms::Job, 5> jobs1 = {{{'a', 2, 100},
167+
{'b', 1, 19},
168+
{'c', 2, 27},
169+
{'d', 1, 25},
170+
{'e', 3, 15}}};
166171

167172
// 1st test
168-
assert(greedy_algorithms::getJobScheduling(jobs1, n)[0] == 'a');
169-
assert(greedy_algorithms::getJobScheduling(jobs1, n)[1] == 'c');
170-
assert(greedy_algorithms::getJobScheduling(jobs1, n)[2] == 'e');
173+
std::vector<char> result1 = greedy_algorithms::getJobScheduling(jobs1);
174+
assert(result1[0] == 'a');
175+
assert(result1[1] == 'c');
176+
assert(result1[2] == 'e');
171177

172-
greedy_algorithms::Job jobs2[] = {
173-
{'x', 1, 50}, {'y', 2, 60}, {'z', 2, 20}, {'w', 3, 30}};
174-
n = sizeof(jobs2) / sizeof(jobs2[0]);
178+
std::array<greedy_algorithms::Job, 4> jobs2 = {
179+
{{'x', 1, 50}, {'y', 2, 60}, {'z', 2, 20}, {'w', 3, 30}}};
175180

176181
// 2nd test
177-
assert(greedy_algorithms::getJobScheduling(jobs2, n)[0] == 'x');
178-
assert(greedy_algorithms::getJobScheduling(jobs2, n)[1] == 'y');
179-
assert(greedy_algorithms::getJobScheduling(jobs2, n)[2] == 'w');
182+
std::vector<char> result2 = greedy_algorithms::getJobScheduling(jobs2);
183+
assert(result2[0] == 'x');
184+
assert(result2[1] == 'y');
185+
assert(result2[2] == 'w');
180186

181187
std::cout << "All tests have successfully passed!\n";
182188
}
183-
184189
/**
185190
* @brief Main function
186191
* @returns 0 on exit

0 commit comments

Comments
 (0)