-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingCompetition.cpp
More file actions
182 lines (150 loc) · 4.83 KB
/
Copy pathSortingCompetition.cpp
File metadata and controls
182 lines (150 loc) · 4.83 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "SortingCompetition.h"
#include <iostream>
#include <iomanip>
#include <omp.h>
SortingCompetition::SortingCompetition(const string& inputFileName)
{
fin.open(inputFileName);
omp_set_num_threads(4);
}
void SortingCompetition::setFileName(const string& inputFileName) {
fin.close();
fin.open(inputFileName);
}
bool SortingCompetition::readData() {
while(!fin.eof()) {
char* word = new char[50];
fin >> word;
prePrepare.push_back(word);
}
return true;
}
bool SortingCompetition::prepareData() {
//length prefixed char* implementation
lenarray = new char*[prePrepare.size()];
for(int i = 0; i < prePrepare.size(); i++) {
lenarray[i] = new char[50];
for(int j = 0; j < 50; j++) {
lenarray[i][j] = 0;
}
lenarray[i][0] = strlen(prePrepare.at(i)); //set first char to length of string
for(int j = 0; j < strlen(prePrepare.at(i)) + 1; j++) {
lenarray[i][j + 1] = prePrepare.at(i)[j]; //copy rest of the data
}
}
return true;
}
void SortingCompetition::sortData() {
quickSortOmp(lenarray, prePrepare.size());
}
void SortingCompetition::outputData(const string& outputfinName) {
ofstream fout(outputfinName);
if(!fout.is_open()) {
cout << "ERROR OPENING OUTPUT FILE" << endl;
}
else {
for(int i = 0; i < prePrepare.size(); i++) { //special file out for length prefixed
for(int j = 1; j < lenarray[i][0] + 1; j++) {
fout << lenarray[i][j];
}
fout << endl;
}
}
}
/** Swap to value */
template <class NumType>
inline void Swap(NumType& value, NumType& other)
{
NumType temp = value;
value = other;
other = temp;
}
int SortingCompetition::QsPartition(char** arr, int l, int h)
{
char* x = arr[h]; // pivot
int i = (l - 1); // Index of smaller element
for (int j = l; j <= h- 1; j++)
{
// If current element is smaller than or equal to pivot
if (getpstrlen(arr[j]) <= getpstrlen(x))
{
if (getpstrlen(arr[j]) == getpstrlen(x) && pstrcmp(arr[j],x)<0)
{
i++; // increment index of smaller element
Swap(arr[i], arr[j]); // Swap current element with index
}
if (getpstrlen(arr[j]) < getpstrlen(x))
{
i++; // increment index of smaller element
Swap(arr[i], arr[j]); // Swap current element with index
}
}
}
Swap(arr[i + 1], arr[h]);
return (i + 1);
}
/* a sequential qs */
void SortingCompetition::QsSequential(char** array, int left, int right){
if(left < right){
int part = QsPartition(array, left, right);
QsSequential(array,left,part - 1);
QsSequential(array,part + 1,right);
}
}
/** A task dispatcher */
void SortingCompetition::QuickSortOmpTask(char** array, int left, int right, const int deep){
if(left < right){
int part = QsPartition(array, left, right);
if( deep ){
#pragma omp task
QuickSortOmpTask(array,part + 1,right, deep - 1);
#pragma omp task
QuickSortOmpTask(array,left,part - 1, deep - 1);
}
else {
QsSequential(array,part + 1,right);
QsSequential(array,left,part - 1);
}
}
}
/** The openmp quick sort */
void SortingCompetition::quickSortOmp(char** array, int size){
const int nbTasksRequired = (omp_get_max_threads() * 5);
int deep = 0;
while( (1 << deep) < nbTasksRequired ) deep += 1;
#pragma omp parallel
{
#pragma omp single nowait
{
QuickSortOmpTask(array, 0, size - 1 , deep);
}
}
}
int SortingCompetition::getpstrlen(char * word) {
return word[0]; //return length of string
}
int SortingCompetition::pstrcmp(char * first, char * second) {
int length = 0;
if (first[0] > second[0]) { //find which one has a longer length and use that
length = first[0];
}
else {
length = second[0];
}
for(int i = 1; i < length + 1; i++) {
if(first[i] < second[i]) { //compare char by char
return -1; //first argument has lower value
}
else if(second[i] < first[i]) {
return 1; //second argument has higher value
}
}
return 0; //equal strings
}
SortingCompetition::~SortingCompetition() {
for(int i = 0; i < prePrepare.size(); i++) {
delete [] prePrepare.at(i);
delete [] lenarray[i];
}
delete lenarray;
}