-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.cpp
More file actions
132 lines (111 loc) · 2.9 KB
/
generate.cpp
File metadata and controls
132 lines (111 loc) · 2.9 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
#include <list>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <random>
#include <stdint.h>
#include <chrono>
#include <omp.h>
#include <array>
#include <algorithm>
#include <future>
#include <assert.h>
#include "Channel.hpp"
using namespace std;
const uint64_t max_phone_number=9'999'999'999;
struct entry{
uint64_t phoneNumber;
uint8_t service_area_code;
uint8_t prefrences;
uint8_t phone_type;
uint8_t opstype;
entry( std::mt19937_64 &mt){
std::uniform_int_distribution<uint64_t>phone_distro(0,max_phone_number);
std::uniform_int_distribution<uint8_t> service_distro(1,23);
std::uniform_int_distribution<uint8_t> prefrences_distro(0,127);
std::uniform_int_distribution<uint8_t> phone_type_distro(1,3);
std::uniform_int_distribution<uint8_t> opstype_distro(0,1);
phoneNumber = phone_distro(mt);
service_area_code = service_distro(mt);
prefrences = prefrences_distro(mt);
phone_type = phone_type_distro(mt);
opstype = opstype_distro(mt);
}
string prefrences_string()const{
if( prefrences==0 ){
return "0";
}
string ret="";
for( int i=0; i<7; i++){
if( ( prefrences >> i ) & 1 ){
ret+=to_string(1+i)+"#";
}
}
ret.erase( ret.end()-1, ret.end());
return ret;
}
};
string to_string( const entry & e){
string ret;
ret.reserve(1024);
ret = "\""+to_string(e.service_area_code)+"\","+"\""+to_string( e.phoneNumber)+"\","+"\""+ e.prefrences_string() +"\","+"\""+(e.opstype?"A":"D")+"\","+"\""+to_string( e.phone_type)+"\"";
return ret;
}
void fun(mt19937_64 mt, Channel<size_t> *in, Channel<vector<string>> *out){
size_t todo ;
while(true){
if( ! in->get( todo ) ){
return;
}
vector<string> v;
v.reserve(todo);
for( size_t ii=0; ii<todo; ii++){
string s;
s.reserve(1024);
s= to_string( entry(mt) ) + "\n";
v.emplace_back( move(s) );
}
out->emplace(move(v));
}
}
int main(int argc, char* argv[] ){
size_t size=600'000'000;
if(argc == 2 ){
size=atoi(argv[1]);
}
ofstream file("dump");
const int number_of_workers=8;
array<thread,number_of_workers> workers;
Channel<uint64_t> in;
Channel<vector<string>> out;
for( int i=0; i<number_of_workers; i++){
mt19937_64 mer;
mer.seed(i);
workers[i] = thread( fun, mer, &in, &out);
workers[i].detach();
}
file<<"\"Service Area Code\",\"Phone Numbers\",\"Preferences\",\"Opstype\",\"Phone Type\""<<endl;
const int step=500000;
int cout_in=0;
while(size > 0){
int toget = step;
if( step > size ) toget = size;
in.put(toget);
{
size_t oldsize = size;
size -= step;
if( oldsize < size ) size=0; //prevent underflow
}
cout_in++;
}
in.close();
for( int i=0; i<cout_in; i++){
cout<<"count_in: "<<cout_in--<<endl;
vector<string> v;
out.get(v);
for( const auto &s : v){
file<<s;
}
}
return 0;
}