• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 
3 // SampleCppLinq.cpp : Defines the entry point for the console application.
4 //
5 
6 #include <cpplinq/linq.hpp>
7 
8 #include <iostream>
9 #include <fstream>
10 #include <iomanip>
11 #include <string>
12 #include <vector>
13 #include <exception>
14 #include <regex>
15 #include <cmath>
16 #include <algorithm>
17 #include <numeric>
18 
19 using namespace std;
20 
21 vector<string> load_data();
22 string extract_value(const string& input, const string& key);
23 
24 
run()25 void run()
26 {
27     using namespace cpplinq;
28 
29     struct item {
30         string args;
31         int    concurrency;
32         double time;
33 
34         item(const string& input) {
35             args =              extract_value(input, "args");
36             concurrency = atoi( extract_value(input, "concurrency").c_str() );
37             time =        atof( extract_value(input, "time").c_str() );
38         }
39     };
40 
41     auto data_unparsed = load_data();
42     auto data_parsed =
43         from(data_unparsed)
44         .select([](const string& line) { return item(line); })
45         .to_vector();
46 
47     cout << "data loaded" << endl;
48 
49     auto data =
50         from(data_parsed)
51         .groupby([](const item& i) { return i.args; });
52 
53     for (auto giter = data.begin(), end = data.end(); giter != end; ++giter)
54     {
55         const auto& g = *giter;
56 
57         cout << "arguments: " << g.key << endl;
58 
59         cout << "concurrency, mean, |, raw_data," << endl;
60         auto seq =
61             from(g)
62             .groupby([](const item& i) { return i.concurrency; });
63 
64         for (auto giter = seq.begin(), end = seq.end(); giter != end; ++giter)
65         {
66             const auto& g = *giter;
67 
68             cout << g.key << ", ";
69 
70             auto times = from(g).select([](const item& i) { return i.time; });
71 
72             auto n = from(g).count();
73             auto sum = std::accumulate(times.begin(), times.end(), 0.0);
74 
75             cout << (sum / n) << ", |";
76 
77             for (auto timeIter = times.begin(), end = times.end();
78                 timeIter != end;
79                 ++timeIter)
80             {
81                 cout << ", " << *timeIter;
82             }
83             cout << endl;
84         }
85     }
86 }
87 
88 
main()89 int main()
90 {
91     try {
92         run();
93     } catch (exception& e) {
94         cerr << "exception: " << e.what() << endl;
95     }
96 }
97 
load_data()98 vector<string> load_data()
99 {
100 	ifstream datafile("data.txt");
101     vector<string> v;
102     string line;
103 
104     if (datafile.fail())
105         throw logic_error("could not find file");
106 
107     while(getline(datafile, line))
108         v.push_back(line);
109 
110     return v;
111 }
112 
113 regex key_value_pair("'([^\']*)'\\s*[:,]\\s*(\\d+(?:\\.\\d+)?|'[^']*')");
114 
extract_value(const string & input,const string & key)115 string extract_value(const string& input, const string& key)
116 {
117     const std::sregex_iterator end;
118     for (std::sregex_iterator i(input.cbegin(), input.cend(), key_value_pair);
119         i != end;
120         ++i)
121     {
122         if ((*i)[1] == key)
123         {
124             return (*i)[2];
125         }
126     }
127     throw std::range_error("search key not found");
128 }
129