phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef BASE_ID_MAP_H_ |
| 6 | #define BASE_ID_MAP_H_ |
| 7 | |
| 8 | #include <set> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/hash_tables.h" |
| 12 | #include "base/logging.h" |
| 13 | |
| 14 | // This object maintains a list of IDs that can be quickly converted to |
| 15 | // pointers to objects. It is implemented as a hash table, optimized for |
| 16 | // relatively small data sets (in the common case, there will be exactly one |
| 17 | // item in the list). |
| 18 | // |
| 19 | // Items can be inserted into the container with arbitrary ID, but the caller |
| 20 | // must ensure they are unique. Inserting IDs and relying on automatically |
| 21 | // generated ones is not allowed because they can collide. |
| 22 | template<class T> |
| 23 | class IDMap { |
| 24 | private: |
| 25 | typedef base::hash_map<int32, T*> HashTable; |
| 26 | |
| 27 | public: |
| 28 | IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
| 29 | } |
| 30 | |
| 31 | // Sets whether Add should CHECK if passed in NULL data. Default is false. |
| 32 | void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
| 33 | |
| 34 | // Adds a view with an automatically generated unique ID. See AddWithID. |
| 35 | int32 Add(T* data) { |
| 36 | CHECK(!check_on_null_data_ || data); |
| 37 | int32 this_id = next_id_; |
| 38 | DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
| 39 | data_[this_id] = data; |
| 40 | next_id_++; |
| 41 | return this_id; |
| 42 | } |
| 43 | |
| 44 | // Adds a new data member with the specified ID. The ID must not be in |
| 45 | // the list. The caller either must generate all unique IDs itself and use |
| 46 | // this function, or allow this object to generate IDs and call Add. These |
| 47 | // two methods may not be mixed, or duplicate IDs may be generated |
| 48 | void AddWithID(T* data, int32 id) { |
| 49 | CHECK(!check_on_null_data_ || data); |
| 50 | DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| 51 | data_[id] = data; |
| 52 | } |
| 53 | |
| 54 | void Remove(int32 id) { |
| 55 | typename HashTable::iterator i = data_.find(id); |
| 56 | if (i == data_.end()) { |
| 57 | NOTREACHED() << "Attempting to remove an item not in the list"; |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if (iteration_depth_ == 0) |
| 62 | data_.erase(i); |
| 63 | else |
| 64 | removed_ids_.insert(id); |
| 65 | } |
| 66 | |
| 67 | bool IsEmpty() const { |
| 68 | return data_.empty(); |
| 69 | } |
| 70 | |
| 71 | T* Lookup(int32 id) const { |
| 72 | typename HashTable::const_iterator i = data_.find(id); |
| 73 | if (i == data_.end()) |
| 74 | return NULL; |
| 75 | return i->second; |
| 76 | } |
| 77 | |
| 78 | size_t size() const { |
| 79 | return data_.size(); |
| 80 | } |
| 81 | |
| 82 | // It is safe to remove elements from the map during iteration. All iterators |
| 83 | // will remain valid. |
| 84 | template<class ReturnType> |
| 85 | class Iterator { |
| 86 | public: |
| 87 | Iterator(IDMap<T>* map) |
| 88 | : map_(map), |
| 89 | iter_(map_->data_.begin()) { |
| 90 | ++map_->iteration_depth_; |
| 91 | SkipRemovedEntries(); |
| 92 | } |
| 93 | |
| 94 | ~Iterator() { |
| 95 | if (--map_->iteration_depth_ == 0) |
| 96 | map_->Compact(); |
| 97 | } |
| 98 | |
| 99 | bool IsAtEnd() const { |
| 100 | return iter_ == map_->data_.end(); |
| 101 | } |
| 102 | |
| 103 | int32 GetCurrentKey() const { |
| 104 | return iter_->first; |
| 105 | } |
| 106 | |
| 107 | ReturnType* GetCurrentValue() const { |
| 108 | return iter_->second; |
| 109 | } |
| 110 | |
| 111 | void Advance() { |
| 112 | ++iter_; |
| 113 | SkipRemovedEntries(); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | void SkipRemovedEntries() { |
| 118 | while (iter_ != map_->data_.end() && |
| 119 | map_->removed_ids_.find(iter_->first) != |
| 120 | map_->removed_ids_.end()) { |
| 121 | ++iter_; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | IDMap<T>* map_; |
| 126 | typename HashTable::const_iterator iter_; |
| 127 | }; |
| 128 | |
| 129 | typedef Iterator<T> iterator; |
| 130 | typedef Iterator<const T> const_iterator; |
| 131 | |
| 132 | private: |
| 133 | void Compact() { |
| 134 | DCHECK_EQ(0, iteration_depth_); |
| 135 | for (std::set<int32>::const_iterator i = removed_ids_.begin(); |
| 136 | i != removed_ids_.end(); ++i) { |
| 137 | Remove(*i); |
| 138 | } |
| 139 | removed_ids_.clear(); |
| 140 | } |
| 141 | |
| 142 | // Keep track of how many iterators are currently iterating on us to safely |
| 143 | // handle removing items during iteration. |
| 144 | int iteration_depth_; |
| 145 | |
| 146 | // Keep set of IDs that should be removed after the outermost iteration has |
| 147 | // finished. This way we manage to not invalidate the iterator when an element |
| 148 | // is removed. |
| 149 | std::set<int32> removed_ids_; |
| 150 | |
| 151 | // The next ID that we will return from Add() |
| 152 | int32 next_id_; |
| 153 | |
| 154 | HashTable data_; |
| 155 | |
| 156 | // See description above setter. |
| 157 | bool check_on_null_data_; |
| 158 | |
| 159 | DISALLOW_COPY_AND_ASSIGN(IDMap); |
| 160 | }; |
| 161 | |
| 162 | #endif // BASE_ID_MAP_H_ |