-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortingData.cpp
More file actions
150 lines (128 loc) · 3.6 KB
/
SortingData.cpp
File metadata and controls
150 lines (128 loc) · 3.6 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
/**
* SortingData.cpp
* @author: Kevin German
**/
#include "SortingData.h"
#include "sort.h"
#include <chrono>
#include <thread>
#include <mutex>
#include <exception>
extern bool sortingDisabled; //used for stopping sorting algorithms immediately (in case of a user event)
void SortingData::setDelay(const std::chrono::nanoseconds compareDelay,const std::chrono::nanoseconds assignmentDelay)
{
mCompareDelay = compareDelay;
mAssignmentDelay = assignmentDelay;
}
SortingData::SortingData(SortingData&& other) noexcept
: mAssignmentDelay(other.mAssignmentDelay),mCompareDelay(other.mCompareDelay), mRecentlyAssigned(true), mKey(other.mKey)
{
other.mRecentlyAssigned = false;
other.mKey = 0;
other.mRecentlyCompared = false;
}
SortingData::SortingData(const SortingData& other)
: mAssignmentDelay(other.mAssignmentDelay), mCompareDelay(other.mCompareDelay), mRecentlyAssigned(true), mKey(other.mKey)
{
std::lock(mMutex, other.mMutex);
std::lock_guard<std::mutex> lock(mMutex,std::adopt_lock);
std::lock_guard<std::mutex> lockOther(other.mMutex,std::adopt_lock);
std::this_thread::sleep_for(mAssignmentDelay); //delay to simulate heavy copy work
}
bool SortingData::operator<(const SortingData& other) const
{
{
std::lock(mMutex, other.mMutex);
std::lock_guard<std::mutex> lock(mMutex, std::adopt_lock);
std::lock_guard<std::mutex> lockOther(other.mMutex, std::adopt_lock);
if (mVerificationEnabled)
{
if (mKey < other.mKey)
{
mRecentlyCompared = false;
other.mRecentlyCompared = false;
}
else
{
mRecentlyCompared = true;
other.mRecentlyCompared = true;
}
}
else
{
mRecentlyCompared = true;
other.mRecentlyCompared = true;
}
}
std::this_thread::sleep_for(mCompareDelay);//delay to simulate heavy comparison work
return mKey < other.mKey;
}
int SortingData::operator&(const int other) const
{
std::lock_guard<std::mutex> lock(mMutex);
return mKey & other;
}
SortingData& SortingData::operator=(const SortingData &other)
{
if (sortingDisabled)
throw std::exception("Sort interrupted by user input. ");
if (this != &other)
{
{
std::lock(mMutex, other.mMutex);
std::lock_guard<std::mutex> lock(mMutex, std::adopt_lock);
std::lock_guard<std::mutex> lockOther(other.mMutex, std::adopt_lock);
mRecentlyAssigned = true;
mKey = other.mKey;
}
std::this_thread::sleep_for(mAssignmentDelay); //delay to simulate heavy copy work
}
return *this;
}
SortingData& SortingData::operator=(SortingData&& other)
{
if (sortingDisabled)
throw std::exception("Sort interrupted by user input. ");
{
std::lock_guard<std::mutex> lockThis(mMutex);
mKey = other.mKey;
mRecentlyAssigned = true;
}
return *this;
}
SortingData& SortingData::operator++()
{
std::lock_guard<std::mutex> lock(mMutex);
++mKey;
return *this;
}
bool SortingData::compared()
{
std::lock_guard<std::mutex> lock(mMutex);
const auto tmpCompared = mRecentlyCompared;
if (!mVerificationEnabled)
mRecentlyCompared = false;
return tmpCompared;
}
bool SortingData::assigned()
{
std::lock_guard<std::mutex> lock(mMutex);
const auto tmpAssigned = mRecentlyAssigned;
mRecentlyAssigned = false;
return tmpAssigned;
}
void SortingData::enableVerification(const bool enable)
{
std::lock_guard<std::mutex> lock(mMutex);
mRecentlyAssigned = false;
mRecentlyCompared = false;
if (enable)
{
mCompareDelay = mCompareDelay*timeForVerification;
}
mVerificationEnabled = enable;
}
bool SortingData::verificationEnabled() const
{
return mVerificationEnabled;
}