Tweak/Wontfix a few tests that we could care less about.
[chromium-blink-merge.git] / base / id_map.h
blob00ef16a2798aa8291eb54cf22503e1df96848357
1 // Copyright (c) 2006-2008 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 #ifndef BASE_ID_MAP_H_
6 #define BASE_ID_MAP_H_
8 #include <set>
10 #include "base/basictypes.h"
11 #include "base/hash_tables.h"
12 #include "base/logging.h"
14 // Ownership semantics - own pointer means the pointer is deleted in Remove()
15 // & during destruction
16 enum IDMapOwnershipSemantics {
17 IDMapExternalPointer,
18 IDMapOwnPointer
21 // This object maintains a list of IDs that can be quickly converted to
22 // pointers to objects. It is implemented as a hash table, optimized for
23 // relatively small data sets (in the common case, there will be exactly one
24 // item in the list).
26 // Items can be inserted into the container with arbitrary ID, but the caller
27 // must ensure they are unique. Inserting IDs and relying on automatically
28 // generated ones is not allowed because they can collide.
30 // This class does not have a virtual destructor, do not inherit from it when
31 // ownership semantics are set to own because pointers will leak.
32 template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer>
33 class IDMap {
34 private:
35 typedef int32 KeyType;
36 typedef base::hash_map<KeyType, T*> HashTable;
38 public:
39 IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) {
42 ~IDMap() {
43 Releaser<OS, 0>::release_all(&data_);
46 // Sets whether Add should CHECK if passed in NULL data. Default is false.
47 void set_check_on_null_data(bool value) { check_on_null_data_ = value; }
49 // Adds a view with an automatically generated unique ID. See AddWithID.
50 KeyType Add(T* data) {
51 CHECK(!check_on_null_data_ || data);
52 KeyType this_id = next_id_;
53 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
54 data_[this_id] = data;
55 next_id_++;
56 return this_id;
59 // Adds a new data member with the specified ID. The ID must not be in
60 // the list. The caller either must generate all unique IDs itself and use
61 // this function, or allow this object to generate IDs and call Add. These
62 // two methods may not be mixed, or duplicate IDs may be generated
63 void AddWithID(T* data, KeyType id) {
64 CHECK(!check_on_null_data_ || data);
65 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
66 data_[id] = data;
69 void Remove(KeyType id) {
70 typename HashTable::iterator i = data_.find(id);
71 if (i == data_.end()) {
72 NOTREACHED() << "Attempting to remove an item not in the list";
73 return;
76 if (iteration_depth_ == 0) {
77 Releaser<OS, 0>::release(i->second);
78 data_.erase(i);
79 } else {
80 removed_ids_.insert(id);
84 bool IsEmpty() const {
85 return size() == 0u;
88 T* Lookup(KeyType id) const {
89 typename HashTable::const_iterator i = data_.find(id);
90 if (i == data_.end())
91 return NULL;
92 return i->second;
95 size_t size() const {
96 return data_.size() - removed_ids_.size();
99 // It is safe to remove elements from the map during iteration. All iterators
100 // will remain valid.
101 template<class ReturnType>
102 class Iterator {
103 public:
104 Iterator(IDMap<T, OS>* map)
105 : map_(map),
106 iter_(map_->data_.begin()) {
107 ++map_->iteration_depth_;
108 SkipRemovedEntries();
111 ~Iterator() {
112 if (--map_->iteration_depth_ == 0)
113 map_->Compact();
116 bool IsAtEnd() const {
117 return iter_ == map_->data_.end();
120 KeyType GetCurrentKey() const {
121 return iter_->first;
124 ReturnType* GetCurrentValue() const {
125 return iter_->second;
128 void Advance() {
129 ++iter_;
130 SkipRemovedEntries();
133 private:
134 void SkipRemovedEntries() {
135 while (iter_ != map_->data_.end() &&
136 map_->removed_ids_.find(iter_->first) !=
137 map_->removed_ids_.end()) {
138 ++iter_;
142 IDMap<T, OS>* map_;
143 typename HashTable::const_iterator iter_;
146 typedef Iterator<T> iterator;
147 typedef Iterator<const T> const_iterator;
149 private:
151 // The dummy parameter is there because C++ standard does not allow
152 // explicitly specialized templates inside classes
153 template<IDMapOwnershipSemantics OI, int dummy> struct Releaser {
154 static inline void release(T* ptr) {}
155 static inline void release_all(HashTable* table) {}
158 template<int dummy> struct Releaser<IDMapOwnPointer, dummy> {
159 static inline void release(T* ptr) { delete ptr;}
160 static inline void release_all(HashTable* table) {
161 for (typename HashTable::iterator i = table->begin();
162 i != table->end(); ++i) {
163 delete i->second;
165 table->clear();
169 void Compact() {
170 DCHECK_EQ(0, iteration_depth_);
171 for (std::set<KeyType>::const_iterator i = removed_ids_.begin();
172 i != removed_ids_.end(); ++i) {
173 Remove(*i);
175 removed_ids_.clear();
178 // Keep track of how many iterators are currently iterating on us to safely
179 // handle removing items during iteration.
180 int iteration_depth_;
182 // Keep set of IDs that should be removed after the outermost iteration has
183 // finished. This way we manage to not invalidate the iterator when an element
184 // is removed.
185 std::set<KeyType> removed_ids_;
187 // The next ID that we will return from Add()
188 KeyType next_id_;
190 HashTable data_;
192 // See description above setter.
193 bool check_on_null_data_;
195 DISALLOW_COPY_AND_ASSIGN(IDMap);
198 #endif // BASE_ID_MAP_H_