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 #ifndef CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ | |
6 #define CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 | |
13 namespace metrics { | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
I think 'metrics' namespace is for things in compo
dhsharp
2015/09/15 00:00:21
You just told me to put it in 'metrics'... Done.
Alexei Svitkine (slow)
2015/09/15 15:31:18
Sorry, my previous comment was about your "using"
| |
14 | |
15 // RandomSelector can be used to pick vectors of strings according to certain | |
16 // probabilities. The probabilities are set using SetOdds(). A randomly picked | |
17 // vector can be obtained by calling GetNext(). | |
18 // | |
19 // Sample usage: | |
20 // | |
21 // RandomSelector random_selector; | |
22 // std::vector<RandomSelector::OddsAndValue> odds { | |
23 // {50, "a"}, | |
24 // {40, "b"}, | |
25 // {10, "c"} | |
26 // }; | |
27 // random_selector.SetOdds(odds); | |
28 // | |
29 // // The following should return "a" with a probability of 50%, | |
30 // // "b" with a probability of 40%, and "c" with a probability of 10%: | |
31 // | |
32 // std::vector<std::string>& selection = random_selector.GetNext(); | |
33 class RandomSelector { | |
34 public: | |
35 struct OddsAndValue { | |
36 OddsAndValue(double weight, const std::string& value) | |
37 : weight(weight), value(value) { | |
38 } | |
39 | |
40 double weight; | |
41 std::string value; | |
42 }; | |
43 | |
44 RandomSelector(); | |
45 virtual ~RandomSelector(); | |
46 | |
47 // Set the probabilities for various strings. | |
48 void SetOdds(const std::vector<OddsAndValue>& odds); | |
49 | |
50 // Get the next randomly picked string in |next|. | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
Update comment. No |next|.
Also, document semanti
dhsharp
2015/09/15 00:00:21
Renamed "Select".
| |
51 const std::string& GetNext(); | |
52 | |
53 // Returns the number of string entries. | |
54 size_t GetNumValues() const { | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
Nit: User hacker_style for inline trivial function
dhsharp
2015/09/15 00:00:21
Done.
| |
55 return odds_.size(); | |
56 } | |
57 | |
58 // Sum of the |weight| fields in the vector. | |
59 static double SumOdds(const std::vector<OddsAndValue>& odds); | |
60 | |
61 private: | |
62 // Get a floating point number between 0.0 and |max|. | |
63 virtual double RandDoubleUpTo(double max); | |
64 | |
65 // Get a string corresponding to a random double |value| that is in our odds | |
66 // vector. Stores the result in |key|. | |
Alexei Svitkine (slow)
2015/09/14 19:12:33
Comment seems incorrect - there's no |key|.
dhsharp
2015/09/15 00:00:21
Done. Sorry, old comments from debugd.
| |
67 const std::string& GetKeyOf(double value); | |
68 | |
69 // A dictionary representing the strings to choose from and associated odds. | |
70 std::vector<OddsAndValue> odds_; | |
71 | |
72 double sum_of_odds_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(RandomSelector); | |
75 }; | |
76 | |
77 } // namespace metrics | |
78 | |
79 #endif // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ | |
OLD | NEW |