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
| #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <climits> #include <algorithm> #include <queue> #include <vector> #include <fstream> #include <ctime> #include <random> using namespace std;
struct Variable//变量结构体 { Variable(){ visitDiskTime=0; n=0; m=0; numberOfDisk=0; } string path; string fileName="Disk.txt"; string file; int visitDiskTime,n,m,numberOfDisk; char ch1,ch; }; struct SequentialStringPlayer//用于构建顺串 { int id,key; bool operator<=(SequentialStringPlayer &p){ return (id!=p.id) ? (id<p.id) : (key<p.key); } }; template <class T> class loserTree { public: virtual ~loserTree(){} virtual void initialize(T* thePlayer,int number)=0; virtual int getTheWinner() const =0; virtual void rePlay(int thePLayer,T newvalue)=0; }; template <class T> class MinimumLoserTree : public loserTree<T> { public: MinimumLoserTree(T* thePlayer=nullptr,int theNumberOfPlayers=0){ tree=nullptr; advance=nullptr; initialize(thePlayer,theNumberOfPlayers); } ~MinimumLoserTree(){delete[] tree;delete[] advance;} void initialize(T* thePlayer,int theNumberOfPlayers); int getTheWinner() const {return tree[0];}; void rePlay(int thePlayer,T newvalue); private: int numberOfPlayers; int* tree; int* advance; T* player; int lowExt; int offset; void play(int,int,int); int winner(int x,int y){return player[x]<=player[y]?x:y;}; int loser(int x,int y){return player[x]<=player[y]?y:x;}; }; template <class T> void MinimumLoserTree<T>::initialize(T* thePlayer,int theNumberOfPlayers) { int n=theNumberOfPlayers; if(n<2) { cout<<"Error! the number must >= 2"<<endl; return; } player=thePlayer; numberOfPlayers=n; delete[] tree; delete[] advance; tree=new int[n+1]; advance=new int[n+1];
int s; for (s=1; 2*s<=n-1; s+=s);
lowExt=2*(n-s); offset=2*s-1;
for (int i=2; i<=lowExt; i+=2) play((i+offset)/2,i-1,i);
int temp=0; if(n%2==1){ play(n/2,advance[n-1],lowExt+1); temp=lowExt+3; } else temp=lowExt+2;
for (int i=temp; i<=n; i+=2) play((i-lowExt+n-1)/2,i-1,i);
tree[0]=advance[1];
} template <class T> void MinimumLoserTree<T>::play(int p, int leftChild, int rightChild) { tree[p]=loser(leftChild,rightChild); advance[p]=winner(leftChild,rightChild);
while(p % 2 == 1 && p > 1){ tree[p/2]=loser(advance[p-1],advance[p]); advance[p/2]=winner(advance[p-1],advance[p]); p /= 2; } } template <class T> void MinimumLoserTree<T>::rePlay(int thePlayer,T newvalue) { int n=numberOfPlayers; if(thePlayer <= 0 || thePlayer > n){ cout<<"Error! the number must >0 & <=n"<<endl; return; }
player[thePlayer]=newvalue;
int matchNode, leftChild, rightChild;
if(thePlayer<=lowExt){ matchNode=(offset+thePlayer)/2; leftChild=2*matchNode-offset; rightChild=leftChild+1; } else { matchNode=(thePlayer-lowExt+n-1)/2; if(2*matchNode==n-1){ leftChild=advance[2*matchNode]; rightChild=thePlayer; } else{ leftChild=2*matchNode-n+1+lowExt; rightChild=leftChild+1; } }
if(thePlayer==tree[0]){ for (; matchNode>=1; matchNode/=2){ int oldLoserNode=tree[matchNode]; tree[matchNode]=loser(oldLoserNode,thePlayer); advance[matchNode]=winner(oldLoserNode,thePlayer); thePlayer=advance[matchNode]; } } else { tree[matchNode]=loser(leftChild,rightChild); advance[matchNode]=winner(leftChild,rightChild); if(matchNode==n-1 && n%2==1){ matchNode/=2; tree[matchNode]=loser(advance[n-1],lowExt+1); advance[matchNode]=winner(advance[n-1],lowExt+1); } matchNode/=2; for (; matchNode>=1; matchNode/=2){ tree[matchNode]=loser(advance[matchNode*2],advance[matchNode*2+1]); advance[matchNode]=winner(advance[matchNode*2],advance[matchNode*2+1]); } } tree[0]=advance[1]; } void init(Variable &va) { cout<<"请输入您想要的模拟磁盘位置,接下来的操作都是在当前路径下进行,输入样例:/Users/longzhengtian/Desktop/text/"<<endl; cout<<"请输入:"; cin>>va.path; va.file=va.path+va.fileName; cout<<"请输入您想要在磁盘中初始化数字的个数,这些数字将用于模拟外排序过程:"; cin>>va.n; cout<<"请输入缓冲区大小(此处为缓冲区能存储数字的个数):"; cin>>va.m; cout<<"请问您是否想要将原始数据,顺串,最终数据显示在控制台中('y' or 'n'):"; cin>>va.ch1; cout<<endl;
ofstream fout1(va.file); if(!fout1.is_open()){ cout<<"无法打开指定磁盘文件,无法初始化磁盘"<<endl; exit(0); } if(va.ch1=='y') cout<<"原始磁盘文件有:"<<endl; for (int i=1; i<=va.n; i++){ int x=random()%1000+1; fout1<<x<<' '; if(va.ch1=='y') cout<<x<<' '; } if(va.ch1=='y') cout<<endl<<endl; fout1.close(); } void SequentialStringConstruction(Variable &va,SequentialStringPlayer* ssplayer) { ifstream fin1(va.file); if(!fin1.is_open()){ cout<<"无法打开指定磁盘文件,无法进行顺串构造"<<endl; exit(0); } for (int i=1; i<=va.m; i++){ fin1>>ssplayer[i].key; ssplayer[i].id=1; va.visitDiskTime++; } MinimumLoserTree<SequentialStringPlayer> sstree(ssplayer,va.m); int num=0; for (int i=1; i<=va.n; i++){
if(!(fin1>>num)) num=INT_MIN; else va.visitDiskTime++;
SequentialStringPlayer tempwinner=ssplayer[sstree.getTheWinner()]; SequentialStringPlayer tempnum; tempnum.key=num;
if(num >= tempwinner.key) tempnum.id=tempwinner.id; else tempnum.id=num=(num==INT_MIN) ? INT_MAX :tempwinner.id+1;
sstree.rePlay(sstree.getTheWinner(),tempnum);
string smallfile=va.path+"smallfile"+to_string(tempwinner.id)+".txt"; va.numberOfDisk=max(va.numberOfDisk,tempwinner.id); ofstream fout2(smallfile, ios::app); fout2<<tempwinner.key<<' ';
fout2.close(); va.visitDiskTime++; } fin1.close(); cout<<"顺串分配完毕,生成"<<va.numberOfDisk<<"个顺串,顺串文件见您当前路径下出现的新文件"<<endl; if(va.ch1=='y'){ cout<<"我们将这些数据在这里显示如下:"<<endl; for (int i=1; i<=va.numberOfDisk; i++) { string smallfile=va.path+"smallfile"+to_string(i)+".txt"; ifstream finx(smallfile); int tempx=0; cout<<"smallfile"<<i<<".txt: "; while(finx>>tempx) cout<<tempx<<' '; cout<<endl; finx.close(); } } } void GenerateTheFinalResult(Variable &va) { cout<<endl<<"请问是否将最终排序结果放入原文件,如果否,则程序将新建一个Disk2.txt文件并放入此文件中(输入'y'或'n'代表'是'与'否'):"; cin>>va.ch; string newFile; if(va.ch=='y') newFile=va.file; else newFile=va.path+"Disk2.txt";
ofstream fout3(newFile);
if(va.numberOfDisk==1){ string smallfile=va.path+"smallfile"+to_string(1)+".txt"; ifstream fin4(smallfile); int tempnumber; while(fin4>>tempnumber){ fout3<<tempnumber<<' '; va.visitDiskTime+=2; } fout3.close(); fin4.close(); cout<<"由于只生成了1个顺串,直接将此结果放入目标文件中,磁盘访存次数为"<<va.visitDiskTime<<"次,原因是每个数据都读写4次"<<endl; if(va.ch1=='y'){ cout<<"最终外排序结果如下:"<<endl; ifstream finy(newFile); int tempy; while(finy>>tempy) cout<<tempy<<' '; cout<<endl; finy.close(); } exit(0); }
int dplayer[va.numberOfDisk+10]; int pointer[va.numberOfDisk+10]; for (int i=1; i<=va.numberOfDisk; i++){ string smallfile=va.path+"smallfile"+to_string(i)+".txt"; ifstream fin2(smallfile); fin2>>dplayer[i]; pointer[i]=fin2.tellg(); fin2.close(); } MinimumLoserTree<int> dtree(dplayer,va.numberOfDisk); int cnt=0; while(cnt<va.n){ cnt++; int temp=dtree.getTheWinner(); int tempwinner=dplayer[temp]; fout3<<tempwinner<<' '; va.visitDiskTime++; string smallfile=va.path+"smallfile"+to_string(temp)+".txt"; ifstream fin3(smallfile); fin3.clear(); fin3.seekg(pointer[temp]+1);
int tempnum; if(pointer[temp]+1==0) tempnum=INT_MAX; else { fin3>>tempnum; pointer[temp]=fin3.tellg(); if(pointer[temp]+1==0) tempnum=INT_MAX; } va.visitDiskTime++; dtree.rePlay(temp,tempnum); fin3.close(); } fout3.close(); cout<<endl; cout<<"将这些文件进行"<<va.numberOfDisk<<"路归并,已经结果存入到目标文件中。磁盘访存次数为"<<va.visitDiskTime<<"次,原因是每个数据都读写4次"<<endl; if(va.ch1=='y'){ cout<<"最终外排序结果如下:"<<endl; ifstream finy(newFile); int tempy; while(finy>>tempy) cout<<tempy<<' '; cout<<endl; finy.close(); } } int main() { srand(time(nullptr)); Variable va;
init(va);
SequentialStringPlayer ssplayer[va.n+10];
SequentialStringConstruction(va,ssplayer);
GenerateTheFinalResult(va);
return 0; }
|