blob: d41fa8d186a5fcc2fc79cb0855725c6a839f9900 [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#include "rtc_base/unique_id_generator.h"
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080012
13#include <limits>
14#include <vector>
15
16#include "rtc_base/helpers.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/string_encode.h"
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080018#include "rtc_base/string_to_number.h"
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080019
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080020namespace rtc {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080021
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080022UniqueRandomIdGenerator::UniqueRandomIdGenerator() : known_ids_() {}
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080023UniqueRandomIdGenerator::UniqueRandomIdGenerator(ArrayView<uint32_t> known_ids)
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080024 : known_ids_(known_ids.begin(), known_ids.end()) {}
25
26UniqueRandomIdGenerator::~UniqueRandomIdGenerator() = default;
27
28uint32_t UniqueRandomIdGenerator::GenerateId() {
Elad Alonefc9a142019-02-08 23:35:59 +010029 RTC_CHECK_LT(known_ids_.size(), std::numeric_limits<uint32_t>::max() - 1);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080030 while (true) {
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080031 auto pair = known_ids_.insert(CreateRandomNonZeroId());
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080032 if (pair.second) {
33 return *pair.first;
34 }
35 }
36}
37
Elad Alonefc9a142019-02-08 23:35:59 +010038bool UniqueRandomIdGenerator::AddKnownId(uint32_t value) {
39 return known_ids_.insert(value).second;
Amit Hilbuchae3df542019-01-07 12:13:08 -080040}
41
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080042UniqueStringGenerator::UniqueStringGenerator() : unique_number_generator_() {}
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080043UniqueStringGenerator::UniqueStringGenerator(ArrayView<std::string> known_ids) {
Amit Hilbuchae3df542019-01-07 12:13:08 -080044 for (const std::string& str : known_ids) {
45 AddKnownId(str);
46 }
47}
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080048
49UniqueStringGenerator::~UniqueStringGenerator() = default;
50
51std::string UniqueStringGenerator::GenerateString() {
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080052 return ToString(unique_number_generator_.GenerateNumber());
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080053}
54
Elad Alonefc9a142019-02-08 23:35:59 +010055bool UniqueStringGenerator::AddKnownId(const std::string& value) {
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080056 absl::optional<uint32_t> int_value = StringToNumber<uint32_t>(value);
Amit Hilbuchae3df542019-01-07 12:13:08 -080057 // The underlying generator works for uint32_t values, so if the provided
58 // value is not a uint32_t it will never be generated anyway.
59 if (int_value.has_value()) {
Elad Alonefc9a142019-02-08 23:35:59 +010060 return unique_number_generator_.AddKnownId(int_value.value());
Amit Hilbuchae3df542019-01-07 12:13:08 -080061 }
Elad Alonefc9a142019-02-08 23:35:59 +010062 return false;
Amit Hilbuchae3df542019-01-07 12:13:08 -080063}
64
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080065} // namespace rtc