forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
memshuf.cpp
44 lines (36 loc) · 1.02 KB
/
memshuf.cpp
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
#include <fstream>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
typedef unsigned long int Index;
int main(int argc, char const *argv[])
{
if (argc < 2 || strcmp(argv[1], "--help") == 0){
cout << "This is a memory-efficient line shuffling algorithm.\n"
<< "It reads from a file and outputs to stdout.\n"
<< "Usage: " << argv[0] << " <file>\n";
return 0;
}
std::string fileName = argv[1];
std::ifstream file(fileName, std::ifstream::in);
// first, find all of the line markers
std::vector<Index> lineStartOffsets;
std::string line;
while (!file.eof()) {
lineStartOffsets.push_back(file.tellg());
std::getline(file, line); // don't actually care what the line is yet
file.peek();
}
// randomly shuffle the line numbers
std::random_shuffle(lineStartOffsets.begin(), lineStartOffsets.end());
for (auto offset : lineStartOffsets){
file.seekg(offset);
std::getline(file, line);
cout << line << endl;
}
file.close();
return 0;
}