blob: 836dc70b615dc597e12a4cb50f96673a5198a040 [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.
Elad Alonefc9a142019-02-08 23:35:59 +010046 // Return value indicates whether the ID was hitherto unknown.
47 bool AddKnownId(TIntegral value);
Amit Hilbuchae3df542019-01-07 12:13:08 -080048
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080049 private:
50 static_assert(std::is_integral<TIntegral>::value, "Must be integral type.");
51 TIntegral counter_;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080052 std::set<TIntegral> known_ids_;
53};
54
55// This class will generate unique ids. Ids are 32 bit unsigned integers.
56// The generated ids will be unique, in the local scope of the generator.
57// This means that a generator will never generate the same id twice.
58// The generator can also be initialized with a sequence of known ids.
59// In such a case, it will never generate an id from that list.
60class UniqueRandomIdGenerator {
61 public:
62 typedef uint32_t value_type;
63 UniqueRandomIdGenerator();
64 // Create a generator that will never return any value from the given list.
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080065 explicit UniqueRandomIdGenerator(ArrayView<uint32_t> known_ids);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080066 ~UniqueRandomIdGenerator();
67
68 // Generates a random id that this generator has never produced before.
69 // This method becomes more expensive with each use, as the probability of
70 // collision for the randomly generated numbers increases.
71 uint32_t GenerateId();
72 uint32_t operator()() { return GenerateId(); }
73
Amit Hilbuchae3df542019-01-07 12:13:08 -080074 // Adds an id that this generator should no longer generate.
Elad Alonefc9a142019-02-08 23:35:59 +010075 // Return value indicates whether the ID was hitherto unknown.
76 bool AddKnownId(uint32_t value);
Amit Hilbuchae3df542019-01-07 12:13:08 -080077
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080078 private:
79 std::set<uint32_t> known_ids_;
80};
81
82// This class will generate strings. A common use case is for identifiers.
83// The generated strings will be unique, in the local scope of the generator.
84// This means that a generator will never generate the same string twice.
85// The generator can also be initialized with a sequence of known ids.
86// In such a case, it will never generate an id from that list.
87class UniqueStringGenerator {
88 public:
89 typedef std::string value_type;
90 UniqueStringGenerator();
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080091 explicit UniqueStringGenerator(ArrayView<std::string> known_ids);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080092 ~UniqueStringGenerator();
93
94 std::string GenerateString();
95 std::string operator()() { return GenerateString(); }
96
Amit Hilbuchae3df542019-01-07 12:13:08 -080097 // Adds an id that this generator should no longer generate.
Elad Alonefc9a142019-02-08 23:35:59 +010098 // Return value indicates whether the ID was hitherto unknown.
99 bool AddKnownId(const std::string& value);
Amit Hilbuchae3df542019-01-07 12:13:08 -0800100
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800101 private:
102 // This implementation will be simple and will generate "0", "1", ...
103 UniqueNumberGenerator<uint32_t> unique_number_generator_;
104};
105
106template <typename TIntegral>
107UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator() : counter_(0) {}
108
109template <typename TIntegral>
110UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator(
Amit Hilbuchdbb49df2019-01-23 14:54:24 -0800111 ArrayView<TIntegral> known_ids)
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800112 : counter_(0), known_ids_(known_ids.begin(), known_ids.end()) {}
113
114template <typename TIntegral>
115UniqueNumberGenerator<TIntegral>::~UniqueNumberGenerator() {}
116
117template <typename TIntegral>
118TIntegral UniqueNumberGenerator<TIntegral>::GenerateNumber() {
119 while (true) {
120 RTC_CHECK_LT(counter_, std::numeric_limits<TIntegral>::max());
121 auto pair = known_ids_.insert(counter_++);
122 if (pair.second) {
123 return *pair.first;
124 }
125 }
126}
127
Amit Hilbuchae3df542019-01-07 12:13:08 -0800128template <typename TIntegral>
Elad Alonefc9a142019-02-08 23:35:59 +0100129bool UniqueNumberGenerator<TIntegral>::AddKnownId(TIntegral value) {
130 return known_ids_.insert(value).second;
Amit Hilbuchae3df542019-01-07 12:13:08 -0800131}
Amit Hilbuchdbb49df2019-01-23 14:54:24 -0800132} // namespace rtc
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800133
Amit Hilbuchdbb49df2019-01-23 14:54:24 -0800134#endif // RTC_BASE_UNIQUE_ID_GENERATOR_H_