OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ | 5 #ifndef CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
6 #define CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ | 6 #define CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 // | 28 // |
29 // The above line should return "a" with a probability of 50%, | 29 // The above line should return "a" with a probability of 50%, |
30 // "b" with a probability of 40%, and "c" with a probability of 10%: | 30 // "b" with a probability of 40%, and "c" with a probability of 10%: |
31 class RandomSelector { | 31 class RandomSelector { |
32 public: | 32 public: |
33 struct WeightAndValue { | 33 struct WeightAndValue { |
34 WeightAndValue(double weight, const std::string& value) | 34 WeightAndValue(double weight, const std::string& value) |
35 : weight(weight), value(value) { | 35 : weight(weight), value(value) { |
36 } | 36 } |
37 | 37 |
| 38 bool operator==(const WeightAndValue& other) const { |
| 39 return weight == other.weight && value == other.value; |
| 40 } |
| 41 |
38 // Probability weight for selecting this value. | 42 // Probability weight for selecting this value. |
39 double weight; | 43 double weight; |
40 // Value to be returned by Select(), if selected. | 44 // Value to be returned by Select(), if selected. |
41 std::string value; | 45 std::string value; |
42 }; | 46 }; |
43 | 47 |
44 RandomSelector(); | 48 RandomSelector(); |
45 virtual ~RandomSelector(); | 49 virtual ~RandomSelector(); |
46 | 50 |
47 // Set the probabilities for various strings. | 51 // Set the probabilities for various strings. Returns false and doesn't |
48 void SetOdds(const std::vector<WeightAndValue>& odds); | 52 // modify the values if odds contains any invalid weights (<=0.0) or if |
| 53 // odds is empty. |
| 54 bool SetOdds(const std::vector<WeightAndValue>& odds); |
49 | 55 |
50 // Randomly select one of the values from the set. | 56 // Randomly select one of the values from the set. |
51 const std::string& Select(); | 57 const std::string& Select(); |
52 | 58 |
53 // Returns the number of string entries. | 59 // Returns the number of string entries. |
54 size_t num_values() const { | 60 size_t num_values() const { |
55 return odds_.size(); | 61 return odds_.size(); |
56 } | 62 } |
57 | 63 |
58 // Sum of the |weight| fields in the vector. | 64 const std::vector<WeightAndValue>& odds() const { return odds_; } |
| 65 |
| 66 // Sum of the |weight| fields in the vector. Returns -1.0 if odds contains any |
| 67 // weight <= 0.0. |
59 static double SumWeights(const std::vector<WeightAndValue>& odds); | 68 static double SumWeights(const std::vector<WeightAndValue>& odds); |
60 | 69 |
61 private: | 70 private: |
62 // Get a floating point number between 0.0 and |max|. | 71 // Get a floating point number between 0.0 and |max|. |
63 virtual double RandDoubleUpTo(double max); | 72 virtual double RandDoubleUpTo(double max); |
64 | 73 |
65 // Get a string corresponding to |random| that is in the odds vector. | 74 // Get a string corresponding to |random| that is in the odds vector. |
66 // |random| must be a number between zero and the sum of the probability | 75 // |random| must be a number between zero and the sum of the probability |
67 // weights. | 76 // weights. |
68 const std::string& GetValueFor(double random); | 77 const std::string& GetValueFor(double random); |
69 | 78 |
70 // A dictionary representing the strings to choose from and associated odds. | 79 // A dictionary representing the strings to choose from and associated odds. |
71 std::vector<WeightAndValue> odds_; | 80 std::vector<WeightAndValue> odds_; |
72 | 81 |
73 // Sum of the probability weights. | 82 // Sum of the probability weights. |
74 double sum_of_weights_; | 83 double sum_of_weights_; |
75 | 84 |
76 DISALLOW_COPY_AND_ASSIGN(RandomSelector); | 85 DISALLOW_COPY_AND_ASSIGN(RandomSelector); |
77 }; | 86 }; |
78 | 87 |
| 88 ::std::ostream& operator<<( |
| 89 ::std::ostream& os, const RandomSelector::WeightAndValue& value); |
| 90 |
79 #endif // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ | 91 #endif // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
OLD | NEW |