1 /* 2 * Copyright (c) 2022 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 #include "test/pc/e2e/analyzer/video/names_collection.h" 12 13 #include <set> 14 15 #include "absl/strings/string_view.h" 16 #include "absl/types/optional.h" 17 18 namespace webrtc { 19 NamesCollection(rtc::ArrayView<const std::string> names)20NamesCollection::NamesCollection(rtc::ArrayView<const std::string> names) { 21 names_ = std::vector<std::string>(names.begin(), names.end()); 22 for (size_t i = 0; i < names_.size(); ++i) { 23 index_.emplace(names_[i], i); 24 removed_.emplace_back(false); 25 } 26 size_ = names_.size(); 27 } 28 HasName(absl::string_view name) const29bool NamesCollection::HasName(absl::string_view name) const { 30 auto it = index_.find(name); 31 if (it == index_.end()) { 32 return false; 33 } 34 return !removed_[it->second]; 35 } 36 AddIfAbsent(absl::string_view name)37size_t NamesCollection::AddIfAbsent(absl::string_view name) { 38 auto it = index_.find(name); 39 if (it != index_.end()) { 40 // Name was registered in the collection before: we need to restore it. 41 size_t index = it->second; 42 if (removed_[index]) { 43 removed_[index] = false; 44 size_++; 45 } 46 return index; 47 } 48 size_t out = names_.size(); 49 size_t old_capacity = names_.capacity(); 50 names_.emplace_back(name); 51 removed_.emplace_back(false); 52 size_++; 53 size_t new_capacity = names_.capacity(); 54 55 if (old_capacity == new_capacity) { 56 index_.emplace(names_[out], out); 57 } else { 58 // Reallocation happened in the vector, so we need to rebuild `index_` to 59 // fix absl::string_view internal references. 60 index_.clear(); 61 for (size_t i = 0; i < names_.size(); ++i) { 62 index_.emplace(names_[i], i); 63 } 64 } 65 return out; 66 } 67 RemoveIfPresent(absl::string_view name)68absl::optional<size_t> NamesCollection::RemoveIfPresent( 69 absl::string_view name) { 70 auto it = index_.find(name); 71 if (it == index_.end()) { 72 return absl::nullopt; 73 } 74 size_t index = it->second; 75 if (removed_[index]) { 76 return absl::nullopt; 77 } 78 removed_[index] = true; 79 size_--; 80 return index; 81 } 82 GetPresentIndexes() const83std::set<size_t> NamesCollection::GetPresentIndexes() const { 84 std::set<size_t> out; 85 for (size_t i = 0; i < removed_.size(); ++i) { 86 if (!removed_[i]) { 87 out.insert(i); 88 } 89 } 90 return out; 91 } 92 GetAllIndexes() const93std::set<size_t> NamesCollection::GetAllIndexes() const { 94 std::set<size_t> out; 95 for (size_t i = 0; i < names_.size(); ++i) { 96 out.insert(i); 97 } 98 return out; 99 } 100 101 } // namespace webrtc 102