-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtm2.cpp
More file actions
369 lines (302 loc) · 7.37 KB
/
tm2.cpp
File metadata and controls
369 lines (302 loc) · 7.37 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <iostream>
using std::cout;
#include <deque>
#include <vector>
#include <utility>
#include <assert.h>
typedef unsigned int data_t;
typedef std::deque<data_t> tape_t;
unsigned int n;
////////////////////////////////////////////////////////////////////////////////
// Prototypes
////////////////////////////////////////////////////////////////////////////////
bool is_full(tape_t *t);
bool is_end(tape_t *t1);
int read(tape_t *t1, tape_t *t2, data_t *d);
void write(data_t data, tape_t *t1, tape_t *t2);
void rewind(tape_t *tape);
bool is_sorted (tape_t *t1, tape_t *t2);
void sort_3(data_t *a, data_t *b, data_t *c);
void print(tape_t *t1, tape_t *t2);
void print_all(data_t x,
data_t y,
data_t z,
tape_t *s1,
tape_t *s2,
tape_t *d1,
tape_t *d2);
void sort(tape_t *t1, tape_t *t2, tape_t *t3, tape_t *t4);
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
/*!
Might have to jiggle if 'n' is odd.
Returns true if tape is full.
is_full() and is_end() together form the proper IsEnd() of the
problem. It's just clearer to me for the simulated tape to have
two routines.
*/
bool
is_full(tape_t *t)
{
return t->size() == n / 2;
}
/*!
You're at the end of the tape if:
a) Read all data off tape
b) Written tape until it's full
I stuck the 'b' sense in is_full().
*/
bool
is_end(tape_t *t)
{
return t->empty();
}
/*!
For my simulated tape a read is destructive: for a deque it's
annoying to write over previous values. The ultimate behavior is
the same as you'd get with the tape in the problem.
*/
int
read(tape_t *t1, tape_t *t2, data_t *d)
{
int got_data = 0;
if (!is_end(t1))
{
*d = t1->front();
t1->pop_front();
got_data = 1;
} else if (!is_end(t2))
{
*d = t2->front();
t2->pop_front();
got_data = 1;
}
return got_data;
}
/*!
Put value 'data' on one of t1 or t2: treat them as a single,
longer tape of size 'n'. Having both tapes full makes us 'splode.
*/
void
write(tape_t *t1, tape_t *t2, data_t data)
{
assert((is_full(t1) && is_full(t2)) || "both tapes full");
if (!is_full(t1))
t1->push_back(data);
else
t2->push_back(data);
}
/*!
There's nothing to do here, since I'm only modeling the tape.
This is just so you can see where a rewind would be placed.
The goober code is so that I don't get a warning about an unused
variable.
Uhm, I consider rewind()s to be O(c).
*/
void
rewind(tape_t *tape)
{
if (!tape)
cout << "Poopers\n";
}
/*!
This is O(n).
*/
bool
is_sorted (tape_t *t1, tape_t *t2)
{
rewind(t1);
rewind(t2);
std::vector<data_t> v;
v.insert(v.end(), t1->begin(), t1->end());
v.insert(v.end(), t2->begin(), t2->end());
data_t monotonic = v[0];
unsigned int size = v.size();
for (unsigned i = 1; i < size; ++i)
{
if (v[i] >= monotonic) // not strictly monotonic: can have multiple
monotonic = v[i];
else
return false;
}
return true;
}
/*!
Puts data pointed to by 'a', 'b', 'c' in sorted order: smallest in
'a', etc.
*/
void
sort_3(data_t *a, data_t *b, data_t *c)
{
data_t x = *a;
data_t y = *b;
data_t z = *c;
if (z < y)
std::swap(y,z);
if (y < x)
std::swap(y,x);
if (z < y)
std::swap(z, y);
*a = x;
*b = y;
*c = z;
}
void
print(tape_t *t1, tape_t *t2)
{
if (t1->empty())
cout << "empty ";
else
{
tape_t::const_iterator i = t1->begin();
const tape_t::const_iterator e = t1->end();
for ( ; i != e; ++i)
cout << *i << " ";
}
cout << " | ";
if (t2->empty())
cout << "empty";
else
{
tape_t::const_iterator i = t2->begin();
const tape_t::const_iterator e = t2->end();
for ( ; i != e; ++i)
cout << *i << " ";
}
}
void
print_all
(
data_t x,
data_t y,
data_t z,
tape_t *s1,
tape_t *s2,
tape_t *d1,
tape_t *d2
)
{
cout << "x " << x << " y " << y << " z " << z << "\t\t";
print(s1, s2);
cout << "\t\t";
print(d1, d2);
cout << "\n";
}
/*!
Does the real work.
*/
void
sort
(
tape_t *t1,
tape_t *t2,
tape_t *t3,
tape_t *t4
)
{
tape_t *source1 = t1;
tape_t *source2 = t2;
tape_t *dest1 = t3;
tape_t *dest2 = t4;
unsigned int x, y, z;
unsigned int count = 17; // whatever, as long as > 3
x = y = z = 0;
// small "hand-coded" sorts for n <= 3
if (n < 4)
{
count = 0;
count += read(source1, source2, &x);
count += read(source1, source2, &y);
count += read(source1, source2, &z);
}
switch (count)
{
case 0:
// no elements to sort
print(dest1, dest2);
return;
case 1:
// sorted already;
write(dest1, dest2, x);
print(dest1, dest2);
return;
case 2:
if (x < y)
{
write(dest1, dest2, x);
write(dest1, dest2, y);
} else
{
write(dest1, dest2, y);
write(dest1, dest2, x);
}
print(dest1, dest2);
return;
case 3:
sort_3(&x, &y, &z);
write(dest1, dest2, x);
write(dest1, dest2, y);
write(dest1, dest2, z);
return;
}
print_all(x, y, z, source1, source2, dest1, dest2);
// "big" routine needs tweaking for odd numbers: n / 2 not so good.
while (!is_sorted(source1, source2))
{
count = 0;
read(source1, source2, &x);
read(source1, source2, &y);
read(source1, source2, &z);
sort_3(&x, &y, &z);
// print_all(x, y, z, source1, source2, dest1, dest2);
// x < y < z
write(dest1, dest2, x);
// print_all(x, y, z, source1, source2, dest1, dest2);
// read until source tapes are empty, always writing smallest
// value into destination tapes
while (read(source1, source2, &x))
{
// puts smallest of x, y, z into x
sort_3(&x, &y, &z);
// print_all(x, y, z, source1, source2, dest1, dest2);
write(dest1, dest2, x);
// print_all(x, y, z, source1, source2, dest1, dest2);
}
// two values remain: in 'y' and 'z', with y < z: write them out.
write(dest1, dest2, y);
write(dest1, dest2, z);
print_all(x, y, z, source1, source2, dest1, dest2);
// source tapes are empty: switch source and dest pointers
std::swap(source1, dest1);
std::swap(source2, dest2);
// Another pass (over n elements) complete: rewind tapes
rewind(source1);
rewind(source2);
rewind(dest1);
rewind(dest2);
}
cout << "\n\nSorted list: ";
print(source1, source2);
cout << "\n\n\n";
}
int
main(int argc, char *argv[])
{
tape_t t1;
tape_t t2;
tape_t t3;
tape_t t4;
// fill the tape
n = 8;
write(&t1, &t2, 1);
write(&t1, &t2, 19);
write(&t1, &t2, 17);
write(&t1, &t2, 3);
write(&t1, &t2, 56);
write(&t1, &t2, 42);
write(&t1, &t2, 5);
write(&t1, &t2, 18);
sort(&t1, &t2, &t3, &t4);
return 0;
}