Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CC_BASE_SCOPED_PTR_VECTOR_H_ | 5 #ifndef CC_BASE_SCOPED_PTR_VECTOR_H_ |
| 6 #define CC_BASE_SCOPED_PTR_VECTOR_H_ | 6 #define CC_BASE_SCOPED_PTR_VECTOR_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 void swap(iterator a, iterator b) { | 152 void swap(iterator a, iterator b) { |
| 153 DCHECK(a < end()); | 153 DCHECK(a < end()); |
| 154 DCHECK(b < end()); | 154 DCHECK(b < end()); |
| 155 if (a == end() || b == end() || a == b) | 155 if (a == end() || b == end() || a == b) |
| 156 return; | 156 return; |
| 157 typename std::vector<T*>::iterator writable_a = a; | 157 typename std::vector<T*>::iterator writable_a = a; |
| 158 typename std::vector<T*>::iterator writable_b = b; | 158 typename std::vector<T*>::iterator writable_b = b; |
| 159 std::swap(*writable_a, *writable_b); | 159 std::swap(*writable_a, *writable_b); |
| 160 } | 160 } |
| 161 | 161 |
| 162 // This acts like std::remove_if but with one key difference. The values to be | |
| 163 // removed to will each appear exactly once at or after the returned iterator, | |
| 164 // so that erase(foo.remove_if(P), foo.end()) will not leak or double-free the | |
| 165 // pointers in the vector. | |
| 166 template <typename Predicate> | |
| 167 iterator remove_if(Predicate predicate) { | |
|
vmpstr
2014/12/09 00:10:19
Gone after rebase?
danakj
2014/12/09 18:20:22
Yep
| |
| 168 typename std::vector<T*>::iterator it = | |
| 169 std::find_if(data_.begin(), data_.end(), predicate); | |
| 170 typename std::vector<T*>::iterator end = data_.end(); | |
| 171 if (it == end) | |
| 172 return it; | |
| 173 typename std::vector<T*>::iterator result = it; | |
| 174 ++it; | |
| 175 for (; it != end; ++it) { | |
| 176 if (!static_cast<bool>(predicate(*it))) { | |
| 177 // Swap here instead of just assign to |result| so that all the | |
| 178 // pointers are preserved to be deleted afterward. | |
| 179 std::swap(*result, *it); | |
| 180 ++result; | |
| 181 } | |
| 182 } | |
| 183 return result; | |
| 184 } | |
| 185 | |
| 162 template<class Compare> | 186 template<class Compare> |
| 163 inline void sort(Compare comp) { | 187 inline void sort(Compare comp) { |
| 164 std::sort(data_.begin(), data_.end(), comp); | 188 std::sort(data_.begin(), data_.end(), comp); |
| 165 } | 189 } |
| 166 | 190 |
| 167 template <class Compare> | 191 template <class Compare> |
| 168 inline void make_heap(Compare comp) { | 192 inline void make_heap(Compare comp) { |
| 169 std::make_heap(data_.begin(), data_.end(), comp); | 193 std::make_heap(data_.begin(), data_.end(), comp); |
| 170 } | 194 } |
| 171 | 195 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 191 | 215 |
| 192 private: | 216 private: |
| 193 std::vector<T*> data_; | 217 std::vector<T*> data_; |
| 194 | 218 |
| 195 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); | 219 DISALLOW_COPY_AND_ASSIGN(ScopedPtrVector); |
| 196 }; | 220 }; |
| 197 | 221 |
| 198 } // namespace cc | 222 } // namespace cc |
| 199 | 223 |
| 200 #endif // CC_BASE_SCOPED_PTR_VECTOR_H_ | 224 #endif // CC_BASE_SCOPED_PTR_VECTOR_H_ |
| OLD | NEW |