generated from echavemann/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Implementation of Priority Queue #2
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
60901f6
PQ done
minnce f016bf2
oops forgot formatting
minnce 914c41a
forgot to uncomment test
minnce 7ef98fa
formatting again pain
minnce dda41eb
fix newLargest type
minnce 1ab7b09
switch up formatting
minnce cde2722
refactor
minnce b2d9e2a
make github happy
minnce 098257d
make github happy(2)
minnce 736a702
make style changes according to other ones:
minnce cb78fca
better use of const and refs
minnce 6e56a03
Merge branch 'main' into Tree
minnce fc818b6
PQ fixes
minnce 777f3f2
formatting
minnce 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #pragma once | ||
| #include <concepts> | ||
|
|
||
| namespace algorithms | ||
| namespace mystd | ||
| { | ||
| template <std::sortable T> | ||
|
|
||
|
|
||
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,93 @@ | ||
| #pragma once | ||
| #include <Data_Structures/Array.hpp> | ||
| #include <algorithm> | ||
| #include <iostream> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
|
|
||
| namespace mystd | ||
| { | ||
| template <typename T> class priority_queue | ||
| { | ||
| public: | ||
| priority_queue() : tree(4) | ||
| { | ||
| sz = 0; | ||
| maxSz = 4; | ||
| } | ||
|
|
||
| void push(T item) | ||
| { | ||
| if (sz == maxSz) | ||
| { | ||
| reallocate(maxSz + maxSz / 2); | ||
| } | ||
| tree[sz] = item; | ||
| size_t currInd = sz; | ||
| while (currInd != 0 && tree[(currInd - 1) / 2] < tree[currInd]) | ||
| { | ||
| std::swap(tree[currInd], tree[(currInd - 1) / 2]); | ||
| currInd = (currInd - 1) / 2; | ||
| } | ||
| sz++; | ||
| } | ||
|
|
||
| const T &top() const | ||
| { | ||
| if (sz == 0) | ||
| { | ||
| throw std::out_of_range("No items in Priority Queue."); | ||
| } | ||
| return tree[0]; | ||
| } | ||
|
|
||
| T pop() | ||
| { | ||
| if (sz == 0) | ||
| { | ||
| throw std::out_of_range("No items in Priority Queue."); | ||
| } | ||
| T toRet = tree[0]; | ||
| tree[0] = std::move(tree[sz - 1]); | ||
| sz--; | ||
| heapify(0); | ||
| return toRet; | ||
| } | ||
|
|
||
| bool empty() const { return sz == 0; } | ||
|
|
||
| size_t size() const { return sz; } | ||
|
|
||
| private: | ||
| void heapify(size_t currInd) | ||
| { | ||
| const size_t left = currInd * 2 + 1; | ||
| const size_t right = currInd * 2 + 2; | ||
| size_t newLargest = currInd; | ||
| if (left < sz && tree[left] > tree[newLargest]) | ||
| newLargest = left; | ||
| if (right < sz && tree[right] > tree[newLargest]) | ||
| newLargest = right; | ||
| if (newLargest != currInd) | ||
| { | ||
| std::swap(tree[currInd], tree[newLargest]); | ||
| heapify(newLargest); | ||
| } | ||
| } | ||
|
|
||
| void reallocate(size_t newSize) | ||
| { | ||
| array<T> newTree = array<T>(newSize); | ||
| for (size_t i = 0; i < sz; i++) | ||
| { | ||
| newTree[i] = std::move(tree[i]); | ||
| } | ||
| tree = std::move(newTree); | ||
| maxSz = newSize; | ||
| } | ||
|
|
||
| array<T> tree; | ||
| size_t sz; | ||
| size_t maxSz; | ||
| }; | ||
| } // namespace mystd |
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 |
|---|---|---|
|
|
@@ -46,7 +46,7 @@ template <typename T> class queue | |
| currSize++; | ||
| } | ||
|
|
||
| const T &pop() | ||
| T pop() | ||
| { | ||
| if (currSize == 0) | ||
| { | ||
|
|
||
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
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,103 @@ | ||
| #include <Data_Structures/Priority_Queue.hpp> | ||
| #include <gtest/gtest.h> | ||
| #include <stdexcept> | ||
|
|
||
| using namespace mystd; | ||
|
|
||
| TEST(PriorityTest, InitializeEmpty) | ||
| { | ||
| priority_queue<int> pq; | ||
| ASSERT_EQ(pq.size(), 0); | ||
| ASSERT_TRUE(pq.empty()); | ||
| ASSERT_THROW(pq.pop(), std::out_of_range); | ||
| } | ||
|
|
||
| TEST(PriorityTest, PushPopSingle) | ||
| { | ||
| priority_queue<int> pq; | ||
| pq.push(42); | ||
| ASSERT_EQ(pq.size(), 1); | ||
| ASSERT_EQ(pq.pop(), 42); | ||
| ASSERT_TRUE(pq.empty()); | ||
| } | ||
|
|
||
| TEST(PriorityTest, PushMultipleMaintainsOrder) | ||
| { | ||
| priority_queue<int> pq; | ||
| pq.push(10); | ||
| pq.push(50); | ||
| pq.push(20); | ||
| pq.push(40); | ||
| pq.push(30); | ||
|
|
||
| ASSERT_EQ(pq.size(), 5); | ||
| ASSERT_EQ(pq.pop(), 50); | ||
| ASSERT_EQ(pq.pop(), 40); | ||
| ASSERT_EQ(pq.pop(), 30); | ||
| ASSERT_EQ(pq.pop(), 20); | ||
| ASSERT_EQ(pq.pop(), 10); | ||
| ASSERT_TRUE(pq.empty()); | ||
| } | ||
|
|
||
| TEST(PriorityTest, HandlesDuplicatesCorrectly) | ||
| { | ||
| priority_queue<int> pq; | ||
| pq.push(5); | ||
| pq.push(5); | ||
| pq.push(5); | ||
| ASSERT_EQ(pq.size(), 3); | ||
|
|
||
| ASSERT_EQ(pq.pop(), 5); | ||
| ASSERT_EQ(pq.pop(), 5); | ||
| ASSERT_EQ(pq.pop(), 5); | ||
| ASSERT_TRUE(pq.empty()); | ||
| } | ||
|
|
||
| TEST(PriorityTest, HandlesNegativeNumbers) | ||
| { | ||
| priority_queue<int> pq; | ||
| pq.push(-10); | ||
| pq.push(0); | ||
| pq.push(-5); | ||
| pq.push(5); | ||
|
|
||
| ASSERT_EQ(pq.pop(), 5); | ||
| ASSERT_EQ(pq.pop(), 0); | ||
| ASSERT_EQ(pq.pop(), -5); | ||
| ASSERT_EQ(pq.pop(), -10); | ||
| ASSERT_TRUE(pq.empty()); | ||
| } | ||
|
|
||
| TEST(PriorityTest, StressTestLargeNumberOfElements) | ||
| { | ||
| priority_queue<int> pq; | ||
| int N = 1000; | ||
|
|
||
| for (int i = 0; i < N; ++i) | ||
| { | ||
| pq.push(i); | ||
| } | ||
| ASSERT_EQ(pq.size(), N); | ||
|
|
||
| for (int i = N - 1; i >= 0; --i) | ||
| { | ||
| ASSERT_EQ(pq.pop(), i); | ||
| } | ||
| ASSERT_TRUE(pq.empty()); | ||
| } | ||
|
|
||
| TEST(PriorityTest, InterleavedPushPopOperations) | ||
| { | ||
| priority_queue<int> pq; | ||
| pq.push(10); | ||
| pq.push(20); | ||
| ASSERT_EQ(pq.pop(), 20); | ||
| pq.push(15); | ||
| ASSERT_EQ(pq.pop(), 15); | ||
| pq.push(30); | ||
| pq.push(25); | ||
| ASSERT_EQ(pq.pop(), 30); | ||
| ASSERT_EQ(pq.pop(), 25); | ||
| ASSERT_EQ(pq.pop(), 10); | ||
| ASSERT_TRUE(pq.empty()); | ||
| } |
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.
correct change