5#ifndef RUNTIME_PLATFORM_PRIORITY_QUEUE_H_
6#define RUNTIME_PLATFORM_PRIORITY_QUEUE_H_
24template <
typename P,
typename V>
38 if (min_heap_ ==
nullptr)
FATAL(
"Cannot allocate memory.");
45 bool IsEmpty()
const {
return size_ == 0; }
52 if (size_ == min_heap_size_) {
53 Resize(min_heap_size_ << 1);
80 auto entry = FindMapEntry(
value);
81 if (entry !=
nullptr) {
82 const intptr_t
offset = ValueOfMapEntry(entry);
97 auto map_entry = FindMapEntry(
value);
98 if (map_entry ==
nullptr) {
103 const intptr_t
offset = ValueOfMapEntry(map_entry);
111 intptr_t parent = (
offset - 1) / 2;
115 }
else if (diff > 0) {
123 intptr_t min_heap_size() {
return min_heap_size_; }
128 static bool MatchFun(
void* key1,
void* key2) {
return key1 == key2; }
131 return hashmap_.
Lookup(CastKey(
key), HashKey(
key), insert);
133 void RemoveMapEntry(
const V&
key) {
137 void SetMapEntry(
const V&
key, intptr_t
value) {
138 FindMapEntry(
key,
true)->
value =
reinterpret_cast<void*
>(
value);
140 static uint32_t HashKey(
const V&
key) {
141 return static_cast<uint32_t
>(
reinterpret_cast<intptr_t
>(CastKey(
key)));
144 return reinterpret_cast<intptr_t
>(entry->value);
146 static void* CastKey(
const V&
key) {
147 return reinterpret_cast<void*
>((
const_cast<V&
>(
key)));
150 void RemoveAt(intptr_t
offset) {
162 if (size_ <= (min_heap_size_ >> 2) &&
164 Resize(min_heap_size_ >> 1);
168 void BubbleUp(intptr_t
offset) {
172 intptr_t parent = (
offset - 1) / 2;
173 if (min_heap_[parent].priority > min_heap_[
offset].priority) {
180 void BubbleDown(intptr_t
offset) {
182 intptr_t left_child_index = 2 *
offset + 1;
183 bool has_left_child = left_child_index < size_;
185 if (!has_left_child)
return;
187 intptr_t smallest_index =
offset;
189 if (min_heap_[left_child_index].priority < min_heap_[
offset].priority) {
190 smallest_index = left_child_index;
193 intptr_t right_child_index = left_child_index + 1;
194 bool has_right_child = right_child_index < size_;
195 if (has_right_child) {
196 if (min_heap_[right_child_index].priority <
197 min_heap_[smallest_index].priority) {
198 smallest_index = right_child_index;
202 if (
offset == smallest_index) {
206 Swap(
offset, smallest_index);
211 void Set(intptr_t offset1,
const Entry& entry) {
212 min_heap_[offset1] = entry;
213 SetMapEntry(entry.value, offset1);
216 void Swap(intptr_t offset1, intptr_t offset2) {
217 Entry temp = min_heap_[offset1];
218 min_heap_[offset1] = min_heap_[offset2];
219 min_heap_[offset2] = temp;
221 SetMapEntry(min_heap_[offset1].
value, offset1);
222 SetMapEntry(min_heap_[offset2].
value, offset2);
225 void Replace(intptr_t index, intptr_t with_other) {
226 RemoveMapEntry(min_heap_[index].
value);
228 const Entry& entry = min_heap_[with_other];
229 SetMapEntry(entry.value, index);
230 min_heap_[index] = entry;
233 void Resize(intptr_t new_min_heap_size) {
234 ASSERT(size_ < new_min_heap_size);
235 ASSERT(new_min_heap_size != min_heap_size_);
237 Entry* new_backing =
reinterpret_cast<Entry*
>(
240 if (new_backing ==
nullptr)
FATAL(
"Cannot allocate memory.");
242 min_heap_ = new_backing;
243 min_heap_size_ = new_min_heap_size;
262 intptr_t min_heap_size_;
264 SimpleHashMap hashmap_;
bool RemoveByValue(const V &value)
const Entry & Minimum() const
bool ContainsValue(const V &value)
static constexpr intptr_t kMinimumSize
void Insert(const P &priority, const V &value)
bool InsertOrChangePriority(const P &priority, const V &value)
Entry * Lookup(void *key, uint32_t hash, bool insert)
void Remove(void *key, uint32_t hash)
T __attribute__((ext_vector_type(N))) V
void * malloc(size_t size)
void * realloc(void *ptr, size_t size)