Skip to content
Merged
2 changes: 1 addition & 1 deletion src/Algorithms/sort.hpp
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>

Expand Down
4 changes: 2 additions & 2 deletions src/Data_Structures/Deque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ template <typename T> class deque
currSize++;
}

const T &pop_front()
T pop_front()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct change

{
if (currSize == 0)
{
Expand All @@ -83,7 +83,7 @@ template <typename T> class deque
return toRet;
}

const T &pop_back()
T pop_back()
{
if (currSize == 0)
{
Expand Down
93 changes: 93 additions & 0 deletions src/Data_Structures/Priority_Queue.hpp
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
2 changes: 1 addition & 1 deletion src/Data_Structures/Queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template <typename T> class queue
currSize++;
}

const T &pop()
T pop()
{
if (currSize == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Data_Structures/Ringbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ template <typename T> class ringbuffer
currSize++;
}

const T &get_item()
T get_item()
{
if (currSize == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Data_Structures/Stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ template <typename T> class stack

void push(T item) { data.push_back(item); }

const T &pop() { return data.pop_back(); }
T pop() { return data.pop_back(); }

T &top()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Data_Structures/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ template <typename T> class vector
currSize++;
}

const T &pop_back()
T pop_back()
{
if (currSize == 0)
{
// std::cout << "No items in vector\n";
throw std::out_of_range("No items.");
}
T &ret = data[currSize - 1];
T ret = data[currSize - 1];
currSize--;
return ret;
}
Expand Down
103 changes: 103 additions & 0 deletions tst/test_priority_queue.cpp
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());
}
Loading