Chromium Code Reviews| Index: chrome/browser/metrics/perf/random_selector.h |
| diff --git a/chrome/browser/metrics/perf/random_selector.h b/chrome/browser/metrics/perf/random_selector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7d45e7e1d3bc22569678c0fb82723ed6fdb6be41 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/perf/random_selector.h |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
| +#define CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| + |
| +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"
|
| + |
| +// RandomSelector can be used to pick vectors of strings according to certain |
| +// probabilities. The probabilities are set using SetOdds(). A randomly picked |
| +// vector can be obtained by calling GetNext(). |
| +// |
| +// Sample usage: |
| +// |
| +// RandomSelector random_selector; |
| +// std::vector<RandomSelector::OddsAndValue> odds { |
| +// {50, "a"}, |
| +// {40, "b"}, |
| +// {10, "c"} |
| +// }; |
| +// random_selector.SetOdds(odds); |
| +// |
| +// // The following should return "a" with a probability of 50%, |
| +// // "b" with a probability of 40%, and "c" with a probability of 10%: |
| +// |
| +// std::vector<std::string>& selection = random_selector.GetNext(); |
| +class RandomSelector { |
| + public: |
| + struct OddsAndValue { |
| + OddsAndValue(double weight, const std::string& value) |
| + : weight(weight), value(value) { |
| + } |
| + |
| + double weight; |
| + std::string value; |
| + }; |
| + |
| + RandomSelector(); |
| + virtual ~RandomSelector(); |
| + |
| + // Set the probabilities for various strings. |
| + void SetOdds(const std::vector<OddsAndValue>& odds); |
| + |
| + // 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".
|
| + const std::string& GetNext(); |
| + |
| + // Returns the number of string entries. |
| + 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.
|
| + return odds_.size(); |
| + } |
| + |
| + // Sum of the |weight| fields in the vector. |
| + static double SumOdds(const std::vector<OddsAndValue>& odds); |
| + |
| + private: |
| + // Get a floating point number between 0.0 and |max|. |
| + virtual double RandDoubleUpTo(double max); |
| + |
| + // Get a string corresponding to a random double |value| that is in our odds |
| + // 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.
|
| + const std::string& GetKeyOf(double value); |
| + |
| + // A dictionary representing the strings to choose from and associated odds. |
| + std::vector<OddsAndValue> odds_; |
| + |
| + double sum_of_odds_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RandomSelector); |
| +}; |
| + |
| +} // namespace metrics |
| + |
| +#endif // CHROME_BROWSER_METRICS_PERF_RANDOM_SELECTOR_H_ |