1 // Copyright (c) 2013 The Chromium 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.
5 #include "ui/gfx/sequential_id_generator.h"
7 #include "base/logging.h"
11 // Removes |key| from |first|, and |first[key]| from |second|.
13 void Remove(uint32 key
, T
* first
, T
* second
) {
14 typename
T::iterator iter
= first
->find(key
);
15 if (iter
== first
->end())
18 uint32 second_key
= iter
->second
;
21 iter
= second
->find(second_key
);
22 DCHECK(iter
!= second
->end());
30 SequentialIDGenerator::SequentialIDGenerator(uint32 min_id
)
32 min_available_id_(min_id
) {
35 SequentialIDGenerator::~SequentialIDGenerator() {
38 uint32
SequentialIDGenerator::GetGeneratedID(uint32 number
) {
39 IDMap::iterator find
= number_to_id_
.find(number
);
40 if (find
!= number_to_id_
.end())
43 int id
= GetNextAvailableID();
44 number_to_id_
.insert(std::make_pair(number
, id
));
45 id_to_number_
.insert(std::make_pair(id
, number
));
49 bool SequentialIDGenerator::HasGeneratedIDFor(uint32 number
) const {
50 return number_to_id_
.find(number
) != number_to_id_
.end();
53 void SequentialIDGenerator::ReleaseGeneratedID(uint32 id
) {
54 UpdateNextAvailableIDAfterRelease(id
);
55 Remove(id
, &id_to_number_
, &number_to_id_
);
58 void SequentialIDGenerator::ReleaseNumber(uint32 number
) {
59 DCHECK_GT(number_to_id_
.count(number
), 0U);
60 UpdateNextAvailableIDAfterRelease(number_to_id_
[number
]);
61 Remove(number
, &number_to_id_
, &id_to_number_
);
64 uint32
SequentialIDGenerator::GetNextAvailableID() {
65 const uint32 kMaxID
= 128;
66 while (id_to_number_
.count(min_available_id_
) > 0 &&
67 min_available_id_
< kMaxID
) {
70 if (min_available_id_
>= kMaxID
)
71 min_available_id_
= min_id_
;
72 return min_available_id_
;
75 void SequentialIDGenerator::UpdateNextAvailableIDAfterRelease(uint32 id
) {
76 if (id
< min_available_id_
) {
77 min_available_id_
= id
;
78 DCHECK_GE(min_available_id_
, min_id_
);