summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/test/pc/e2e/analyzer/video/names_collection.cc
blob: 3ccab620f8476ea8dbb690ad1748363c4909b035 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "test/pc/e2e/analyzer/video/names_collection.h"

#include <set>

#include "absl/strings/string_view.h"
#include "absl/types/optional.h"

namespace webrtc {

NamesCollection::NamesCollection(rtc::ArrayView<const std::string> names) {
  names_ = std::vector<std::string>(names.begin(), names.end());
  for (size_t i = 0; i < names_.size(); ++i) {
    index_.emplace(names_[i], i);
    removed_.emplace_back(false);
  }
  size_ = names_.size();
}

bool NamesCollection::HasName(absl::string_view name) const {
  auto it = index_.find(name);
  if (it == index_.end()) {
    return false;
  }
  return !removed_[it->second];
}

size_t NamesCollection::AddIfAbsent(absl::string_view name) {
  auto it = index_.find(name);
  if (it != index_.end()) {
    // Name was registered in the collection before: we need to restore it.
    size_t index = it->second;
    if (removed_[index]) {
      removed_[index] = false;
      size_++;
    }
    return index;
  }
  size_t out = names_.size();
  size_t old_capacity = names_.capacity();
  names_.emplace_back(name);
  removed_.emplace_back(false);
  size_++;
  size_t new_capacity = names_.capacity();

  if (old_capacity == new_capacity) {
    index_.emplace(names_[out], out);
  } else {
    // Reallocation happened in the vector, so we need to rebuild `index_` to
    // fix absl::string_view internal references.
    index_.clear();
    for (size_t i = 0; i < names_.size(); ++i) {
      index_.emplace(names_[i], i);
    }
  }
  return out;
}

absl::optional<size_t> NamesCollection::RemoveIfPresent(
    absl::string_view name) {
  auto it = index_.find(name);
  if (it == index_.end()) {
    return absl::nullopt;
  }
  size_t index = it->second;
  if (removed_[index]) {
    return absl::nullopt;
  }
  removed_[index] = true;
  size_--;
  return index;
}

std::set<size_t> NamesCollection::GetPresentIndexes() const {
  std::set<size_t> out;
  for (size_t i = 0; i < removed_.size(); ++i) {
    if (!removed_[i]) {
      out.insert(i);
    }
  }
  return out;
}

std::set<size_t> NamesCollection::GetAllIndexes() const {
  std::set<size_t> out;
  for (size_t i = 0; i < names_.size(); ++i) {
    out.insert(i);
  }
  return out;
}

}  // namespace webrtc