Flutter Windows Embedder
sequential_id_generator.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 namespace flutter {
8 
9 namespace {
10 
11 // Removes |key| from |first|, and |first[key]| from |second|.
12 template <typename T>
13 void Remove(uint32_t key, T* first, T* second) {
14  auto iter = first->find(key);
15  if (iter == first->end())
16  return;
17 
18  uint32_t second_key = iter->second;
19  first->erase(iter);
20 
21  iter = second->find(second_key);
22  second->erase(iter);
23 }
24 
25 } // namespace
26 
27 SequentialIdGenerator::SequentialIdGenerator(uint32_t min_id, uint32_t max_id)
28  : min_id_(min_id), min_available_id_(min_id), max_id_(max_id) {}
29 
31 
32 uint32_t SequentialIdGenerator::GetGeneratedId(uint32_t number) {
33  auto it = number_to_id_.find(number);
34  if (it != number_to_id_.end())
35  return it->second;
36 
37  auto id = GetNextAvailableId();
38  number_to_id_.emplace(number, id);
39  id_to_number_.emplace(id, number);
40  return id;
41 }
42 
43 bool SequentialIdGenerator::HasGeneratedIdFor(uint32_t number) const {
44  return number_to_id_.find(number) != number_to_id_.end();
45 }
46 
47 void SequentialIdGenerator::ReleaseNumber(uint32_t number) {
48  if (number_to_id_.count(number) > 0U) {
49  UpdateNextAvailableIdAfterRelease(number_to_id_[number]);
50  Remove(number, &number_to_id_, &id_to_number_);
51  }
52 }
53 
55  if (id_to_number_.count(id) > 0U) {
56  UpdateNextAvailableIdAfterRelease(id);
57  Remove(id_to_number_[id], &number_to_id_, &id_to_number_);
58  }
59 }
60 
61 uint32_t SequentialIdGenerator::GetNextAvailableId() {
62  while (id_to_number_.count(min_available_id_) > 0 &&
63  min_available_id_ < max_id_) {
64  ++min_available_id_;
65  }
66  if (min_available_id_ >= max_id_)
67  min_available_id_ = min_id_;
68  return min_available_id_;
69 }
70 
71 void SequentialIdGenerator::UpdateNextAvailableIdAfterRelease(uint32_t id) {
72  if (id < min_available_id_) {
73  min_available_id_ = id;
74  }
75 }
76 
77 } // namespace flutter
flutter::SequentialIdGenerator::ReleaseId
void ReleaseId(uint32_t id)
Definition: sequential_id_generator.cc:54
flutter::SequentialIdGenerator::ReleaseNumber
void ReleaseNumber(uint32_t number)
Definition: sequential_id_generator.cc:47
flutter::SequentialIdGenerator::SequentialIdGenerator
SequentialIdGenerator(uint32_t min_id, uint32_t max_id)
Definition: sequential_id_generator.cc:27
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::SequentialIdGenerator::~SequentialIdGenerator
~SequentialIdGenerator()
Definition: sequential_id_generator.cc:30
sequential_id_generator.h
flutter::SequentialIdGenerator::HasGeneratedIdFor
bool HasGeneratedIdFor(uint32_t number) const
Definition: sequential_id_generator.cc:43
flutter::SequentialIdGenerator::GetGeneratedId
uint32_t GetGeneratedId(uint32_t number)
Definition: sequential_id_generator.cc:32
key
int key
Definition: keyboard_key_handler_unittests.cc:114