-
Notifications
You must be signed in to change notification settings - Fork 844
TS-4295: Add Priority Queue in lib/ts/ #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /** @file | ||
|
|
||
| Priority Queue Implementation using Binary Heap. | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef __PRIORITY_QUEUE_H__ | ||
| #define __PRIORITY_QUEUE_H__ | ||
|
|
||
| #include "ts/ink_assert.h" | ||
| #include "ts/Vec.h" | ||
|
|
||
| template <typename T> struct PriorityQueueEntry { | ||
| PriorityQueueEntry(T n) : index(0), node(n) {} | ||
|
|
||
| uint32_t index; | ||
| T node; | ||
| }; | ||
|
|
||
| template <typename T> struct PriorityQueueLess { | ||
| bool operator()(const T &a, const T &b) { return *a < *b; } | ||
| }; | ||
|
|
||
| template <typename T, class Comp = PriorityQueueLess<T> > class PriorityQueue | ||
| { | ||
| public: | ||
| PriorityQueue() {} | ||
| ~PriorityQueue() {} | ||
|
|
||
| const bool empty(); | ||
| PriorityQueueEntry<T> *top(); | ||
| void pop(); | ||
| void push(PriorityQueueEntry<T> *); | ||
| void update(PriorityQueueEntry<T> *); | ||
| void update(PriorityQueueEntry<T> *, bool); | ||
| const Vec<PriorityQueueEntry<T> *> &dump() const; | ||
|
|
||
| private: | ||
| Vec<PriorityQueueEntry<T> *> _v; | ||
|
|
||
| void _swap(uint32_t, uint32_t); | ||
| void _bubble_up(uint32_t); | ||
| void _bubble_down(uint32_t); | ||
| }; | ||
|
|
||
| template <typename T, typename Comp> | ||
| const Vec<PriorityQueueEntry<T> *> & | ||
| PriorityQueue<T, Comp>::dump() const | ||
| { | ||
| return _v; | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| const bool | ||
| PriorityQueue<T, Comp>::empty() | ||
| { | ||
| return _v.length() == 0; | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::push(PriorityQueueEntry<T> *entry) | ||
| { | ||
| ink_release_assert(entry != NULL); | ||
|
|
||
| int len = _v.length(); | ||
| _v.push_back(entry); | ||
| entry->index = len; | ||
|
|
||
| _bubble_up(len); | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| PriorityQueueEntry<T> * | ||
| PriorityQueue<T, Comp>::top() | ||
| { | ||
| if (empty()) { | ||
| return NULL; | ||
| } else { | ||
| return _v[0]; | ||
| } | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::pop() | ||
| { | ||
| if (empty()) { | ||
| return; | ||
| } | ||
|
|
||
| _v[0] = _v[_v.length() - 1]; | ||
| _v.pop(); | ||
| _bubble_down(0); | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::update(PriorityQueueEntry<T> *entry) | ||
| { | ||
| ink_release_assert(entry != NULL); | ||
|
|
||
| if (empty()) { | ||
| return; | ||
| } | ||
|
|
||
| // One of them will works whether weight is increased or decreased | ||
| _bubble_down(entry->index); | ||
| _bubble_up(entry->index); | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::update(PriorityQueueEntry<T> *entry, bool increased) | ||
| { | ||
| ink_release_assert(entry != NULL); | ||
|
|
||
| if (empty()) { | ||
| return; | ||
| } | ||
|
|
||
| if (increased) { | ||
| _bubble_down(entry->index); | ||
| } else { | ||
| _bubble_up(entry->index); | ||
| } | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::_swap(uint32_t i, uint32_t j) | ||
| { | ||
| PriorityQueueEntry<T> *tmp = _v[i]; | ||
| _v[i] = _v[j]; | ||
| _v[j] = tmp; | ||
|
|
||
| _v[i]->index = i; | ||
| _v[j]->index = j; | ||
| } | ||
|
|
||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::_bubble_up(uint32_t index) | ||
| { | ||
| if (empty()) { | ||
| ink_release_assert(false); | ||
| } | ||
|
|
||
| Comp comp; | ||
|
|
||
| uint32_t parent; | ||
| while (index != 0) { | ||
| parent = (index - 1) / 2; | ||
| if (comp(_v[index]->node, _v[parent]->node)) { | ||
| _swap(parent, index); | ||
| index = parent; | ||
| continue; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| template <typename T, typename Comp> | ||
| void | ||
| PriorityQueue<T, Comp>::_bubble_down(uint32_t index) | ||
| { | ||
| if (empty()) { | ||
| // Do nothing | ||
| return; | ||
| } | ||
|
|
||
| uint32_t left, right, smaller; | ||
|
|
||
| Comp comp; | ||
|
|
||
| while (true) { | ||
| if ((left = index * 2 + 1) >= _v.length()) { | ||
| break; | ||
| } else if ((right = index * 2 + 2) >= _v.length()) { | ||
| smaller = left; | ||
| } else { | ||
| smaller = comp(_v[left]->node, _v[right]->node) ? left : right; | ||
| } | ||
|
|
||
| if (comp(_v[smaller]->node, _v[index]->node)) { | ||
| _swap(smaller, index); | ||
| index = smaller; | ||
| continue; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| #endif // __PRIORITY_QUEUE_H__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| */ | ||
|
|
||
| #include <stdarg.h> | ||
| #include "ts/ink_apidefs.h" | ||
| #include "ts/Regression.h" | ||
|
|
||
| namespace | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simply call both the up and down. Otherwise, caller must know whether the value is increased.
I suggest that make the flag completely optional and use it as a hint only if a caller explicitly specifies it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, if both of them are called, one of them works and other does nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, something like that. I was expecting overloading, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overloading sounds good. I'll add a function to call both of up and down.