scoped_ptr_hash_map.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 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.
  4. #ifndef BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_
  5. #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_
  6. #include <algorithm>
  7. #include <utility>
  8. #include "base/basictypes.h"
  9. #include "base/containers/hash_tables.h"
  10. #include "base/logging.h"
  11. #include "base/memory/scoped_ptr.h"
  12. #include "base/stl_util.h"
  13. namespace base {
  14. // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of
  15. // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data
  16. // structure.
  17. template <typename Key, typename Value>
  18. class ScopedPtrHashMap {
  19. typedef base::hash_map<Key, Value*> Container;
  20. public:
  21. typedef typename Container::key_type key_type;
  22. typedef typename Container::mapped_type mapped_type;
  23. typedef typename Container::value_type value_type;
  24. typedef typename Container::iterator iterator;
  25. typedef typename Container::const_iterator const_iterator;
  26. ScopedPtrHashMap() {}
  27. ~ScopedPtrHashMap() { clear(); }
  28. void swap(ScopedPtrHashMap<Key, Value>& other) {
  29. data_.swap(other.data_);
  30. }
  31. // Replaces value but not key if key is already present.
  32. iterator set(const Key& key, scoped_ptr<Value> data) {
  33. iterator it = find(key);
  34. if (it != end()) {
  35. delete it->second;
  36. it->second = data.release();
  37. return it;
  38. }
  39. return data_.insert(std::make_pair(key, data.release())).first;
  40. }
  41. // Does nothing if key is already present
  42. std::pair<iterator, bool> add(const Key& key, scoped_ptr<Value> data) {
  43. std::pair<iterator, bool> result =
  44. data_.insert(std::make_pair(key, data.get()));
  45. if (result.second)
  46. ignore_result(data.release());
  47. return result;
  48. }
  49. void erase(iterator it) {
  50. delete it->second;
  51. data_.erase(it);
  52. }
  53. size_t erase(const Key& k) {
  54. iterator it = data_.find(k);
  55. if (it == data_.end())
  56. return 0;
  57. erase(it);
  58. return 1;
  59. }
  60. scoped_ptr<Value> take(iterator it) {
  61. DCHECK(it != data_.end());
  62. if (it == data_.end())
  63. return scoped_ptr<Value>();
  64. scoped_ptr<Value> ret(it->second);
  65. it->second = NULL;
  66. return ret.Pass();
  67. }
  68. scoped_ptr<Value> take(const Key& k) {
  69. iterator it = find(k);
  70. if (it == data_.end())
  71. return scoped_ptr<Value>();
  72. return take(it);
  73. }
  74. scoped_ptr<Value> take_and_erase(iterator it) {
  75. DCHECK(it != data_.end());
  76. if (it == data_.end())
  77. return scoped_ptr<Value>();
  78. scoped_ptr<Value> ret(it->second);
  79. data_.erase(it);
  80. return ret.Pass();
  81. }
  82. scoped_ptr<Value> take_and_erase(const Key& k) {
  83. iterator it = find(k);
  84. if (it == data_.end())
  85. return scoped_ptr<Value>();
  86. return take_and_erase(it);
  87. }
  88. // Returns the element in the hash_map that matches the given key.
  89. // If no such element exists it returns NULL.
  90. Value* get(const Key& k) const {
  91. const_iterator it = find(k);
  92. if (it == end())
  93. return NULL;
  94. return it->second;
  95. }
  96. inline bool contains(const Key& k) const { return data_.count(k) > 0; }
  97. inline void clear() { STLDeleteValues(&data_); }
  98. inline const_iterator find(const Key& k) const { return data_.find(k); }
  99. inline iterator find(const Key& k) { return data_.find(k); }
  100. inline size_t count(const Key& k) const { return data_.count(k); }
  101. inline std::pair<const_iterator, const_iterator> equal_range(
  102. const Key& k) const {
  103. return data_.equal_range(k);
  104. }
  105. inline std::pair<iterator, iterator> equal_range(const Key& k) {
  106. return data_.equal_range(k);
  107. }
  108. inline size_t size() const { return data_.size(); }
  109. inline size_t max_size() const { return data_.max_size(); }
  110. inline bool empty() const { return data_.empty(); }
  111. inline size_t bucket_count() const { return data_.bucket_count(); }
  112. inline void resize(size_t size) { return data_.resize(size); }
  113. inline iterator begin() { return data_.begin(); }
  114. inline const_iterator begin() const { return data_.begin(); }
  115. inline iterator end() { return data_.end(); }
  116. inline const_iterator end() const { return data_.end(); }
  117. private:
  118. Container data_;
  119. DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap);
  120. };
  121. } // namespace base
  122. #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_