blob: 849daa9758b221fb928e7c6a3fa8674088470675 [file] [log] [blame]
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080011#ifndef RTC_BASE_UNIQUE_ID_GENERATOR_H_
12#define RTC_BASE_UNIQUE_ID_GENERATOR_H_
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080013
14#include <limits>
15#include <set>
16#include <string>
17
18#include "api/array_view.h"
19
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080020namespace rtc {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080021
22// This class will generate numbers. A common use case is for identifiers.
23// The generated numbers will be unique, in the local scope of the generator.
24// This means that a generator will never generate the same number twice.
25// The generator can also be initialized with a sequence of known ids.
26// In such a case, it will never generate an id from that list.
27// Recommendedations:
28// * Prefer unsigned types.
29// * Prefer larger types (uint8_t will run out quickly).
30template <typename TIntegral>
31class UniqueNumberGenerator {
32 public:
33 typedef TIntegral value_type;
34 UniqueNumberGenerator();
35 // Creates a generator that will never return any value from the given list.
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080036 explicit UniqueNumberGenerator(ArrayView<TIntegral> known_ids);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080037 ~UniqueNumberGenerator();
38
39 // Generates a number that this generator has never produced before.
40 // If there are no available numbers to generate, this method will fail
41 // with an |RTC_CHECK|.
42 TIntegral GenerateNumber();
43 TIntegral operator()() { return GenerateNumber(); }
44
Amit Hilbuchae3df542019-01-07 12:13:08 -080045 // Adds an id that this generator should no longer generate.
46 void AddKnownId(TIntegral value);
47
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080048 private:
49 static_assert(std::is_integral<TIntegral>::value, "Must be integral type.");
50 TIntegral counter_;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080051 std::set<TIntegral> known_ids_;
52};
53
54// This class will generate unique ids. Ids are 32 bit unsigned integers.
55// The generated ids will be unique, in the local scope of the generator.
56// This means that a generator will never generate the same id twice.
57// The generator can also be initialized with a sequence of known ids.
58// In such a case, it will never generate an id from that list.
59class UniqueRandomIdGenerator {
60 public:
61 typedef uint32_t value_type;
62 UniqueRandomIdGenerator();
63 // Create a generator that will never return any value from the given list.
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080064 explicit UniqueRandomIdGenerator(ArrayView<uint32_t> known_ids);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080065 ~UniqueRandomIdGenerator();
66
67 // Generates a random id that this generator has never produced before.
68 // This method becomes more expensive with each use, as the probability of
69 // collision for the randomly generated numbers increases.
70 uint32_t GenerateId();
71 uint32_t operator()() { return GenerateId(); }
72
Amit Hilbuchae3df542019-01-07 12:13:08 -080073 // Adds an id that this generator should no longer generate.
74 void AddKnownId(uint32_t value);
75
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080076 private:
77 std::set<uint32_t> known_ids_;
78};
79
80// This class will generate strings. A common use case is for identifiers.
81// The generated strings will be unique, in the local scope of the generator.
82// This means that a generator will never generate the same string twice.
83// The generator can also be initialized with a sequence of known ids.
84// In such a case, it will never generate an id from that list.
85class UniqueStringGenerator {
86 public:
87 typedef std::string value_type;
88 UniqueStringGenerator();
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080089 explicit UniqueStringGenerator(ArrayView<std::string> known_ids);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080090 ~UniqueStringGenerator();
91
92 std::string GenerateString();
93 std::string operator()() { return GenerateString(); }
94
Amit Hilbuchae3df542019-01-07 12:13:08 -080095 // Adds an id that this generator should no longer generate.
96 void AddKnownId(const std::string& value);
97
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080098 private:
99 // This implementation will be simple and will generate "0", "1", ...
100 UniqueNumberGenerator<uint32_t> unique_number_generator_;
101};
102
103template <typename TIntegral>
104UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator() : counter_(0) {}
105
106template <typename TIntegral>
107UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator(
Amit Hilbuchdbb49df2019-01-23 14:54:24 -0800108 ArrayView<TIntegral> known_ids)
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800109 : counter_(0), known_ids_(known_ids.begin(), known_ids.end()) {}
110
111template <typename TIntegral>
112UniqueNumberGenerator<TIntegral>::~UniqueNumberGenerator() {}
113
114template <typename TIntegral>
115TIntegral UniqueNumberGenerator<TIntegral>::GenerateNumber() {
116 while (true) {
117 RTC_CHECK_LT(counter_, std::numeric_limits<TIntegral>::max());
118 auto pair = known_ids_.insert(counter_++);
119 if (pair.second) {
120 return *pair.first;
121 }
122 }
123}
124
Amit Hilbuchae3df542019-01-07 12:13:08 -0800125template <typename TIntegral>
126void UniqueNumberGenerator<TIntegral>::AddKnownId(TIntegral value) {
127 known_ids_.insert(value);
128}
Amit Hilbuchdbb49df2019-01-23 14:54:24 -0800129} // namespace rtc
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800130
Amit Hilbuchdbb49df2019-01-23 14:54:24 -0800131#endif // RTC_BASE_UNIQUE_ID_GENERATOR_H_