include iostream include fstream include list include vector define LE

  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
#include <iostream>
#include <fstream>
#include <list>
#include <vector>
#define LENGHT_STRING 255
using namespace std;
int rand_gen(int size){
return rand()%size;
}
class Key{
int key1;
unsigned char key2;
public:
Key(){
key1=0;
key2=0;
}
Key(Key* a_key){
key1=a_key->key1;
key2=a_key->key2;
}
Key(int a_key1, char a_key2){
key1=a_key1;
key2=a_key2;
}
friend ostream &operator<<(ostream &stream, Key key);
friend int operator<(Key key1, Key key2);
Key operator++(int i){
key2-=97;
key2++;
if(key2/26){
key1++;
key2=0;
}
key2+=97;
return *this;
}
Key operator=(Key op2){
key1=op2.key1;
key2=op2.key2;
return *this;
}
Key operator=(Key *op2){
key1=op2->key1;
key2=op2->key2;
return *this;
}
};
ostream &operator<<(ostream &stream, Key key){
stream<<key.key1<<key.key2;
}
int operator<(Key key1, Key key2){
if(key1.key1 != key2.key1)
return key1.key1 < key2.key1;
return key1.key2 < key2.key2;
}
class Row{
public:
Key *key;
char *value;
public:
Row (Key *a_key, char* a_value){
key=a_key;
value=a_value;
}
};
ostream &operator<<(ostream &stream, Row row){
stream<< *(row.key)<<": "<<(row.value)<<"\n";
}
int operator<(Row row1, Row row2){
return *row1.key<*row2.key;
}
class Table{
list<Row*> rows;
void Add(char* str){
Key *last_key;
last_key = new Key();
if(rows.size()){
Row* last_row=rows.back();
last_key=new Key(last_row->key);
}
(*last_key)++;
rows.push_back(new Row(last_key,str));
}
void LoadFromFile(char* filename){
int i,x,y,h,w;
double re,im;
ifstream in(filename);
if(!in)
exit(1);
while(in){
char *str= new char[LENGHT_STRING];
in.getline(str,LENGHT_STRING);
Add(str);
}
}
public:
Table(char* filename){
rows.clear();
LoadFromFile(filename);
}
void merge(int lb, int split, int ub){
list<Row*>::iterator left_i=rows.begin();
list<Row*>::iterator right_i=rows.begin();
int left=lb;
int right=split;
for(int i=0;i<lb;i++)
left_i++;
right_i=left_i;
for(int i=lb;i<split;i++)
right_i++;
list<Row*>::iterator initial_left = left_i;
list<Row*>::iterator initial_right = right_i;
list<Row*> temp;
list<Row*>::iterator temp_i=temp.begin();
while(left<split && right<ub){
if( (**left_i) < (**right_i)){
temp.push_back(*left_i);
left_i++;
left++;
}
else{
temp.push_back(*right_i);
right_i++;
right++;
}
}
while(left<split){
temp.push_back(*left_i);
left_i++;
left++;
}
while(right<ub){
temp.push_back(*right_i);
right_i++;
right++;
}
left_i=initial_left;
while(temp.size()){
rows.insert(rows.erase(left_i++),temp.front());
temp.erase(temp.begin());
}
}
void mergeSort(int lb, int ub) {
long split;
if (ub-lb>1) {
split = (lb + ub)/2;
mergeSort(lb, split);
mergeSort(split, ub);
merge(lb, split, ub);
}
}
void MergeSort(){
mergeSort(0,rows.size());
}
void OrderDesc(){
reverse(rows.begin(),rows.end());
}
void OrderRand(){
vector<Row*> rows2;
//list<Row*>::iterator b=rows.end();
rows2.insert(rows2.begin(),rows.begin(),rows.end());
random_shuffle(rows2.begin(),rows2.end());
rows.clear();
rows.insert(rows.begin(),rows2.begin(),rows2.end());
}
friend ostream &operator<<(ostream &stream, Table table);
};
ostream &operator<<(ostream &stream, Table table){
cout<<"===============================================================================\n";
list<Row*>::iterator p=table.rows.begin();
while(p!=table.rows.end()){
cout<<**p;
p++;
}
cout<<"===============================================================================\n";
}
void ShowMeTheMagic(char* fn){
Table table(fn);
cout<<"Initial table:\n";
cout<<table;
table.MergeSort();
cout<<"Table after sort:\n";
cout<<table;
cout<<"Table in reverse order:\n";
table.OrderDesc();
cout<<table;
table.MergeSort();
cout<<"Table after sort:\n";
cout<<table;
cout<<"Table in random order:\n";
table.OrderRand();
cout<<table;
table.MergeSort();
cout<<"Table after sort:\n";
cout<<table;
}
int main(){
ShowMeTheMagic("duck.txt");
ShowMeTheMagic("blinky.txt");
ShowMeTheMagic("test.txt");
system("pause");
}