Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 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 TOOLS_GN_POINTER_SET_H_ | |
| 6 #define TOOLS_GN_POINTER_SET_H_ | |
| 7 | |
| 8 #include <functional> | |
| 9 #include <set> | |
| 10 | |
| 11 namespace internal { | |
| 12 | |
| 13 // Comparator that dereferences the pointers before comparing them. | |
| 14 template <typename T, typename Comparator> | |
| 15 class DereferenceComparator { | |
|
mithro-old
2015/12/03 05:20:31
Does something like this already exist in stl at a
Zachary Forman
2015/12/03 07:27:02
Nope. Which is a shame as it's sometimes useful.
| |
| 16 public: | |
| 17 // Constructs a DereferenceComparator that uses the provided comparator. | |
| 18 DereferenceComparator(Comparator comparator) : comparator(comparator) {} | |
| 19 | |
| 20 // Compares the values pointed to by a and b with the comparator. | |
| 21 bool operator()(const T* a, const T* b) const { return comparator(*a, *b); } | |
| 22 | |
| 23 private: | |
| 24 Comparator comparator; | |
| 25 }; | |
| 26 | |
| 27 } // namespace internal | |
| 28 | |
| 29 // A set of pointers that are ordered by what they point at. | |
| 30 // Takes the type that is pointed to as its first template parameter, | |
| 31 // and optionally a second template parameter to define the ordering, | |
| 32 // which is - by default - the class's operator<. | |
| 33 // The comparator should be default constructible. | |
| 34 // In most ways, matches the interface of a std::set. | |
| 35 template <typename T, typename Comparator = std::less<T>> | |
|
mithro-old
2015/12/03 05:20:31
Do we actually need a full wrapper class here? Cou
Zachary Forman
2015/12/03 07:27:02
Yep - I've factored it down to a much smaller thin
| |
| 36 class PointerSet { | |
| 37 public: | |
| 38 // Convenience typedefs. | |
| 39 typedef internal::DereferenceComparator<T, Comparator> DereferenceComparator; | |
| 40 typedef std::set<T*, DereferenceComparator> Set; | |
| 41 typedef typename Set::iterator iterator; | |
| 42 typedef typename Set::const_iterator const_iterator; | |
| 43 | |
| 44 explicit PointerSet(const Comparator& comparator = Comparator()) | |
| 45 : set_(comparator) {} | |
| 46 | |
| 47 template <typename InputIterator> | |
| 48 PointerSet(InputIterator first, | |
| 49 InputIterator last, | |
| 50 const Comparator& comparator = Comparator()) | |
| 51 : set_(first, last, comparator) {} | |
| 52 | |
| 53 PointerSet(const PointerSet& set) : set_(set.set()) {} | |
| 54 | |
| 55 PointerSet(PointerSet&& set) : set_(set.set()) {} | |
| 56 | |
| 57 const Set& set() const { return set_; } | |
| 58 Set&& set() { return std::move(set_); } | |
| 59 | |
| 60 // Forward most of the std::set's methods. | |
| 61 const_iterator cbegin() const { return set_.cbegin(); } | |
| 62 const_iterator begin() const { return set_.cbegin(); } | |
| 63 iterator begin() { return set_.begin(); } | |
| 64 const_iterator cend() const { return set_.cend(); } | |
| 65 const_iterator end() const { return set_.cend(); } | |
| 66 iterator end() { return set_.end(); } | |
| 67 const_iterator crbegin() const { return set_.crbegin(); } | |
| 68 const_iterator rbegin() const { return set_.crbegin(); } | |
| 69 iterator rbegin() { return set_.rbegin(); } | |
| 70 const_iterator crend() const { return set_.crend(); } | |
| 71 const_iterator rend() const { return set_.crend(); } | |
| 72 iterator rend() { return set_.rend(); } | |
| 73 | |
| 74 bool empty() const { return set_.empty(); } | |
| 75 size_t size() const { return set_.size(); } | |
| 76 size_t max_size() const { return set_.max_size(); } | |
| 77 | |
| 78 std::pair<iterator, bool> insert(T* val) { return set_.insert(val); } | |
| 79 | |
| 80 iterator insert(const_iterator position, T* val) { | |
| 81 return set_.insert(position, val); | |
| 82 } | |
| 83 | |
| 84 template <typename InputIterator> | |
| 85 void insert(InputIterator first, InputIterator last) { | |
| 86 set_.insert(first, last); | |
| 87 } | |
| 88 | |
| 89 iterator erase(const_iterator position) { return set_.erase(position); } | |
| 90 | |
| 91 size_t erase(const T* val) { return set_.erase(val); } | |
| 92 | |
| 93 iterator erase(const_iterator first, const_iterator last) { | |
| 94 return set_.erase(first, last); | |
| 95 } | |
| 96 | |
| 97 void swap(PointerSet& s) { set_.swap(s.set()); } | |
| 98 void clear() noexcept { set_.clear(); } | |
| 99 | |
| 100 const_iterator find(const T* val) const { return set_.find(val); } | |
| 101 iterator find(const T* val) { return set_.find(val); } | |
| 102 | |
| 103 size_t count(const T* val) const { return set_.count(val); } | |
| 104 | |
| 105 const_iterator lower_bound(const T* val) const { | |
| 106 return set_.lower_bound(val); | |
| 107 } | |
| 108 | |
| 109 iterator lower_bound(const T* val) { return set_.lower_bound(val); } | |
| 110 | |
| 111 const_iterator upper_bound(const T* val) const { | |
| 112 return set_.upper_bound(val); | |
| 113 } | |
| 114 | |
| 115 iterator upper_bound(const T* val) { return set_.upper_bound(val); } | |
| 116 | |
| 117 std::pair<const_iterator, const_iterator> equal_range(const T* val) const { | |
| 118 return set_.equal_range(val); | |
| 119 } | |
| 120 | |
| 121 std::pair<iterator, iterator> equal_range(const T* val) { | |
| 122 return set_.equal_range(val); | |
| 123 } | |
| 124 | |
| 125 private: | |
| 126 // The underlying std::set that this class references. | |
| 127 std::set<T*, DereferenceComparator> set_; | |
| 128 }; | |
| 129 | |
| 130 #endif // TOOLS_GN_POINTER_SET_H_ | |
| OLD | NEW |