mojo: Fix map of booleans for python bindings.
[chromium-blink-merge.git] / ui / gfx / sequential_id_generator.cc
blob4b9c8de74287b8e64c161577fa7b5dbda247cc2f
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"
9 namespace {
11 // Removes |key| from |first|, and |first[key]| from |second|.
12 template<typename T>
13 void Remove(uint32 key, T* first, T* second) {
14 typename T::iterator iter = first->find(key);
15 if (iter == first->end())
16 return;
18 uint32 second_key = iter->second;
19 first->erase(iter);
21 iter = second->find(second_key);
22 DCHECK(iter != second->end());
23 second->erase(iter);
26 } // namespace
28 namespace ui {
30 SequentialIDGenerator::SequentialIDGenerator(uint32 min_id)
31 : min_id_(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())
41 return find->second;
43 int id = GetNextAvailableID();
44 number_to_id_.insert(std::make_pair(number, id));
45 id_to_number_.insert(std::make_pair(id, number));
46 return id;
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 void SequentialIDGenerator::ResetForTest() {
65 number_to_id_.clear();
66 id_to_number_.clear();
67 min_available_id_ = min_id_;
70 uint32 SequentialIDGenerator::GetNextAvailableID() {
71 const uint32 kMaxID = 128;
72 while (id_to_number_.count(min_available_id_) > 0 &&
73 min_available_id_ < kMaxID) {
74 ++min_available_id_;
76 if (min_available_id_ >= kMaxID)
77 min_available_id_ = min_id_;
78 return min_available_id_;
81 void SequentialIDGenerator::UpdateNextAvailableIDAfterRelease(uint32 id) {
82 if (id < min_available_id_) {
83 min_available_id_ = id;
84 DCHECK_GE(min_available_id_, min_id_);
88 } // namespace ui