Repository navigation
Expand file tree
/
Copy pathutils.h
More file actions
83 lines (67 loc) · 2.27 KB
/
Copy pathutils.h
File metadata and controls
83 lines (67 loc) · 2.27 KB
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
#include <vector>
#include <string>
using namespace std;
/**********************************************************************/
struct pieceOfWork {
double start;
double end;
pieceOfWork(double i, double j) : start(i), end(j) {}
pieceOfWork() { start = 0; end = 0; }
~pieceOfWork() {}
};
/******************************************************************************/
// Template class - returns subset vector containing elements X to Y
template <typename T>
vector<T> slicing(vector<T> const& v, size_t X, size_t Y) {
if (v.size() == 0) { return v; }
// Begin and End iterator
auto first = v.begin() + X;
auto last = v.begin() + Y;
// Copy the element
vector<T> vector(first, last);
// Return the results
return vector;
}
/******************************************************************************/
template<typename T>
string toString(const T&x) {
stringstream output;
output << x;
return output.str();
}
/******************************************************************************/
template<typename T>
void printVector(const vector<T>& x) {
string result = "";
char delim = ',';
if (x.size() == 0) { return; }
result = toString(x[0]);
for (int i = 1; i < x.size(); i++) {
result += delim + toString(x[i]);
}
cout << result << endl;
}
/******************************************************************************/
class Utils {
public:
vector<pieceOfWork> divideWork(double numItems, int numProcessors) {
// divide work between processors
vector<pieceOfWork> work;
if (numItems < numProcessors) { numProcessors = numItems; }
size_t startIndex = 0;
for (size_t remainingProcessors = numProcessors; remainingProcessors > 0;
remainingProcessors--) {
//case for last processor
size_t numToProcess = numItems;
if (remainingProcessors != 1) {
numToProcess = ceil(numItems / remainingProcessors);
}
work.push_back(pieceOfWork(startIndex, (startIndex+numToProcess)));
startIndex += numToProcess;
numItems -= numToProcess;
}
return work;
}
private:
};
/******************************************************************************/