Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: ui/base/sequential_id_generator.cc

Issue 23654045: ui: Fix removing a number from the SequentialIDGenerator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/sequential_id_generator.h ('k') | ui/base/sequential_id_generator_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/sequential_id_generator.h" 5 #include "ui/base/sequential_id_generator.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace { 9 namespace {
10 10
(...skipping 22 matching lines...) Expand all
33 } 33 }
34 34
35 SequentialIDGenerator::~SequentialIDGenerator() { 35 SequentialIDGenerator::~SequentialIDGenerator() {
36 } 36 }
37 37
38 uint32 SequentialIDGenerator::GetGeneratedID(uint32 number) { 38 uint32 SequentialIDGenerator::GetGeneratedID(uint32 number) {
39 IDMap::iterator find = number_to_id_.find(number); 39 IDMap::iterator find = number_to_id_.find(number);
40 if (find != number_to_id_.end()) 40 if (find != number_to_id_.end())
41 return find->second; 41 return find->second;
42 42
43 int id = GetNextAvailableSlot(); 43 int id = GetNextAvailableID();
44 number_to_id_.insert(std::make_pair(number, id)); 44 number_to_id_.insert(std::make_pair(number, id));
45 id_to_number_.insert(std::make_pair(id, number)); 45 id_to_number_.insert(std::make_pair(id, number));
46 return id; 46 return id;
47 } 47 }
48 48
49 bool SequentialIDGenerator::HasGeneratedIDFor(uint32 number) const { 49 bool SequentialIDGenerator::HasGeneratedIDFor(uint32 number) const {
50 return number_to_id_.find(number) != number_to_id_.end(); 50 return number_to_id_.find(number) != number_to_id_.end();
51 } 51 }
52 52
53 void SequentialIDGenerator::ReleaseGeneratedID(uint32 id) { 53 void SequentialIDGenerator::ReleaseGeneratedID(uint32 id) {
54 if (id < min_available_id_) { 54 UpdateNextAvailableIDAfterRelease(id);
55 min_available_id_ = id;
56 DCHECK_GE(min_available_id_, min_id_);
57 }
58 Remove(id, &id_to_number_, &number_to_id_); 55 Remove(id, &id_to_number_, &number_to_id_);
59 } 56 }
60 57
61 void SequentialIDGenerator::ReleaseNumber(uint32 number) { 58 void SequentialIDGenerator::ReleaseNumber(uint32 number) {
59 DCHECK_GT(number_to_id_.count(number), 0U);
60 UpdateNextAvailableIDAfterRelease(number_to_id_[number]);
62 Remove(number, &number_to_id_, &id_to_number_); 61 Remove(number, &number_to_id_, &id_to_number_);
63 } 62 }
64 63
65 uint32 SequentialIDGenerator::GetNextAvailableSlot() { 64 uint32 SequentialIDGenerator::GetNextAvailableID() {
66 const uint32 kMaxID = 128; 65 const uint32 kMaxID = 128;
67 while (id_to_number_.count(min_available_id_) > 0 && 66 while (id_to_number_.count(min_available_id_) > 0 &&
68 min_available_id_ < kMaxID) { 67 min_available_id_ < kMaxID) {
69 ++min_available_id_; 68 ++min_available_id_;
70 } 69 }
71 if (min_available_id_ >= kMaxID) 70 if (min_available_id_ >= kMaxID)
72 min_available_id_ = min_id_; 71 min_available_id_ = min_id_;
73 return min_available_id_; 72 return min_available_id_;
74 } 73 }
75 74
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_);
79 }
80 }
81
76 } // namespace ui 82 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/sequential_id_generator.h ('k') | ui/base/sequential_id_generator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698