-
Notifications
You must be signed in to change notification settings - Fork 16
/
split6.cpp
86 lines (76 loc) · 2.41 KB
/
split6.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
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
// http://stackoverflow.com/a/9379203/106471
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
class StringRef
{
private:
char const* begin_;
int size_;
public:
int size() const { return size_; }
char const* begin() const { return begin_; }
char const* end() const { return begin_ + size_; }
StringRef( char const* const begin, int const size )
: begin_( begin )
, size_( size )
{}
};
void split3( string const& str, vector<StringRef> &result, char delimiter = ' ' )
{
enum State { inSpace, inToken };
State state = inSpace;
char const* pTokenBegin = 0; // Init to satisfy compiler.
for( string::const_iterator it = str.begin(); it != str.end(); ++it )
{
State const newState = (*it == delimiter? inSpace : inToken);
if( newState != state )
{
switch( newState )
{
case inSpace:
result.push_back( StringRef( pTokenBegin, &*it - pTokenBegin ) );
break;
case inToken:
pTokenBegin = &*it;
}
}
state = newState;
}
if( state == inToken )
{
result.push_back( StringRef( pTokenBegin, &*str.end() - pTokenBegin ) );
}
}
int main() {
string input_line;
vector<StringRef> spline;
long count = 0;
timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
cin.sync_with_stdio(false); //disable synchronous IO
size_t numWords = 0;
size_t numChars = 0;
while(getline(cin, input_line)) {
spline.clear();
split3( input_line, spline );
numWords += spline.size();
for (vector<StringRef>::const_iterator iter = spline.begin(); iter != spline.end(); ++iter)
numChars += iter->size();
count++;
};
timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
const double sec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
cerr << "C++ : Saw " << count << " lines (" << numWords << " words/" << numChars << " chars) in " << fixed << setprecision(1) << sec << " seconds." ;
if (sec > 0) {
const double lps = count / sec;
cerr << " Crunch speed: " << fixed << setprecision(1) << lps << endl;
} else
cerr << endl;
return 0;
}