From 60901f61c40103cd384c4cb1bc211df4559e2f03 Mon Sep 17 00:00:00 2001 From: minnce Date: Fri, 11 Apr 2025 18:39:42 -0500 Subject: [PATCH 01/13] PQ done --- src/Data_Structures/Priority_Queue.hpp | 80 +++++++++++++++++++++ tst/test_priority_queue.cpp | 96 ++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/Data_Structures/Priority_Queue.hpp create mode 100644 tst/test_priority_queue.cpp diff --git a/src/Data_Structures/Priority_Queue.hpp b/src/Data_Structures/Priority_Queue.hpp new file mode 100644 index 0000000..97cf25d --- /dev/null +++ b/src/Data_Structures/Priority_Queue.hpp @@ -0,0 +1,80 @@ +#pragma once +#include +#include +#include +#include + +namespace dataStructures { +template +class PriorityQueue { +public: + PriorityQueue() { + sz = 0; + reallocate(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--; + refactor(0); + return toRet; + } + + bool Empty() const { + return sz==0; + } + + size_t Size() const { + return sz; + } +private: + void refactor(size_t currInd) { + size_t left = currInd*2+1; + size_t right = currInd*2+2; + 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]); + refactor(newLargest); + } + } + + void reallocate(size_t newSize) { + std::unique_ptr newTree = std::make_unique(newSize); + for (size_t i = 0; i < sz; i++) { + newTree[i] = std::move(tree[i]); + } + tree = std::move(newTree); + maxSz = newSize; + } + std::unique_ptr tree; + size_t sz; + size_t maxSz; +}; +} \ No newline at end of file diff --git a/tst/test_priority_queue.cpp b/tst/test_priority_queue.cpp new file mode 100644 index 0000000..7f73e07 --- /dev/null +++ b/tst/test_priority_queue.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +using namespace dataStructures; + +TEST(PriorityTest, InitializeEmpty) { + PriorityQueue pq; + ASSERT_EQ(pq.Size(), 0); + ASSERT_TRUE(pq.Empty()); + ASSERT_THROW(pq.Pop(), std::out_of_range); +} + +TEST(PriorityTest, PushPopSingle) { + PriorityQueue pq; + pq.Push(42); + ASSERT_EQ(pq.Size(), 1); + ASSERT_EQ(pq.Pop(), 42); + ASSERT_TRUE(pq.Empty()); +} + +TEST(PriorityTest, PushMultipleMaintainsOrder) { + PriorityQueue 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) { + PriorityQueue 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) { + PriorityQueue 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) { +// PriorityQueue 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) { + PriorityQueue 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()); +} From f016bf27a1e674e2ffe51906a3b471ccc30d21d8 Mon Sep 17 00:00:00 2001 From: minnce Date: Fri, 11 Apr 2025 18:41:47 -0500 Subject: [PATCH 02/13] oops forgot formatting --- src/Data_Structures/Priority_Queue.hpp | 84 +++++++++++++++----------- tst/test_priority_queue.cpp | 22 ++++--- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/src/Data_Structures/Priority_Queue.hpp b/src/Data_Structures/Priority_Queue.hpp index 97cf25d..845995e 100644 --- a/src/Data_Structures/Priority_Queue.hpp +++ b/src/Data_Structures/Priority_Queue.hpp @@ -1,80 +1,92 @@ #pragma once +#include #include #include -#include #include -namespace dataStructures { -template -class PriorityQueue { -public: - PriorityQueue() { +namespace dataStructures +{ +template class PriorityQueue +{ + public: + PriorityQueue() + { sz = 0; reallocate(4); } - void Push(T item) { - if (sz == maxSz) { - reallocate(maxSz + maxSz/2); + 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; + 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) { + 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) { + 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]); + tree[0] = std::move(tree[sz - 1]); sz--; refactor(0); return toRet; } - bool Empty() const { - return sz==0; - } + bool Empty() const { return sz == 0; } - size_t Size() const { - return sz; - } -private: - void refactor(size_t currInd) { - size_t left = currInd*2+1; - size_t right = currInd*2+2; + size_t Size() const { return sz; } + + private: + void refactor(size_t currInd) + { + size_t left = currInd * 2 + 1; + size_t right = currInd * 2 + 2; 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]); + 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]); refactor(newLargest); } } - void reallocate(size_t newSize) { + void reallocate(size_t newSize) + { std::unique_ptr newTree = std::make_unique(newSize); - for (size_t i = 0; i < sz; i++) { + for (size_t i = 0; i < sz; i++) + { newTree[i] = std::move(tree[i]); } tree = std::move(newTree); maxSz = newSize; } + std::unique_ptr tree; size_t sz; size_t maxSz; }; -} \ No newline at end of file +} // namespace dataStructures \ No newline at end of file diff --git a/tst/test_priority_queue.cpp b/tst/test_priority_queue.cpp index 7f73e07..a6ca937 100644 --- a/tst/test_priority_queue.cpp +++ b/tst/test_priority_queue.cpp @@ -1,19 +1,19 @@ -#include #include +#include #include -#include -#include using namespace dataStructures; -TEST(PriorityTest, InitializeEmpty) { +TEST(PriorityTest, InitializeEmpty) +{ PriorityQueue pq; ASSERT_EQ(pq.Size(), 0); ASSERT_TRUE(pq.Empty()); ASSERT_THROW(pq.Pop(), std::out_of_range); } -TEST(PriorityTest, PushPopSingle) { +TEST(PriorityTest, PushPopSingle) +{ PriorityQueue pq; pq.Push(42); ASSERT_EQ(pq.Size(), 1); @@ -21,7 +21,8 @@ TEST(PriorityTest, PushPopSingle) { ASSERT_TRUE(pq.Empty()); } -TEST(PriorityTest, PushMultipleMaintainsOrder) { +TEST(PriorityTest, PushMultipleMaintainsOrder) +{ PriorityQueue pq; pq.Push(10); pq.Push(50); @@ -38,7 +39,8 @@ TEST(PriorityTest, PushMultipleMaintainsOrder) { ASSERT_TRUE(pq.Empty()); } -TEST(PriorityTest, HandlesDuplicatesCorrectly) { +TEST(PriorityTest, HandlesDuplicatesCorrectly) +{ PriorityQueue pq; pq.Push(5); pq.Push(5); @@ -51,7 +53,8 @@ TEST(PriorityTest, HandlesDuplicatesCorrectly) { ASSERT_TRUE(pq.Empty()); } -TEST(PriorityTest, HandlesNegativeNumbers) { +TEST(PriorityTest, HandlesNegativeNumbers) +{ PriorityQueue pq; pq.Push(-10); pq.Push(0); @@ -80,7 +83,8 @@ TEST(PriorityTest, HandlesNegativeNumbers) { // ASSERT_TRUE(pq.Empty()); // } -TEST(PriorityTest, InterleavedPushPopOperations) { +TEST(PriorityTest, InterleavedPushPopOperations) +{ PriorityQueue pq; pq.Push(10); pq.Push(20); From 914c41a36f42727dbbd13cffc3802347e1264564 Mon Sep 17 00:00:00 2001 From: minnce Date: Fri, 11 Apr 2025 18:42:28 -0500 Subject: [PATCH 03/13] forgot to uncomment test --- tst/test_priority_queue.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tst/test_priority_queue.cpp b/tst/test_priority_queue.cpp index a6ca937..2991db9 100644 --- a/tst/test_priority_queue.cpp +++ b/tst/test_priority_queue.cpp @@ -68,20 +68,20 @@ TEST(PriorityTest, HandlesNegativeNumbers) ASSERT_TRUE(pq.Empty()); } -// TEST(PriorityTest, StressTestLargeNumberOfElements) { -// PriorityQueue pq; -// int N = 1000; +TEST(PriorityTest, StressTestLargeNumberOfElements) { + PriorityQueue pq; + int N = 1000; -// for (int i = 0; i < N; ++i) { -// pq.Push(i); -// } -// ASSERT_EQ(pq.Size(), N); + 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()); -// } + for (int i = N - 1; i >= 0; --i) { + ASSERT_EQ(pq.Pop(), i); + } + ASSERT_TRUE(pq.Empty()); +} TEST(PriorityTest, InterleavedPushPopOperations) { From 7ef98fa06e3e960052e85c6e672459a39184fd0c Mon Sep 17 00:00:00 2001 From: minnce Date: Fri, 11 Apr 2025 18:44:09 -0500 Subject: [PATCH 04/13] formatting again pain --- tst/test_priority_queue.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tst/test_priority_queue.cpp b/tst/test_priority_queue.cpp index 2991db9..4b7b70d 100644 --- a/tst/test_priority_queue.cpp +++ b/tst/test_priority_queue.cpp @@ -68,16 +68,19 @@ TEST(PriorityTest, HandlesNegativeNumbers) ASSERT_TRUE(pq.Empty()); } -TEST(PriorityTest, StressTestLargeNumberOfElements) { +TEST(PriorityTest, StressTestLargeNumberOfElements) +{ PriorityQueue pq; int N = 1000; - for (int i = 0; i < N; ++i) { + for (int i = 0; i < N; ++i) + { pq.Push(i); } ASSERT_EQ(pq.Size(), N); - for (int i = N - 1; i >= 0; --i) { + for (int i = N - 1; i >= 0; --i) + { ASSERT_EQ(pq.Pop(), i); } ASSERT_TRUE(pq.Empty()); From dda41eb857552504bd388f3be8a2f1e025312e81 Mon Sep 17 00:00:00 2001 From: minnce Date: Fri, 11 Apr 2025 19:19:16 -0500 Subject: [PATCH 05/13] fix newLargest type --- src/Data_Structures/Priority_Queue.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data_Structures/Priority_Queue.hpp b/src/Data_Structures/Priority_Queue.hpp index 845995e..62f94c3 100644 --- a/src/Data_Structures/Priority_Queue.hpp +++ b/src/Data_Structures/Priority_Queue.hpp @@ -62,7 +62,7 @@ template class PriorityQueue { size_t left = currInd * 2 + 1; size_t right = currInd * 2 + 2; - T newLargest = currInd; + size_t newLargest = currInd; if (left < sz && tree[left] > tree[newLargest]) newLargest = left; if (right < sz && tree[right] > tree[newLargest]) From 1ab7b09037c0d4c9875e9a8f6e405857701bb685 Mon Sep 17 00:00:00 2001 From: minnce Date: Fri, 11 Apr 2025 19:27:47 -0500 Subject: [PATCH 06/13] switch up formatting --- .clang-format | 2 +- src/Data_Structures/Array.hpp | 3 ++- src/Data_Structures/Deque.hpp | 3 ++- src/Data_Structures/LinkedList.hpp | 3 ++- src/Data_Structures/Queue.hpp | 3 ++- src/Data_Structures/Ringbuffer.hpp | 3 ++- src/Data_Structures/Stack.hpp | 3 ++- src/Data_Structures/Vector.hpp | 3 ++- 8 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.clang-format b/.clang-format index 674ee46..4baae8b 100644 --- a/.clang-format +++ b/.clang-format @@ -4,7 +4,7 @@ UseTab: Never ColumnLimit: 160 BreakBeforeBraces: Allman SeparateDefinitionBlocks: Always -# BreakTemplateDeclarations: Yes +BreakTemplateDeclarations: Yes BraceWrapping: AfterClass: true AfterControlStatement: true diff --git a/src/Data_Structures/Array.hpp b/src/Data_Structures/Array.hpp index b3f6c51..97172a4 100644 --- a/src/Data_Structures/Array.hpp +++ b/src/Data_Structures/Array.hpp @@ -6,7 +6,8 @@ namespace dataStructures { -template class Array +template +class Array { public: Array(size_t initSize) diff --git a/src/Data_Structures/Deque.hpp b/src/Data_Structures/Deque.hpp index 2eed6e2..ea495c3 100644 --- a/src/Data_Structures/Deque.hpp +++ b/src/Data_Structures/Deque.hpp @@ -5,7 +5,8 @@ namespace dataStructures { -template class Deque +template +class Deque { public: Deque() diff --git a/src/Data_Structures/LinkedList.hpp b/src/Data_Structures/LinkedList.hpp index 14b5c28..cd8dd0b 100644 --- a/src/Data_Structures/LinkedList.hpp +++ b/src/Data_Structures/LinkedList.hpp @@ -5,7 +5,8 @@ namespace dataStructures { -template class LinkedList +template +class LinkedList { public: LinkedList() diff --git a/src/Data_Structures/Queue.hpp b/src/Data_Structures/Queue.hpp index 4b9e3f1..0665827 100644 --- a/src/Data_Structures/Queue.hpp +++ b/src/Data_Structures/Queue.hpp @@ -5,7 +5,8 @@ namespace dataStructures { -template class Queue +template +class Queue { public: Queue() diff --git a/src/Data_Structures/Ringbuffer.hpp b/src/Data_Structures/Ringbuffer.hpp index 24af305..7470805 100644 --- a/src/Data_Structures/Ringbuffer.hpp +++ b/src/Data_Structures/Ringbuffer.hpp @@ -6,7 +6,8 @@ namespace dataStructures { -template class Ringbuffer +template +class Ringbuffer { public: Ringbuffer(unsigned int initSize) diff --git a/src/Data_Structures/Stack.hpp b/src/Data_Structures/Stack.hpp index d43456c..b33a10a 100644 --- a/src/Data_Structures/Stack.hpp +++ b/src/Data_Structures/Stack.hpp @@ -7,7 +7,8 @@ namespace dataStructures { -template class Stack +template +class Stack { public: Stack() { data = Vector(); } diff --git a/src/Data_Structures/Vector.hpp b/src/Data_Structures/Vector.hpp index c439f71..a3d2b72 100644 --- a/src/Data_Structures/Vector.hpp +++ b/src/Data_Structures/Vector.hpp @@ -6,7 +6,8 @@ namespace dataStructures { -template class Vector +template +class Vector { public: Vector() { reallocate(2); } From cde27220c188f4dd20bc6d3b795ffddefdb99aa4 Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 07:38:24 -0500 Subject: [PATCH 07/13] refactor --- src/Data_Structures/Array.hpp | 8 +- src/Data_Structures/Deque.hpp | 42 ++++----- src/Data_Structures/LinkedList.hpp | 8 +- src/Data_Structures/Queue.hpp | 24 +++--- src/Data_Structures/Ringbuffer.hpp | 14 +-- src/Data_Structures/Stack.hpp | 22 ++--- src/Data_Structures/Vector.hpp | 16 ++-- tst/test_array.cpp | 4 +- tst/test_deque.cpp | 132 ++++++++++++++--------------- tst/test_linked_list.cpp | 18 ++-- tst/test_queue.cpp | 110 ++++++++++++------------ tst/test_ringbuffer.cpp | 110 ++++++++++++------------ tst/test_stack.cpp | 92 ++++++++++---------- tst/test_vector.cpp | 97 ++++++++++----------- 14 files changed, 345 insertions(+), 352 deletions(-) diff --git a/src/Data_Structures/Array.hpp b/src/Data_Structures/Array.hpp index 97172a4..1124113 100644 --- a/src/Data_Structures/Array.hpp +++ b/src/Data_Structures/Array.hpp @@ -4,13 +4,13 @@ #include #include -namespace dataStructures +namespace mystd { template -class Array +class array { public: - Array(size_t initSize) + array(size_t initSize) { Size = initSize; data = std::make_unique(initSize); @@ -38,4 +38,4 @@ class Array std::unique_ptr data; size_t Size; }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/src/Data_Structures/Deque.hpp b/src/Data_Structures/Deque.hpp index ea495c3..6e66369 100644 --- a/src/Data_Structures/Deque.hpp +++ b/src/Data_Structures/Deque.hpp @@ -1,24 +1,24 @@ #pragma once +#include #include #include #include -namespace dataStructures +namespace mystd { template -class Deque +class deque { public: - Deque() + deque() : buffer(4) { - buffer = std::make_unique(4); head = 0; tail = 0; maxSize = 4; currSize = 0; } - T &Front() + T &front() { if (currSize == 0) { @@ -27,7 +27,7 @@ class Deque return buffer[tail]; } - T &Back() + T &back() { if (currSize == 0) { @@ -36,7 +36,7 @@ class Deque return buffer[head]; } - void Push_Back(T item) + void push_back(T item) { if (currSize == maxSize) { @@ -54,7 +54,7 @@ class Deque currSize++; } - void Push_Front(T item) + void push_front(T item) { if (currSize == maxSize) { @@ -72,7 +72,7 @@ class Deque currSize++; } - const T &Pop_Front() + const T &pop_front() { if (currSize == 0) { @@ -84,7 +84,7 @@ class Deque return toRet; } - const T &Pop_Back() + const T &pop_back() { if (currSize == 0) { @@ -96,7 +96,7 @@ class Deque return toRet; } - void Dump_Buffer() + void dump_buffer() { int dumpTail = tail; for (int i = 0; i < currSize; i++) @@ -107,7 +107,7 @@ class Deque std::cout << "\n"; } - void Dump_Buffer_Raw() + void dump_buffer_raw() { for (int i = 0; i < maxSize; i++) { @@ -116,9 +116,9 @@ class Deque std::cout << "\n"; } - const bool Empty() const { return currSize == 0; } + bool empty() const { return currSize == 0; } - const size_t Size() const { return currSize; } + size_t size() const { return currSize; } const T &operator[](size_t index) const { @@ -130,15 +130,15 @@ class Deque } private: - unsigned int head; - unsigned int tail; - std::unique_ptr buffer; - unsigned int currSize; - unsigned int maxSize; + size_t head; + size_t tail; + size_t currSize; + size_t maxSize; + array buffer; void resize() { - auto newBuffer = std::make_unique(maxSize * 2); + auto newBuffer = array(maxSize * 2); for (int i = 0; i < currSize; i++) { newBuffer[i] = buffer[tail]; @@ -150,4 +150,4 @@ class Deque maxSize *= 2; } }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/src/Data_Structures/LinkedList.hpp b/src/Data_Structures/LinkedList.hpp index cd8dd0b..4758c07 100644 --- a/src/Data_Structures/LinkedList.hpp +++ b/src/Data_Structures/LinkedList.hpp @@ -3,13 +3,13 @@ #include #include -namespace dataStructures +namespace mystd { template -class LinkedList +class linked_list { public: - LinkedList() + linked_list() { head = std::make_shared(T{}); tail = std::make_shared(T{}); @@ -63,4 +63,4 @@ class LinkedList std::shared_ptr head; std::shared_ptr tail; }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/src/Data_Structures/Queue.hpp b/src/Data_Structures/Queue.hpp index 0665827..1fc61b1 100644 --- a/src/Data_Structures/Queue.hpp +++ b/src/Data_Structures/Queue.hpp @@ -3,13 +3,13 @@ #include #include -namespace dataStructures +namespace mystd { template -class Queue +class queue { public: - Queue() + queue() { buffer = std::make_unique(4); head = 0; @@ -18,7 +18,7 @@ class Queue currSize = 0; } - T &Front() + T &front() { if (currSize == 0) { @@ -27,7 +27,7 @@ class Queue return buffer[tail]; } - T &Back() + T &back() { if (currSize == 0) { @@ -36,7 +36,7 @@ class Queue return buffer[(head + maxSize - 1) % maxSize]; } - void Push(T item) + void push(T item) { if (currSize == maxSize) { @@ -47,7 +47,7 @@ class Queue currSize++; } - const T &Pop() + const T &pop() { if (currSize == 0) { @@ -59,7 +59,7 @@ class Queue return toRet; } - void Dump_Buffer() + void dump_buffer() { int dumpTail = tail; for (int i = 0; i < currSize; i++) @@ -70,7 +70,7 @@ class Queue std::cout << "\n"; } - void Dump_Buffer_Raw() + void dump_buffer_raw() { for (int i = 0; i < maxSize; i++) { @@ -79,9 +79,9 @@ class Queue std::cout << "\n"; } - const bool Empty() const { return currSize == 0; } + const bool empty() const { return currSize == 0; } - const size_t Size() const { return currSize; } + const size_t size() const { return currSize; } private: unsigned int head; @@ -104,4 +104,4 @@ class Queue maxSize *= 2; } }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/src/Data_Structures/Ringbuffer.hpp b/src/Data_Structures/Ringbuffer.hpp index 7470805..13dde7c 100644 --- a/src/Data_Structures/Ringbuffer.hpp +++ b/src/Data_Structures/Ringbuffer.hpp @@ -4,13 +4,13 @@ #include #include -namespace dataStructures +namespace mystd { template -class Ringbuffer +class ringbuffer { public: - Ringbuffer(unsigned int initSize) + ringbuffer(unsigned int initSize) { buffer = std::make_unique(initSize); head = 0; @@ -19,7 +19,7 @@ class Ringbuffer currSize = 0; } - void Add_Item(T item) + void add_item(T item) { if (currSize == maxSize) { @@ -30,7 +30,7 @@ class Ringbuffer currSize++; } - const T &Get_Item() + const T &get_item() { if (currSize == 0) { @@ -42,7 +42,7 @@ class Ringbuffer return toRet; } - void Dump_Buffer() + void dump_buffer() { int dumpTail = tail; for (int i = 0; i < maxSize; i++) @@ -74,4 +74,4 @@ class Ringbuffer maxSize *= 2; } }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/src/Data_Structures/Stack.hpp b/src/Data_Structures/Stack.hpp index b33a10a..8b4d3a7 100644 --- a/src/Data_Structures/Stack.hpp +++ b/src/Data_Structures/Stack.hpp @@ -5,32 +5,32 @@ #include #include -namespace dataStructures +namespace mystd { template -class Stack +class stack { public: - Stack() { data = Vector(); } + stack() { data = vector(); } - void Push(T item) { data.Push_Back(item); } + void push(T item) { data.push_back(item); } - const T &Pop() { return data.Pop_Back(); } + const T &pop() { return data.pop_back(); } - T &Top() + T &top() { - if (data.Size() == 0) + if (data.size() == 0) { throw std::out_of_range("No items in stack."); } return data[0]; } - bool Empty() { return data.Size() == 0; } + bool empty() { return data.size() == 0; } - size_t Size() { return data.Size(); } + size_t size() { return data.size(); } private: - Vector data; + vector data; }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/src/Data_Structures/Vector.hpp b/src/Data_Structures/Vector.hpp index a3d2b72..d7bfee6 100644 --- a/src/Data_Structures/Vector.hpp +++ b/src/Data_Structures/Vector.hpp @@ -4,13 +4,13 @@ #include #include -namespace dataStructures +namespace mystd { template -class Vector +class vector { public: - Vector() { reallocate(2); } + vector() { reallocate(2); } T &operator[](unsigned int index) { @@ -30,7 +30,7 @@ class Vector return data[index]; } - void Push_Back(T item) + void push_back(T item) { if (currSize == capacity) { @@ -40,7 +40,7 @@ class Vector currSize++; } - const T &Pop_Back() + const T &pop_back() { if (currSize == 0) { @@ -52,9 +52,7 @@ class Vector return ret; } - size_t Size() { return currSize; } - - size_t getCapacity() { return capacity; } + size_t size() { return currSize; } private: void reallocate(size_t newSize) @@ -72,4 +70,4 @@ class Vector size_t currSize = 0; size_t capacity = 0; }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/tst/test_array.cpp b/tst/test_array.cpp index 0ab8c64..16db253 100644 --- a/tst/test_array.cpp +++ b/tst/test_array.cpp @@ -1,11 +1,11 @@ #include #include -using namespace dataStructures; +using namespace mystd; TEST(ArrayTest, BasicArray) { - auto arr = Array(5); + array arr = array(5); arr[1] = 2; ASSERT_EQ(arr[1], 2); } \ No newline at end of file diff --git a/tst/test_deque.cpp b/tst/test_deque.cpp index 3926e08..23540b7 100644 --- a/tst/test_deque.cpp +++ b/tst/test_deque.cpp @@ -2,94 +2,94 @@ #include #include -using namespace dataStructures; +using namespace mystd; TEST(DequeTest, BasicPushBackPopFront) { - Deque d; - d.Push_Back(1); - d.Push_Back(2); - d.Push_Back(3); - - ASSERT_EQ(d.Size(), 3); - ASSERT_EQ(d.Front(), 1); - ASSERT_EQ(d.Back(), 3); - - ASSERT_EQ(d.Pop_Front(), 1); - ASSERT_EQ(d.Pop_Front(), 2); - ASSERT_EQ(d.Pop_Front(), 3); - ASSERT_TRUE(d.Empty()); + deque d; + d.push_back(1); + d.push_back(2); + d.push_back(3); + + ASSERT_EQ(d.size(), 3); + ASSERT_EQ(d.front(), 1); + ASSERT_EQ(d.back(), 3); + + ASSERT_EQ(d.pop_front(), 1); + ASSERT_EQ(d.pop_front(), 2); + ASSERT_EQ(d.pop_front(), 3); + ASSERT_TRUE(d.empty()); } TEST(DequeTest, BasicPushFrontPopBack) { - Deque d; - d.Push_Front(1); - d.Push_Front(2); - d.Push_Front(3); - - ASSERT_EQ(d.Size(), 3); - ASSERT_EQ(d.Front(), 3); - ASSERT_EQ(d.Back(), 1); - - ASSERT_EQ(d.Pop_Back(), 1); - ASSERT_EQ(d.Pop_Back(), 2); - ASSERT_EQ(d.Pop_Back(), 3); - ASSERT_TRUE(d.Empty()); + deque d; + d.push_front(1); + d.push_front(2); + d.push_front(3); + + ASSERT_EQ(d.size(), 3); + ASSERT_EQ(d.front(), 3); + ASSERT_EQ(d.back(), 1); + + ASSERT_EQ(d.pop_back(), 1); + ASSERT_EQ(d.pop_back(), 2); + ASSERT_EQ(d.pop_back(), 3); + ASSERT_TRUE(d.empty()); } TEST(DequeTest, MixedPushesAndPops) { - Deque d; - d.Push_Back(10); - d.Push_Front(20); - d.Push_Back(30); - d.Push_Front(40); - - ASSERT_EQ(d.Size(), 4); - ASSERT_EQ(d.Front(), 40); - ASSERT_EQ(d.Back(), 30); - - ASSERT_EQ(d.Pop_Front(), 40); - ASSERT_EQ(d.Pop_Back(), 30); - ASSERT_EQ(d.Pop_Front(), 20); - ASSERT_EQ(d.Pop_Back(), 10); - ASSERT_TRUE(d.Empty()); + deque d; + d.push_back(10); + d.push_front(20); + d.push_back(30); + d.push_front(40); + + ASSERT_EQ(d.size(), 4); + ASSERT_EQ(d.front(), 40); + ASSERT_EQ(d.back(), 30); + + ASSERT_EQ(d.pop_front(), 40); + ASSERT_EQ(d.pop_back(), 30); + ASSERT_EQ(d.pop_front(), 20); + ASSERT_EQ(d.pop_back(), 10); + ASSERT_TRUE(d.empty()); } TEST(DequeTest, ThrowsWhenEmptyPopFront) { - Deque d; - ASSERT_THROW(d.Pop_Front(), std::out_of_range); + deque d; + ASSERT_THROW(d.pop_front(), std::out_of_range); } TEST(DequeTest, ThrowsWhenEmptyPopBack) { - Deque d; - ASSERT_THROW(d.Pop_Back(), std::out_of_range); + deque d; + ASSERT_THROW(d.pop_back(), std::out_of_range); } TEST(DequeTest, ThrowsWhenAccessFrontEmpty) { - Deque d; - ASSERT_THROW(d.Front(), std::out_of_range); + deque d; + ASSERT_THROW(d.front(), std::out_of_range); } TEST(DequeTest, ThrowsWhenAccessBackEmpty) { - Deque d; - ASSERT_THROW(d.Back(), std::out_of_range); + deque d; + ASSERT_THROW(d.back(), std::out_of_range); } TEST(DequeTest, PushMoreThanInitialCapacity) { - Deque d; + deque d; for (int i = 0; i < 10; ++i) { - d.Push_Back(i); + d.push_back(i); } - ASSERT_EQ(d.Size(), 10); + ASSERT_EQ(d.size(), 10); for (int i = 0; i < 10; ++i) { ASSERT_EQ(d[i], i); @@ -98,25 +98,25 @@ TEST(DequeTest, PushMoreThanInitialCapacity) TEST(DequeTest, OperatorIndexing) { - Deque d; - d.Push_Back(100); - d.Push_Back(200); - d.Push_Back(300); + deque d; + d.push_back(100); + d.push_back(200); + d.push_back(300); ASSERT_EQ(d[0], 100); ASSERT_EQ(d[1], 200); ASSERT_EQ(d[2], 300); - d.Pop_Front(); + d.pop_front(); ASSERT_EQ(d[0], 200); ASSERT_EQ(d[1], 300); } TEST(DequeTest, IndexOutOfRangeThrows) { - Deque d; - d.Push_Back(1); - d.Push_Back(2); + deque d; + d.push_back(1); + d.push_back(2); ASSERT_THROW(d[2], std::out_of_range); ASSERT_THROW(d[5], std::out_of_range); @@ -124,14 +124,14 @@ TEST(DequeTest, IndexOutOfRangeThrows) TEST(DequeTest, StressTestLargeNumberOfElements) { - Deque d; + deque d; const int N = 1000; for (int i = 0; i < N; ++i) { - d.Push_Back(i); + d.push_back(i); } - ASSERT_EQ(d.Size(), N); + ASSERT_EQ(d.size(), N); for (int i = 0; i < N; ++i) { @@ -140,7 +140,7 @@ TEST(DequeTest, StressTestLargeNumberOfElements) for (int i = 0; i < N; ++i) { - ASSERT_EQ(d.Pop_Front(), i); + ASSERT_EQ(d.pop_front(), i); } - ASSERT_TRUE(d.Empty()); + ASSERT_TRUE(d.empty()); } diff --git a/tst/test_linked_list.cpp b/tst/test_linked_list.cpp index 7e2903a..4fc45a9 100644 --- a/tst/test_linked_list.cpp +++ b/tst/test_linked_list.cpp @@ -3,18 +3,18 @@ #include #include -using namespace dataStructures; +using namespace mystd; TEST(LinkedListTest, EmptyList) { - LinkedList list; + linked_list list; auto vec = list.convert_to_vector(); EXPECT_TRUE(vec.empty()); } TEST(LinkedListTest, SingleItem) { - LinkedList list; + linked_list list; list.add_item(42); auto vec = list.convert_to_vector(); ASSERT_EQ(vec.size(), 1); @@ -23,7 +23,7 @@ TEST(LinkedListTest, SingleItem) TEST(LinkedListTest, MultipleItems) { - LinkedList list; + linked_list list; list.add_item(1); list.add_item(2); list.add_item(3); @@ -37,13 +37,13 @@ TEST(LinkedListTest, MultipleItems) TEST(LinkedListTest, PrintEmpty) { - LinkedList list; + linked_list list; EXPECT_NO_THROW(list.print_list()); } TEST(LinkedListTest, PrintNonEmpty) { - LinkedList list; + linked_list list; list.add_item(100); list.add_item(200); testing::internal::CaptureStdout(); @@ -55,7 +55,7 @@ TEST(LinkedListTest, PrintNonEmpty) TEST(LinkedListTest, StringItems) { - LinkedList list; + linked_list list; list.add_item("hello"); list.add_item("world"); @@ -67,7 +67,7 @@ TEST(LinkedListTest, StringItems) TEST(LinkedListTest, Duplicates) { - LinkedList list; + linked_list list; list.add_item(7); list.add_item(7); list.add_item(7); @@ -81,7 +81,7 @@ TEST(LinkedListTest, Duplicates) TEST(LinkedListTest, StressTest) { - LinkedList list; + linked_list list; const int N = 1000; for (int i = 0; i < N; ++i) list.add_item(i); diff --git a/tst/test_queue.cpp b/tst/test_queue.cpp index f526fdd..f59caa2 100644 --- a/tst/test_queue.cpp +++ b/tst/test_queue.cpp @@ -2,105 +2,105 @@ #include #include -using namespace dataStructures; +using namespace mystd; TEST(QueueTest, BasicInitializeTest) { - auto q = Queue(); - q.Push(2); - ASSERT_EQ(q.Size(), 1); - ASSERT_FALSE(q.Empty()); - ASSERT_EQ(q.Pop(), 2); - ASSERT_THROW(q.Pop(), std::out_of_range); - ASSERT_TRUE(q.Empty()); - ASSERT_EQ(q.Size(), 0); + auto q = queue(); + q.push(2); + ASSERT_EQ(q.size(), 1); + ASSERT_FALSE(q.empty()); + ASSERT_EQ(q.pop(), 2); + ASSERT_THROW(q.pop(), std::out_of_range); + ASSERT_TRUE(q.empty()); + ASSERT_EQ(q.size(), 0); } TEST(QueueTest, PushAndPopMultipleItems) { - Queue q; - q.Push(1); - q.Push(2); - q.Push(3); - - ASSERT_EQ(q.Size(), 3); - ASSERT_EQ(q.Pop(), 1); - ASSERT_EQ(q.Pop(), 2); - ASSERT_EQ(q.Pop(), 3); - ASSERT_TRUE(q.Empty()); + queue q; + q.push(1); + q.push(2); + q.push(3); + + ASSERT_EQ(q.size(), 3); + ASSERT_EQ(q.pop(), 1); + ASSERT_EQ(q.pop(), 2); + ASSERT_EQ(q.pop(), 3); + ASSERT_TRUE(q.empty()); } TEST(QueueTest, FrontAndBackTest) { - Queue q; - q.Push(10); - q.Push(20); - q.Push(30); + queue q; + q.push(10); + q.push(20); + q.push(30); - ASSERT_EQ(q.Front(), 10); - ASSERT_EQ(q.Back(), 30); + ASSERT_EQ(q.front(), 10); + ASSERT_EQ(q.back(), 30); - q.Pop(); - ASSERT_EQ(q.Front(), 20); - ASSERT_EQ(q.Back(), 30); + q.pop(); + ASSERT_EQ(q.front(), 20); + ASSERT_EQ(q.back(), 30); } TEST(QueueTest, PopFromEmptyQueueThrows) { - Queue q; - ASSERT_THROW(q.Pop(), std::out_of_range); + queue q; + ASSERT_THROW(q.pop(), std::out_of_range); - q.Push(5); - q.Pop(); - ASSERT_THROW(q.Pop(), std::out_of_range); + q.push(5); + q.pop(); + ASSERT_THROW(q.pop(), std::out_of_range); } TEST(QueueTest, FrontOnEmptyQueueThrows) { - Queue q; - ASSERT_THROW(q.Front(), std::out_of_range); + queue q; + ASSERT_THROW(q.front(), std::out_of_range); - q.Push(42); - q.Pop(); - ASSERT_THROW(q.Front(), std::out_of_range); + q.push(42); + q.pop(); + ASSERT_THROW(q.front(), std::out_of_range); } TEST(QueueTest, BackOnEmptyQueueThrows) { - Queue q; - ASSERT_THROW(q.Back(), std::out_of_range); + queue q; + ASSERT_THROW(q.back(), std::out_of_range); - q.Push(100); - q.Pop(); - ASSERT_THROW(q.Back(), std::out_of_range); + q.push(100); + q.pop(); + ASSERT_THROW(q.back(), std::out_of_range); } TEST(QueueTest, SizeConsistencyTest) { - Queue q; - ASSERT_EQ(q.Size(), 0); + queue q; + ASSERT_EQ(q.size(), 0); for (int i = 0; i < 5; ++i) { - q.Push(i); - ASSERT_EQ(q.Size(), i + 1); + q.push(i); + ASSERT_EQ(q.size(), i + 1); } for (int i = 5; i > 0; --i) { - q.Pop(); - ASSERT_EQ(q.Size(), i - 1); + q.pop(); + ASSERT_EQ(q.size(), i - 1); } } TEST(QueueTest, EmptyCheck) { - Queue q; - ASSERT_TRUE(q.Empty()); + queue q; + ASSERT_TRUE(q.empty()); - q.Push(123); - ASSERT_FALSE(q.Empty()); + q.push(123); + ASSERT_FALSE(q.empty()); - q.Pop(); - ASSERT_TRUE(q.Empty()); + q.pop(); + ASSERT_TRUE(q.empty()); } diff --git a/tst/test_ringbuffer.cpp b/tst/test_ringbuffer.cpp index a204719..a07788c 100644 --- a/tst/test_ringbuffer.cpp +++ b/tst/test_ringbuffer.cpp @@ -3,94 +3,94 @@ #include #include -using namespace dataStructures; +using namespace mystd; TEST(RingbufferTest, EmptyGetReturnsNullopt) { - auto ring = Ringbuffer(4); - EXPECT_THROW(ring.Get_Item(), std::out_of_range); + auto ring = ringbuffer(4); + EXPECT_THROW(ring.get_item(), std::out_of_range); } TEST(RingbufferTest, BasicPushPop) { - Ringbuffer ring(3); - ring.Add_Item(10); - ring.Add_Item(20); + ringbuffer ring(3); + ring.add_item(10); + ring.add_item(20); - EXPECT_EQ(ring.Get_Item(), 10); - EXPECT_EQ(ring.Get_Item(), 20); - EXPECT_THROW(ring.Get_Item(), std::out_of_range); + EXPECT_EQ(ring.get_item(), 10); + EXPECT_EQ(ring.get_item(), 20); + EXPECT_THROW(ring.get_item(), std::out_of_range); } TEST(RingbufferTest, Wraparound) { - Ringbuffer ring(3); - ring.Add_Item(1); - ring.Add_Item(2); - ring.Add_Item(3); - - EXPECT_EQ(ring.Get_Item(), 1); - ring.Add_Item(4); - - EXPECT_EQ(ring.Get_Item(), 2); - EXPECT_EQ(ring.Get_Item(), 3); - EXPECT_EQ(ring.Get_Item(), 4); - EXPECT_THROW(ring.Get_Item(), std::out_of_range); + ringbuffer ring(3); + ring.add_item(1); + ring.add_item(2); + ring.add_item(3); + + EXPECT_EQ(ring.get_item(), 1); + ring.add_item(4); + + EXPECT_EQ(ring.get_item(), 2); + EXPECT_EQ(ring.get_item(), 3); + EXPECT_EQ(ring.get_item(), 4); + EXPECT_THROW(ring.get_item(), std::out_of_range); } TEST(RingbufferTest, ResizePreservesOrder) { - Ringbuffer ring(2); - ring.Add_Item(100); - ring.Add_Item(200); - ring.Add_Item(300); - - EXPECT_EQ(ring.Get_Item(), 100); - EXPECT_EQ(ring.Get_Item(), 200); - EXPECT_EQ(ring.Get_Item(), 300); - EXPECT_THROW(ring.Get_Item(), std::out_of_range); + ringbuffer ring(2); + ring.add_item(100); + ring.add_item(200); + ring.add_item(300); + + EXPECT_EQ(ring.get_item(), 100); + EXPECT_EQ(ring.get_item(), 200); + EXPECT_EQ(ring.get_item(), 300); + EXPECT_THROW(ring.get_item(), std::out_of_range); } TEST(RingbufferTest, AddAfterReads) { - Ringbuffer ring(3); - ring.Add_Item(5); - ring.Add_Item(6); - EXPECT_EQ(ring.Get_Item(), 5); - EXPECT_EQ(ring.Get_Item(), 6); - - ring.Add_Item(7); - ring.Add_Item(8); - ring.Add_Item(9); - - EXPECT_EQ(ring.Get_Item(), 7); - EXPECT_EQ(ring.Get_Item(), 8); - EXPECT_EQ(ring.Get_Item(), 9); + ringbuffer ring(3); + ring.add_item(5); + ring.add_item(6); + EXPECT_EQ(ring.get_item(), 5); + EXPECT_EQ(ring.get_item(), 6); + + ring.add_item(7); + ring.add_item(8); + ring.add_item(9); + + EXPECT_EQ(ring.get_item(), 7); + EXPECT_EQ(ring.get_item(), 8); + EXPECT_EQ(ring.get_item(), 9); } TEST(RingbufferTest, StringSupport) { - Ringbuffer ring(2); - ring.Add_Item("foo"); - ring.Add_Item("bar"); + ringbuffer ring(2); + ring.add_item("foo"); + ring.add_item("bar"); - EXPECT_EQ(ring.Get_Item(), "foo"); - ring.Add_Item("baz"); + EXPECT_EQ(ring.get_item(), "foo"); + ring.add_item("baz"); - EXPECT_EQ(ring.Get_Item(), "bar"); - EXPECT_EQ(ring.Get_Item(), "baz"); - EXPECT_THROW(ring.Get_Item(), std::out_of_range); + EXPECT_EQ(ring.get_item(), "bar"); + EXPECT_EQ(ring.get_item(), "baz"); + EXPECT_THROW(ring.get_item(), std::out_of_range); } TEST(RingbufferTest, StressTest) { const int N = 10000; - Ringbuffer ring(4); + ringbuffer ring(4); for (int i = 0; i < N; ++i) - ring.Add_Item(i); + ring.add_item(i); for (int i = 0; i < N; ++i) - ASSERT_EQ(ring.Get_Item(), i); + ASSERT_EQ(ring.get_item(), i); - EXPECT_THROW(ring.Get_Item(), std::out_of_range); + EXPECT_THROW(ring.get_item(), std::out_of_range); } diff --git a/tst/test_stack.cpp b/tst/test_stack.cpp index 808b87f..e2adc8f 100644 --- a/tst/test_stack.cpp +++ b/tst/test_stack.cpp @@ -3,7 +3,7 @@ #include #include -using namespace dataStructures; +using namespace mystd; struct Dummy { @@ -16,76 +16,76 @@ struct Dummy TEST(StackTest, BasicInitializeTest) { - auto s = Stack(); - ASSERT_EQ(s.Size(), 0); - ASSERT_TRUE(s.Empty()); - s.Push(1); - s.Push(2); - s.Push(3); - s.Push(4); - ASSERT_FALSE(s.Empty()); - ASSERT_EQ(s.Size(), 4); - ASSERT_EQ(s.Pop(), 4); - ASSERT_EQ(s.Pop(), 3); - ASSERT_EQ(s.Pop(), 2); - ASSERT_EQ(s.Size(), 1); - ASSERT_EQ(s.Pop(), 1); - ASSERT_EQ(s.Size(), 0); - ASSERT_TRUE(s.Empty()); + auto s = stack(); + ASSERT_EQ(s.size(), 0); + ASSERT_TRUE(s.empty()); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + ASSERT_FALSE(s.empty()); + ASSERT_EQ(s.size(), 4); + ASSERT_EQ(s.pop(), 4); + ASSERT_EQ(s.pop(), 3); + ASSERT_EQ(s.pop(), 2); + ASSERT_EQ(s.size(), 1); + ASSERT_EQ(s.pop(), 1); + ASSERT_EQ(s.size(), 0); + ASSERT_TRUE(s.empty()); } TEST(StackTest, StressTest) { - Stack s; + stack s; for (int i = 0; i < 10000; ++i) { - s.Push(i); + s.push(i); } - ASSERT_EQ(s.Size(), 10000); + ASSERT_EQ(s.size(), 10000); for (int i = 9999; i >= 0; --i) { - ASSERT_EQ(s.Pop(), i); + ASSERT_EQ(s.pop(), i); } - ASSERT_TRUE(s.Empty()); + ASSERT_TRUE(s.empty()); } TEST(StackTest, PopFromEmptyStack) { - Stack s; - ASSERT_TRUE(s.Empty()); - EXPECT_THROW(s.Pop(), std::out_of_range); + stack s; + ASSERT_TRUE(s.empty()); + EXPECT_THROW(s.pop(), std::out_of_range); } TEST(StackTest, SingleElementTest) { - Stack s; - s.Push("hello"); - ASSERT_FALSE(s.Empty()); - ASSERT_EQ(s.Size(), 1); - ASSERT_EQ(s.Pop(), "hello"); - ASSERT_TRUE(s.Empty()); - ASSERT_EQ(s.Size(), 0); + stack s; + s.push("hello"); + ASSERT_FALSE(s.empty()); + ASSERT_EQ(s.size(), 1); + ASSERT_EQ(s.pop(), "hello"); + ASSERT_TRUE(s.empty()); + ASSERT_EQ(s.size(), 0); } TEST(StackTest, InterleavedPushPopTest) { - Stack s; - s.Push(1); - s.Push(2); - ASSERT_EQ(s.Pop(), 2); - s.Push(3); - ASSERT_EQ(s.Pop(), 3); - ASSERT_EQ(s.Pop(), 1); - ASSERT_TRUE(s.Empty()); + stack s; + s.push(1); + s.push(2); + ASSERT_EQ(s.pop(), 2); + s.push(3); + ASSERT_EQ(s.pop(), 3); + ASSERT_EQ(s.pop(), 1); + ASSERT_TRUE(s.empty()); } TEST(StackTest, ComplexObjectTest) { - Stack s; - s.Push(Dummy(10)); - s.Push(Dummy(20)); - ASSERT_EQ(s.Pop().x, 20); - ASSERT_EQ(s.Pop().x, 10); - ASSERT_TRUE(s.Empty()); + stack s; + s.push(Dummy(10)); + s.push(Dummy(20)); + ASSERT_EQ(s.pop().x, 20); + ASSERT_EQ(s.pop().x, 10); + ASSERT_TRUE(s.empty()); } diff --git a/tst/test_vector.cpp b/tst/test_vector.cpp index 5481810..50ebad2 100644 --- a/tst/test_vector.cpp +++ b/tst/test_vector.cpp @@ -3,7 +3,7 @@ #include #include -using namespace dataStructures; +using namespace mystd; struct Person { @@ -15,11 +15,10 @@ struct Person TEST(VectorTest, InitializeTest) { - auto v = Vector(); - ASSERT_EQ(v.Size(), 0); - ASSERT_EQ(v.getCapacity(), 2); + auto v = vector(); + ASSERT_EQ(v.size(), 0); ASSERT_THROW(v[0], std::out_of_range); - v.Push_Back(1); + v.push_back(1); ASSERT_EQ(v[0], 1); ASSERT_THROW(v[1], std::out_of_range); ASSERT_THROW(v[2], std::out_of_range); @@ -29,41 +28,37 @@ TEST(VectorTest, InitializeTest) TEST(VectorTest, PopTest) { - auto v = Vector(); - ASSERT_THROW(v.Pop_Back(), std::out_of_range); - v.Push_Back(1); - ASSERT_EQ(1, v.Pop_Back()); - ASSERT_EQ(v.Size(), 0); - ASSERT_EQ(v.getCapacity(), 2); + auto v = vector(); + ASSERT_THROW(v.pop_back(), std::out_of_range); + v.push_back(1); + ASSERT_EQ(1, v.pop_back()); + ASSERT_EQ(v.size(), 0); } TEST(VectorTest, CharTest) { - auto v = Vector(); - v.Push_Back('a'); - ASSERT_EQ('a', v.Pop_Back()); - ASSERT_EQ(v.Size(), 0); - ASSERT_EQ(v.getCapacity(), 2); + auto v = vector(); + v.push_back('a'); + ASSERT_EQ('a', v.pop_back()); + ASSERT_EQ(v.size(), 0); } TEST(VectorTest, StringTest) { - auto v = Vector(); - v.Push_Back("Test"); - ASSERT_EQ("Test", v.Pop_Back()); - ASSERT_EQ(v.Size(), 0); - ASSERT_EQ(v.getCapacity(), 2); + auto v = vector(); + v.push_back("Test"); + ASSERT_EQ("Test", v.pop_back()); + ASSERT_EQ(v.size(), 0); } TEST(VectorTest, ResizeAndAccessBeyondInitialCapacity) { - Vector v; + vector v; for (int i = 0; i < 10; ++i) { - v.Push_Back(i); + v.push_back(i); } - ASSERT_EQ(v.Size(), 10); - ASSERT_GE(v.getCapacity(), 10); + ASSERT_EQ(v.size(), 10); for (int i = 0; i < 10; ++i) { @@ -75,41 +70,41 @@ TEST(VectorTest, ResizeAndAccessBeyondInitialCapacity) TEST(VectorTest, MultiplePops) { - Vector v; - v.Push_Back(5); - v.Push_Back(10); - v.Push_Back(15); - - ASSERT_EQ(v.Pop_Back(), 15); - ASSERT_EQ(v.Pop_Back(), 10); - ASSERT_EQ(v.Pop_Back(), 5); - ASSERT_THROW(v.Pop_Back(), std::out_of_range); + vector v; + v.push_back(5); + v.push_back(10); + v.push_back(15); + + ASSERT_EQ(v.pop_back(), 15); + ASSERT_EQ(v.pop_back(), 10); + ASSERT_EQ(v.pop_back(), 5); + ASSERT_THROW(v.pop_back(), std::out_of_range); } TEST(VectorTest, CustomStructTest) { - Vector people; - people.Push_Back({"Alice", 30}); - people.Push_Back({"Bob", 25}); + vector people; + people.push_back({"Alice", 30}); + people.push_back({"Bob", 25}); - auto p1 = people.Pop_Back(); + auto p1 = people.pop_back(); ASSERT_EQ(p1, Person("Bob", 25)); - auto p2 = people.Pop_Back(); + auto p2 = people.pop_back(); ASSERT_EQ(p2, Person("Alice", 30)); - ASSERT_EQ(people.Size(), 0); + ASSERT_EQ(people.size(), 0); } TEST(VectorTest, PushAndAccessAfterReallocation) { - Vector v; + vector v; for (int i = 0; i < 100; ++i) { - v.Push_Back("Item " + std::to_string(i)); + v.push_back("Item " + std::to_string(i)); } - ASSERT_EQ(v.Size(), 100); + ASSERT_EQ(v.size(), 100); for (int i = 0; i < 100; ++i) { ASSERT_EQ(v[i], "Item " + std::to_string(i)); @@ -118,17 +113,17 @@ TEST(VectorTest, PushAndAccessAfterReallocation) TEST(VectorTest, AlternatingPushPop) { - Vector v; + vector v; - v.Push_Back("A"); - ASSERT_EQ(v.Pop_Back(), "A"); + v.push_back("A"); + ASSERT_EQ(v.pop_back(), "A"); - v.Push_Back("B"); - v.Push_Back("C"); - ASSERT_EQ(v.Pop_Back(), "C"); + v.push_back("B"); + v.push_back("C"); + ASSERT_EQ(v.pop_back(), "C"); - v.Push_Back("D"); - ASSERT_EQ(v.Size(), 2); + v.push_back("D"); + ASSERT_EQ(v.size(), 2); ASSERT_EQ(v[0], "B"); ASSERT_EQ(v[1], "D"); } From b2d9e2a5cc4e766d058d4940af5a482ae3fb426a Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 07:40:22 -0500 Subject: [PATCH 08/13] make github happy --- .clang-format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-format b/.clang-format index 4baae8b..674ee46 100644 --- a/.clang-format +++ b/.clang-format @@ -4,7 +4,7 @@ UseTab: Never ColumnLimit: 160 BreakBeforeBraces: Allman SeparateDefinitionBlocks: Always -BreakTemplateDeclarations: Yes +# BreakTemplateDeclarations: Yes BraceWrapping: AfterClass: true AfterControlStatement: true From 098257de8931da6d84d095f20990cffec59dee8f Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 07:42:08 -0500 Subject: [PATCH 09/13] make github happy(2) --- .gitignore | 1 + src/Data_Structures/Array.hpp | 3 +-- src/Data_Structures/Deque.hpp | 3 +-- src/Data_Structures/LinkedList.hpp | 3 +-- src/Data_Structures/Queue.hpp | 3 +-- src/Data_Structures/Ringbuffer.hpp | 3 +-- src/Data_Structures/Stack.hpp | 3 +-- src/Data_Structures/Vector.hpp | 3 +-- 8 files changed, 8 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index cb7b974..559d631 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ CMakeUserPresets.json cmake-build-debug CMakeFiles .cache +.clang-format \ No newline at end of file diff --git a/src/Data_Structures/Array.hpp b/src/Data_Structures/Array.hpp index 1124113..ad7b67b 100644 --- a/src/Data_Structures/Array.hpp +++ b/src/Data_Structures/Array.hpp @@ -6,8 +6,7 @@ namespace mystd { -template -class array +template class array { public: array(size_t initSize) diff --git a/src/Data_Structures/Deque.hpp b/src/Data_Structures/Deque.hpp index 6e66369..dadb972 100644 --- a/src/Data_Structures/Deque.hpp +++ b/src/Data_Structures/Deque.hpp @@ -6,8 +6,7 @@ namespace mystd { -template -class deque +template class deque { public: deque() : buffer(4) diff --git a/src/Data_Structures/LinkedList.hpp b/src/Data_Structures/LinkedList.hpp index 4758c07..13e0e8f 100644 --- a/src/Data_Structures/LinkedList.hpp +++ b/src/Data_Structures/LinkedList.hpp @@ -5,8 +5,7 @@ namespace mystd { -template -class linked_list +template class linked_list { public: linked_list() diff --git a/src/Data_Structures/Queue.hpp b/src/Data_Structures/Queue.hpp index 1fc61b1..024d747 100644 --- a/src/Data_Structures/Queue.hpp +++ b/src/Data_Structures/Queue.hpp @@ -5,8 +5,7 @@ namespace mystd { -template -class queue +template class queue { public: queue() diff --git a/src/Data_Structures/Ringbuffer.hpp b/src/Data_Structures/Ringbuffer.hpp index 13dde7c..9ee986a 100644 --- a/src/Data_Structures/Ringbuffer.hpp +++ b/src/Data_Structures/Ringbuffer.hpp @@ -6,8 +6,7 @@ namespace mystd { -template -class ringbuffer +template class ringbuffer { public: ringbuffer(unsigned int initSize) diff --git a/src/Data_Structures/Stack.hpp b/src/Data_Structures/Stack.hpp index 8b4d3a7..be3f6cc 100644 --- a/src/Data_Structures/Stack.hpp +++ b/src/Data_Structures/Stack.hpp @@ -7,8 +7,7 @@ namespace mystd { -template -class stack +template class stack { public: stack() { data = vector(); } diff --git a/src/Data_Structures/Vector.hpp b/src/Data_Structures/Vector.hpp index d7bfee6..ce0233c 100644 --- a/src/Data_Structures/Vector.hpp +++ b/src/Data_Structures/Vector.hpp @@ -6,8 +6,7 @@ namespace mystd { -template -class vector +template class vector { public: vector() { reallocate(2); } From 736a702b59db9d39178081424949cb71202539a7 Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 07:47:48 -0500 Subject: [PATCH 10/13] make style changes according to other ones: --- src/Data_Structures/Priority_Queue.hpp | 18 ++-- tst/test_priority_queue.cpp | 118 ++++++++++++------------- 2 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/Data_Structures/Priority_Queue.hpp b/src/Data_Structures/Priority_Queue.hpp index 62f94c3..2cf5413 100644 --- a/src/Data_Structures/Priority_Queue.hpp +++ b/src/Data_Structures/Priority_Queue.hpp @@ -4,18 +4,18 @@ #include #include -namespace dataStructures +namespace mystd { -template class PriorityQueue +template class priority_queue { public: - PriorityQueue() + priority_queue() { sz = 0; reallocate(4); } - void Push(T item) + void push(T item) { if (sz == maxSz) { @@ -31,7 +31,7 @@ template class PriorityQueue sz++; } - const T &Top() const + const T &top() const { if (sz == 0) { @@ -40,7 +40,7 @@ template class PriorityQueue return tree[0]; } - T Pop() + T pop() { if (sz == 0) { @@ -53,9 +53,9 @@ template class PriorityQueue return toRet; } - bool Empty() const { return sz == 0; } + bool empty() const { return sz == 0; } - size_t Size() const { return sz; } + size_t size() const { return sz; } private: void refactor(size_t currInd) @@ -89,4 +89,4 @@ template class PriorityQueue size_t sz; size_t maxSz; }; -} // namespace dataStructures \ No newline at end of file +} // namespace mystd \ No newline at end of file diff --git a/tst/test_priority_queue.cpp b/tst/test_priority_queue.cpp index 4b7b70d..10551b6 100644 --- a/tst/test_priority_queue.cpp +++ b/tst/test_priority_queue.cpp @@ -2,102 +2,102 @@ #include #include -using namespace dataStructures; +using namespace mystd; TEST(PriorityTest, InitializeEmpty) { - PriorityQueue pq; - ASSERT_EQ(pq.Size(), 0); - ASSERT_TRUE(pq.Empty()); - ASSERT_THROW(pq.Pop(), std::out_of_range); + priority_queue pq; + ASSERT_EQ(pq.size(), 0); + ASSERT_TRUE(pq.empty()); + ASSERT_THROW(pq.pop(), std::out_of_range); } TEST(PriorityTest, PushPopSingle) { - PriorityQueue pq; - pq.Push(42); - ASSERT_EQ(pq.Size(), 1); - ASSERT_EQ(pq.Pop(), 42); - ASSERT_TRUE(pq.Empty()); + priority_queue pq; + pq.push(42); + ASSERT_EQ(pq.size(), 1); + ASSERT_EQ(pq.pop(), 42); + ASSERT_TRUE(pq.empty()); } TEST(PriorityTest, PushMultipleMaintainsOrder) { - PriorityQueue pq; - pq.Push(10); - pq.Push(50); - pq.Push(20); - pq.Push(40); - pq.Push(30); + priority_queue 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()); + 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) { - PriorityQueue pq; - pq.Push(5); - pq.Push(5); - pq.Push(5); - ASSERT_EQ(pq.Size(), 3); + priority_queue 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()); + ASSERT_EQ(pq.pop(), 5); + ASSERT_EQ(pq.pop(), 5); + ASSERT_EQ(pq.pop(), 5); + ASSERT_TRUE(pq.empty()); } TEST(PriorityTest, HandlesNegativeNumbers) { - PriorityQueue pq; - pq.Push(-10); - pq.Push(0); - pq.Push(-5); - pq.Push(5); + priority_queue 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()); + 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) { - PriorityQueue pq; + priority_queue pq; int N = 1000; for (int i = 0; i < N; ++i) { - pq.Push(i); + pq.push(i); } - ASSERT_EQ(pq.Size(), N); + ASSERT_EQ(pq.size(), N); for (int i = N - 1; i >= 0; --i) { - ASSERT_EQ(pq.Pop(), i); + ASSERT_EQ(pq.pop(), i); } - ASSERT_TRUE(pq.Empty()); + ASSERT_TRUE(pq.empty()); } TEST(PriorityTest, InterleavedPushPopOperations) { - PriorityQueue 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()); + priority_queue 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()); } From cb78fca93b323ef50ef358fe30ceca315b1cd3ae Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 07:51:52 -0500 Subject: [PATCH 11/13] better use of const and refs --- src/Data_Structures/Deque.hpp | 4 ++-- src/Data_Structures/Queue.hpp | 2 +- src/Data_Structures/Ringbuffer.hpp | 2 +- src/Data_Structures/Stack.hpp | 2 +- src/Data_Structures/Vector.hpp | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Data_Structures/Deque.hpp b/src/Data_Structures/Deque.hpp index dadb972..8d13865 100644 --- a/src/Data_Structures/Deque.hpp +++ b/src/Data_Structures/Deque.hpp @@ -71,7 +71,7 @@ template class deque currSize++; } - const T &pop_front() + T pop_front() { if (currSize == 0) { @@ -83,7 +83,7 @@ template class deque return toRet; } - const T &pop_back() + T pop_back() { if (currSize == 0) { diff --git a/src/Data_Structures/Queue.hpp b/src/Data_Structures/Queue.hpp index 024d747..1315631 100644 --- a/src/Data_Structures/Queue.hpp +++ b/src/Data_Structures/Queue.hpp @@ -46,7 +46,7 @@ template class queue currSize++; } - const T &pop() + T pop() { if (currSize == 0) { diff --git a/src/Data_Structures/Ringbuffer.hpp b/src/Data_Structures/Ringbuffer.hpp index 9ee986a..1a9f2d2 100644 --- a/src/Data_Structures/Ringbuffer.hpp +++ b/src/Data_Structures/Ringbuffer.hpp @@ -29,7 +29,7 @@ template class ringbuffer currSize++; } - const T &get_item() + T get_item() { if (currSize == 0) { diff --git a/src/Data_Structures/Stack.hpp b/src/Data_Structures/Stack.hpp index be3f6cc..cff7e50 100644 --- a/src/Data_Structures/Stack.hpp +++ b/src/Data_Structures/Stack.hpp @@ -14,7 +14,7 @@ template 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() { diff --git a/src/Data_Structures/Vector.hpp b/src/Data_Structures/Vector.hpp index ce0233c..36fe1ac 100644 --- a/src/Data_Structures/Vector.hpp +++ b/src/Data_Structures/Vector.hpp @@ -39,14 +39,14 @@ template 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; } From fc818b69a185e826ff39ada24f3b0b3de90118a0 Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 18:50:20 -0500 Subject: [PATCH 12/13] PQ fixes --- src/Algorithms/sort.hpp | 2 +- src/Data_Structures/Priority_Queue.hpp | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Algorithms/sort.hpp b/src/Algorithms/sort.hpp index dc84bd9..f7b3fb0 100644 --- a/src/Algorithms/sort.hpp +++ b/src/Algorithms/sort.hpp @@ -1,7 +1,7 @@ #pragma once #include -namespace algorithms +namespace mystd { template diff --git a/src/Data_Structures/Priority_Queue.hpp b/src/Data_Structures/Priority_Queue.hpp index 2cf5413..0a96233 100644 --- a/src/Data_Structures/Priority_Queue.hpp +++ b/src/Data_Structures/Priority_Queue.hpp @@ -3,16 +3,17 @@ #include #include #include +#include namespace mystd { template class priority_queue { public: - priority_queue() + priority_queue() : tree(4) { sz = 0; - reallocate(4); + maxSz = 4; } void push(T item) @@ -49,7 +50,7 @@ template class priority_queue T toRet = tree[0]; tree[0] = std::move(tree[sz - 1]); sz--; - refactor(0); + heapify(0); return toRet; } @@ -58,10 +59,10 @@ template class priority_queue size_t size() const { return sz; } private: - void refactor(size_t currInd) + void heapify(size_t currInd) { - size_t left = currInd * 2 + 1; - size_t right = currInd * 2 + 2; + 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; @@ -70,13 +71,13 @@ template class priority_queue if (newLargest != currInd) { std::swap(tree[currInd], tree[newLargest]); - refactor(newLargest); + heapify(newLargest); } } void reallocate(size_t newSize) { - std::unique_ptr newTree = std::make_unique(newSize); + array newTree = array(newSize); for (size_t i = 0; i < sz; i++) { newTree[i] = std::move(tree[i]); @@ -85,7 +86,7 @@ template class priority_queue maxSz = newSize; } - std::unique_ptr tree; + array tree; size_t sz; size_t maxSz; }; From 777f3f2f4902ca3b21d60adbb64a43460f44a186 Mon Sep 17 00:00:00 2001 From: minnce Date: Sat, 12 Apr 2025 18:55:29 -0500 Subject: [PATCH 13/13] formatting --- src/Data_Structures/Priority_Queue.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data_Structures/Priority_Queue.hpp b/src/Data_Structures/Priority_Queue.hpp index 0a96233..0632590 100644 --- a/src/Data_Structures/Priority_Queue.hpp +++ b/src/Data_Structures/Priority_Queue.hpp @@ -1,9 +1,9 @@ #pragma once +#include #include #include #include #include -#include namespace mystd {