forked from himanitawade/project-lawnmover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disks.hpp
209 lines (171 loc) · 4.85 KB
/
disks.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
///////////////////////////////////////////////////////////////////////////////
// disks.hpp
//
// Definitions for two algorithms that each solve the alternating disks
// problem.
//
// As provided, this header has four functions marked with TODO comments.
// You need to write in your own implementation of these functions.
//
///////////////////////////////////////////////////////////////////////////////
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <sstream>
#include <string>
#include <vector>
#include <functional>
#include <iostream>
enum disk_color { DISK_LIGHT, DISK_DARK};
class disk_state {
private:
std::vector<disk_color> _colors;
public:
disk_state(size_t light_count)
: _colors(light_count * 2, DISK_LIGHT) {
assert(light_count > 0);
for (size_t i = 1; i < _colors.size(); i += 2) {
_colors[i] = DISK_DARK;
}
}
bool operator== (const disk_state& rhs) const {
return std::equal(_colors.begin(), _colors.end(), rhs._colors.begin());
}
size_t total_count() const {
return _colors.size();
}
size_t light_count() const {
return total_count() / 2;
}
size_t dark_count() const {
return light_count();
}
bool is_index(size_t i) const {
return (i < total_count());
}
disk_color get(size_t index) const {
assert(is_index(index));
return _colors[index];
}
void swap(size_t left_index) {
assert(is_index(left_index));
auto right_index = left_index + 1;
assert(is_index(right_index));
std::swap(_colors[left_index], _colors[right_index]);
}
std::string to_string() const {
std::stringstream ss;
bool first = true;
for (auto color : _colors) {
if (!first) {
ss << " ";
}
if (color == DISK_LIGHT) {
ss << "L";
} else {
ss << "D";
}
first = false;
}
return ss.str();
}
// Return true when this disk_state is in alternating format. That means
// that the first disk at index 0 is light, the second disk at index 1
// is dark, and so on for the entire row of disks.
bool is_initialized() const {
for (size_t i = 0; i < total_count(); i++){ //check each position before function
if (i % 2 == 0){ //check even position --> should be light
if (_colors[i] == DISK_DARK) {
return false;
}
} else { //check odd position --> should be dark
if (_colors[i] == DISK_LIGHT) {
return false;
}
}
}
return true;
}
// Return true when this disk_state is fully sorted, with all light disks on
// the left (low indices) and all dark disks on the right (high indices).
bool is_sorted() const {
for (size_t i = 0; i < total_count()/2; i++){
if (_colors[i] == DISK_DARK){
return false;
}
}
return true;
}
};
// Data structure for the output of the alternating disks problem. That
// includes both the final disk_state, as well as a count of the number
// of swaps performed.
class sorted_disks {
private:
disk_state _after;
unsigned _swap_count;
public:
sorted_disks(const disk_state& after, unsigned swap_count)
: _after(after), _swap_count(swap_count) { }
sorted_disks(disk_state&& after, unsigned swap_count)
: _after(after), _swap_count(swap_count) { }
const disk_state& after() const {
return _after;
}
unsigned swap_count() const {
return _swap_count;
}
};
// Algorithm that sorts disks using the alternate algorithm.
sorted_disks sort_alternate(const disk_state& before) {
disk_state state = before;
size_t count = state.total_count();
int swaps = 0; //record # of step swap
for (size_t i = 0; i < count+1; i++){
size_t start_offset = 0;
size_t end_offset = 1;
if (i % 2 == 1) {
start_offset++;
end_offset++;
}
for (size_t j = start_offset; j < count-end_offset; j+=2){
if(state.get(j) == DISK_LIGHT) continue;
if(state.get(j+1)== DISK_LIGHT){
state.swap(j);
swaps++;
}
}
}
return sorted_disks(disk_state(state), swaps);
}
// Algorithm that sorts disks using the lawnmower algorithm.
sorted_disks sort_lawnmower(const disk_state& before) {
disk_state state = before;
size_t count = state.total_count();
int swaps = 0;
for (size_t i = 0; i < count/2; i++){
size_t idx = 0;
while (idx < count-1){
if(state.get(idx) == DISK_LIGHT){
}else{
if(state.get(idx+1)== DISK_LIGHT){
state.swap(idx);
swaps++;
}
}
idx++;
}
while (idx > 0){
if(state.get(idx) == DISK_DARK){
}else{
if(state.get(idx-1)== DISK_DARK){
state.swap(idx-1);
swaps++;
}
}
idx--;
}
}
return sorted_disks(disk_state(state), swaps);
}