Flutter Engine
The Flutter Engine
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
5#include "flutter/shell/platform/windows/sequential_id_generator.h"
6
7namespace flutter {
8
9namespace {
10
11// Removes |key| from |first|, and |first[key]| from |second|.
12template <typename T>
13void 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
27SequentialIdGenerator::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
32uint32_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
43bool SequentialIdGenerator::HasGeneratedIdFor(uint32_t number) const {
44 return number_to_id_.find(number) != number_to_id_.end();
45}
46
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
61uint32_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
71void SequentialIdGenerator::UpdateNextAvailableIdAfterRelease(uint32_t id) {
72 if (id < min_available_id_) {
73 min_available_id_ = id;
74 }
75}
76
77} // namespace flutter
bool HasGeneratedIdFor(uint32_t number) const
SequentialIdGenerator(uint32_t min_id, uint32_t max_id)
uint32_t GetGeneratedId(uint32_t number)
#define T
Definition: precompiler.cc:65
const uintptr_t id