OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/metrics/perf/random_selector.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/rand_util.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/string_util.h" | |
15 | |
16 namespace metrics { | |
17 | |
18 RandomSelector::RandomSelector() {} | |
19 | |
20 RandomSelector::~RandomSelector() {} | |
21 | |
22 double RandomSelector::SumOdds(const std::vector<OddsAndValue>& odds) { | |
23 double sum = 0.0; | |
24 for (const auto& odd : odds) { | |
25 sum += odd.weight; | |
26 } | |
27 return sum; | |
28 } | |
29 | |
30 void RandomSelector::SetOdds(const std::vector<OddsAndValue>& odds) { | |
31 odds_ = odds; | |
32 sum_of_odds_ = SumOdds(odds_); | |
33 } | |
34 | |
35 const std::string& RandomSelector::GetNext() { | |
36 // Get a random double between 0 and the sum. | |
37 double random = RandDoubleUpTo(sum_of_odds_); | |
38 // Figure out what it belongs to. | |
39 return GetKeyOf(random); | |
40 } | |
41 | |
42 double RandomSelector::RandDoubleUpTo(double max) { | |
43 CHECK_GT(max, 0.0); | |
44 return max * base::RandDouble(); | |
45 } | |
46 | |
47 const std::string& RandomSelector::GetKeyOf(double value) { | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
The name key in the function name is confusing, si
dhsharp
2015/09/15 00:00:21
Done.
| |
48 double current = 0.0; | |
49 for (const auto& odd : odds_) { | |
50 current += odd.weight; | |
51 if (value < current) { | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
Nit: No {}'s
dhsharp
2015/09/15 00:00:21
Done.
| |
52 return odd.value; | |
53 } | |
54 } | |
55 NOTREACHED() << "Invalid value for key: " << value; | |
56 return base::EmptyString(); | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
The comment on base::EmptyString() suggest that it
dhsharp
2015/09/15 00:00:21
`return std::string()` would be returning a refere
Alexei Svitkine (slow)
2015/09/15 15:31:18
Gotcha. Fair enough.
| |
57 } | |
58 | |
59 } // namespace metrics | |
OLD | NEW |