
Find the bug
By:
LucasLima on
Sep 7th, 2012 | syntax:
C++ | size: 1.85 KB | hits: 27 | expires: Never
#include <fstream>
#include <queue>
#include <cstdlib>
using namespace std;
typedef unsigned long long int ll;
struct Run {
ifstream file;
int contador;
queue<ll> buffer;
bool valid;
};
const long totalSize = 12500000*8;
const long bufferSize = 12500000/8;
const int numberRuns = 8;
Run runs[numberRuns];
long count = 0;
ifstream input("runs.bin", ios::in | ios::binary);
ofstream output("sorted.bin", ios::out | ios::binary);
void start() {
for (int i = 0; i < numberRuns; ++i) {
for (int j = 0; j < bufferSize; ++j) {
ll x;
runs[i].file.read((char*)&x, sizeof(ll));
runs[i].buffer.push(x);
++count;
}
}
}
int teste = 0;
int main () {
for (long i = 0; i < numberRuns; ++i) {
runs[i].file.open("runs.bin", ios::in | ios::binary);
runs[i].file.seekg((i*12500000)*sizeof(ll));
runs[i].contador = 0;
runs[i].valid = true;
}
start();
for (long k = 0; k < totalSize; ++k) {
ll smaller = -10000; int aux = 0;
for (int j = 0; j < numberRuns; ++j) {
if (runs[j].valid) {
if (runs[j].buffer.front() < smaller) {
smaller = runs[j].buffer.front();
aux = j;
}
}
}
runs[aux].buffer.pop();
if (runs[aux].buffer.empty()) {
for (int j = 0; j < bufferSize; ++j) {
ll x;
runs[aux].file.read((char*)&x, sizeof(ll));
runs[aux].buffer.push(x);
++count;
}
if (++runs[aux].contador == (numberRuns - 1)) runs[aux].valid = false;
//printf("%d %d %d\n", ++teste, aux, runs[aux].contador);
}
/*printf("aux %d", aux);
system("PAUSE");*/
output.write((char*)&smaller, sizeof(ll));
}
for (int i = 0; i < numberRuns; ++i) {
if (!runs[i].buffer.empty()) { printf("%d %lld\n", i, runs[i].buffer.front()); runs[i].buffer.pop();}
runs[i].file.close();
}
printf("%ld\n", count);
input.close();
output.close();
return 0;
}