summaryrefslogtreecommitdiffstats
path: root/ui/base/sequential_id_generator.cc
blob: ac3b1a0ed41d0f1bf45e18a8effdcfdfbba181ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/sequential_id_generator.h"

#include "base/logging.h"

namespace {

// Removes |key| from |first|, and |first[key]| from |second|.
template<typename T>
void Remove(uint32 key, T* first, T* second) {
  typename T::iterator iter = first->find(key);
  if (iter == first->end())
    return;

  uint32 second_key = iter->second;
  first->erase(iter);

  iter = second->find(second_key);
  DCHECK(iter != second->end());
  second->erase(iter);
}

}  // namespace

namespace ui {

SequentialIDGenerator::SequentialIDGenerator(uint32 min_id)
    : min_id_(min_id),
      min_available_id_(min_id) {
}

SequentialIDGenerator::~SequentialIDGenerator() {
}

uint32 SequentialIDGenerator::GetGeneratedID(uint32 number) {
  IDMap::iterator find = number_to_id_.find(number);
  if (find != number_to_id_.end())
    return find->second;

  int id = GetNextAvailableID();
  number_to_id_.insert(std::make_pair(number, id));
  id_to_number_.insert(std::make_pair(id, number));
  return id;
}

bool SequentialIDGenerator::HasGeneratedIDFor(uint32 number) const {
  return number_to_id_.find(number) != number_to_id_.end();
}

void SequentialIDGenerator::ReleaseGeneratedID(uint32 id) {
  UpdateNextAvailableIDAfterRelease(id);
  Remove(id, &id_to_number_, &number_to_id_);
}

void SequentialIDGenerator::ReleaseNumber(uint32 number) {
  DCHECK_GT(number_to_id_.count(number), 0U);
  UpdateNextAvailableIDAfterRelease(number_to_id_[number]);
  Remove(number, &number_to_id_, &id_to_number_);
}

uint32 SequentialIDGenerator::GetNextAvailableID() {
  const uint32 kMaxID = 128;
  while (id_to_number_.count(min_available_id_) > 0 &&
         min_available_id_ < kMaxID) {
    ++min_available_id_;
  }
  if (min_available_id_ >= kMaxID)
    min_available_id_ = min_id_;
  return min_available_id_;
}

void SequentialIDGenerator::UpdateNextAvailableIDAfterRelease(uint32 id) {
  if (id < min_available_id_) {
    min_available_id_ = id;
    DCHECK_GE(min_available_id_, min_id_);
  }
}

}  // namespace ui