Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 11 | #ifndef PC_UNIQUE_ID_GENERATOR_H_ |
| 12 | #define PC_UNIQUE_ID_GENERATOR_H_ |
| 13 | |
| 14 | #include <limits> |
| 15 | #include <set> |
| 16 | #include <string> |
| 17 | |
| 18 | #include "api/array_view.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 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). |
| 30 | template <typename TIntegral> |
| 31 | class UniqueNumberGenerator { |
| 32 | public: |
| 33 | typedef TIntegral value_type; |
| 34 | UniqueNumberGenerator(); |
| 35 | // Creates a generator that will never return any value from the given list. |
| 36 | explicit UniqueNumberGenerator(rtc::ArrayView<TIntegral> known_ids); |
| 37 | ~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 | |
| 45 | private: |
| 46 | static_assert(std::is_integral<TIntegral>::value, "Must be integral type."); |
| 47 | TIntegral counter_; |
| 48 | // This class can be further optimized by removing the known_ids_ set when |
| 49 | // the generator was created without a sequence of ids to ignore. |
| 50 | // In such a case, the implementation uses a counter which is sufficient to |
| 51 | // prevent repetitions of the generated values. |
| 52 | 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. |
| 60 | class 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. |
| 65 | explicit UniqueRandomIdGenerator(rtc::ArrayView<uint32_t> known_ids); |
| 66 | ~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 | |
| 74 | private: |
| 75 | std::set<uint32_t> known_ids_; |
| 76 | }; |
| 77 | |
| 78 | // This class will generate strings. A common use case is for identifiers. |
| 79 | // The generated strings will be unique, in the local scope of the generator. |
| 80 | // This means that a generator will never generate the same string twice. |
| 81 | // The generator can also be initialized with a sequence of known ids. |
| 82 | // In such a case, it will never generate an id from that list. |
| 83 | class UniqueStringGenerator { |
| 84 | public: |
| 85 | typedef std::string value_type; |
| 86 | UniqueStringGenerator(); |
| 87 | explicit UniqueStringGenerator(rtc::ArrayView<std::string> known_ids); |
| 88 | ~UniqueStringGenerator(); |
| 89 | |
| 90 | std::string GenerateString(); |
| 91 | std::string operator()() { return GenerateString(); } |
| 92 | |
| 93 | private: |
| 94 | // This implementation will be simple and will generate "0", "1", ... |
| 95 | UniqueNumberGenerator<uint32_t> unique_number_generator_; |
| 96 | }; |
| 97 | |
| 98 | template <typename TIntegral> |
| 99 | UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator() : counter_(0) {} |
| 100 | |
| 101 | template <typename TIntegral> |
| 102 | UniqueNumberGenerator<TIntegral>::UniqueNumberGenerator( |
| 103 | rtc::ArrayView<TIntegral> known_ids) |
| 104 | : counter_(0), known_ids_(known_ids.begin(), known_ids.end()) {} |
| 105 | |
| 106 | template <typename TIntegral> |
| 107 | UniqueNumberGenerator<TIntegral>::~UniqueNumberGenerator() {} |
| 108 | |
| 109 | template <typename TIntegral> |
| 110 | TIntegral UniqueNumberGenerator<TIntegral>::GenerateNumber() { |
| 111 | while (true) { |
| 112 | RTC_CHECK_LT(counter_, std::numeric_limits<TIntegral>::max()); |
| 113 | auto pair = known_ids_.insert(counter_++); |
| 114 | if (pair.second) { |
| 115 | return *pair.first; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } // namespace webrtc |
| 121 | |
| 122 | #endif // PC_UNIQUE_ID_GENERATOR_H_ |