| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_INTRUSIVE_HEAP_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_INTRUSIVE_HEAP_H_ |
| 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_INTRUSIVE_HEAP_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_INTRUSIVE_HEAP_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 const T* end() const { return begin() + size_; } | 122 const T* end() const { return begin() + size_; } |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 enum { | 125 enum { |
| 126 // The majority of sets in the scheduler have 0-3 items in them (a few will | 126 // The majority of sets in the scheduler have 0-3 items in them (a few will |
| 127 // have perhaps up to 100), so this means we usually only have to allocate | 127 // have perhaps up to 100), so this means we usually only have to allocate |
| 128 // memory once. | 128 // memory once. |
| 129 kMinimumHeapSize = 4u | 129 kMinimumHeapSize = 4u |
| 130 }; | 130 }; |
| 131 | 131 |
| 132 friend class IntrusiveHeapTest; |
| 133 |
| 132 size_t MoveHole(size_t new_hole_pos, size_t old_hole_pos) { | 134 size_t MoveHole(size_t new_hole_pos, size_t old_hole_pos) { |
| 133 DCHECK_GT(new_hole_pos, 0u); | 135 DCHECK_GT(new_hole_pos, 0u); |
| 134 DCHECK_LE(new_hole_pos, size_); | 136 DCHECK_LE(new_hole_pos, size_); |
| 135 DCHECK_GT(new_hole_pos, 0u); | 137 DCHECK_GT(new_hole_pos, 0u); |
| 136 DCHECK_LE(new_hole_pos, size_); | 138 DCHECK_LE(new_hole_pos, size_); |
| 137 DCHECK_NE(old_hole_pos, new_hole_pos); | 139 DCHECK_NE(old_hole_pos, new_hole_pos); |
| 138 nodes_[old_hole_pos] = std::move(nodes_[new_hole_pos]); | 140 nodes_[old_hole_pos] = std::move(nodes_[new_hole_pos]); |
| 139 nodes_[old_hole_pos].SetHeapHandle(HeapHandle(old_hole_pos)); | 141 nodes_[old_hole_pos].SetHeapHandle(HeapHandle(old_hole_pos)); |
| 140 return new_hole_pos; | 142 return new_hole_pos; |
| 141 } | 143 } |
| 142 | 144 |
| 143 // Notionally creates a hole in the tree at |index|. | 145 // Notionally creates a hole in the tree at |index|. |
| 144 void MakeHole(size_t index) { | 146 void MakeHole(size_t index) { |
| 145 DCHECK_GT(index, 0u); | 147 DCHECK_GT(index, 0u); |
| 146 DCHECK_LE(index, size_); | 148 DCHECK_LE(index, size_); |
| 147 nodes_[index].ClearHeapHandle(); | 149 nodes_[index].ClearHeapHandle(); |
| 148 } | 150 } |
| 149 | 151 |
| 150 void FillHole(size_t hole, T&& element) { | 152 void FillHole(size_t hole, T&& element) { |
| 151 DCHECK_GT(hole, 0u); | 153 DCHECK_GT(hole, 0u); |
| 152 DCHECK_LE(hole, size_); | 154 DCHECK_LE(hole, size_); |
| 153 nodes_[hole] = std::move(element); | 155 nodes_[hole] = std::move(element); |
| 154 nodes_[hole].SetHeapHandle(HeapHandle(hole)); | 156 nodes_[hole].SetHeapHandle(HeapHandle(hole)); |
| 155 DCHECK(std::is_heap(begin(), end(), CompareNodes)); | 157 DCHECK(std::is_heap(begin(), end(), CompareNodes)); |
| 156 } | 158 } |
| 157 | 159 |
| 158 static bool CompareNodes(const T& a, const T& b) { return b <= a; } | 160 // is_heap requires a strict comparator. |
| 161 static bool CompareNodes(const T& a, const T& b) { return !(a <= b); } |
| 159 | 162 |
| 160 // Moves the |hole| up the tree and when the right position has been found | 163 // Moves the |hole| up the tree and when the right position has been found |
| 161 // |element| is moved in. | 164 // |element| is moved in. |
| 162 void MoveHoleUpAndFillWithElement(size_t hole, T&& element) { | 165 void MoveHoleUpAndFillWithElement(size_t hole, T&& element) { |
| 163 DCHECK_GT(hole, 0u); | 166 DCHECK_GT(hole, 0u); |
| 164 DCHECK_LE(hole, size_); | 167 DCHECK_LE(hole, size_); |
| 165 while (hole >= 2u) { | 168 while (hole >= 2u) { |
| 166 size_t parent_pos = hole / 2; | 169 size_t parent_pos = hole / 2; |
| 167 if (nodes_[parent_pos] <= element) | 170 if (nodes_[parent_pos] <= element) |
| 168 break; | 171 break; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } | 218 } |
| 216 | 219 |
| 217 std::vector<T> nodes_; // NOTE we use 1-based indexing | 220 std::vector<T> nodes_; // NOTE we use 1-based indexing |
| 218 size_t size_; | 221 size_t size_; |
| 219 }; | 222 }; |
| 220 | 223 |
| 221 } // namespace scheduler | 224 } // namespace scheduler |
| 222 } // namespace blink | 225 } // namespace blink |
| 223 | 226 |
| 224 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_INTRUSIVE_HEAP_H_ | 227 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_INTRUSIVE_HEAP_H_ |
| OLD | NEW |