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 #ifndef TEST_PC_E2E_ANALYZER_VIDEO_NAMES_COLLECTION_H_ 12 #define TEST_PC_E2E_ANALYZER_VIDEO_NAMES_COLLECTION_H_ 13 14 #include <map> 15 #include <set> 16 #include <string> 17 #include <vector> 18 19 #include "absl/strings/string_view.h" 20 #include "absl/types/optional.h" 21 #include "api/array_view.h" 22 23 namespace webrtc { 24 25 // Contains mapping between string names and unique size_t values (indexes). 26 // Once the name is added to the collection it is guaranteed: 27 // 1. Name will have the same index until collection will be destructed 28 // 2. Adding, removing and re-adding name won't change its index 29 // 30 // The name is considered in the collection if it was added and wasn't removed. 31 // Adding the name when it is in the collection won't change the collection, the 32 // same as removing the name when it is removed. 33 // 34 // Collection will return name's index and name for the index independently from 35 // was name removed or not. Once the name was added to the collection the index 36 // will be allocated for it. To check if name is in collection right now user 37 // has to explicitly call to `HasName` function. 38 class NamesCollection { 39 public: 40 NamesCollection() = default; 41 42 explicit NamesCollection(rtc::ArrayView<const std::string> names); 43 44 // Returns amount of currently presented names in the collection. size()45 size_t size() const { return size_; } 46 47 // Returns amount of all names known to this collection. GetKnownSize()48 size_t GetKnownSize() const { return names_.size(); } 49 50 // Returns index of the `name` which was known to the collection. Crashes 51 // if `name` was never registered in the collection. index(absl::string_view name)52 size_t index(absl::string_view name) const { return index_.at(name); } 53 54 // Returns name which was known to the collection for the specified `index`. 55 // Crashes if there was no any name registered in the collection for such 56 // `index`. name(size_t index)57 const std::string& name(size_t index) const { return names_.at(index); } 58 59 // Returns if `name` is currently presented in this collection. 60 bool HasName(absl::string_view name) const; 61 62 // Adds specified `name` to the collection if it isn't presented. 63 // Returns index which corresponds to specified `name`. 64 size_t AddIfAbsent(absl::string_view name); 65 66 // Removes specified `name` from the collection if it is presented. 67 // 68 // After name was removed, collection size will be decreased, but `name` index 69 // will be preserved. Collection will return false for `HasName(name)`, but 70 // will continue to return previously known index for `index(name)` and return 71 // `name` for `name(index(name))`. 72 // 73 // Returns the index of the removed value or absl::nullopt if no such `name` 74 // registered in the collection. 75 absl::optional<size_t> RemoveIfPresent(absl::string_view name); 76 77 // Returns a set of indexes for all currently present names in the 78 // collection. 79 std::set<size_t> GetPresentIndexes() const; 80 81 // Returns a set of all indexes known to the collection including indexes for 82 // names that were removed. 83 std::set<size_t> GetAllIndexes() const; 84 85 private: 86 std::vector<std::string> names_; 87 std::vector<bool> removed_; 88 std::map<absl::string_view, size_t> index_; 89 size_t size_ = 0; 90 }; 91 92 } // namespace webrtc 93 94 #endif // TEST_PC_E2E_ANALYZER_VIDEO_NAMES_COLLECTION_H_ 95