From 5aadc1c6ca92300bf551d4ebb102db5bcbdec09f Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Tue, 14 Feb 2023 19:25:40 +0000 Subject: [PATCH 01/33] task1 --- task_01/src/stack.cpp | 6 +++--- task_01/src/stack.hpp | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/task_01/src/stack.cpp b/task_01/src/stack.cpp index 8ca89902..56a1e6b7 100644 --- a/task_01/src/stack.cpp +++ b/task_01/src/stack.cpp @@ -2,11 +2,11 @@ #include -void Stack::Push(int value) { data_.push(value); } +void Stack::Push(int value) { data_.push_back(value); } int Stack::Pop() { - auto result = data_.top(); - data_.pop(); + auto result = data_[data_.size() - 1]; + data_.pop_back(); return result; } diff --git a/task_01/src/stack.hpp b/task_01/src/stack.hpp index b784b235..774a58b5 100644 --- a/task_01/src/stack.hpp +++ b/task_01/src/stack.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include class Stack { @@ -9,7 +8,7 @@ class Stack { int Pop(); private: - std::stack data_; + std::vector data_; }; class MinStack { From c440ee90897637853c14504f1de6747cbd5ab952 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 25 Feb 2023 14:18:13 +0000 Subject: [PATCH 02/33] task3 --- task_03/src/sort.cpp | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 9380a0f4..03e2fc33 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -1,10 +1,40 @@ #include "sort.hpp" -#include #include + std::vector Sort(const std::vector& data) { - std::vector result = data; - std::sort(result.begin(), result.end()); + if (data.size() <= 1) + return data; + std::vector result; + std::vector v1; + std::vector v2; + for (int i = 0; i < data.size() / 2 + data.size() % 2; ++i) { + if (i == data.size() / 2 && data.size() % 2 == 1) { + v1.push_back(data[i]); + break; + } + v1.push_back(data[i]); + v2.push_back(data[i + data.size() / 2 + data.size() % 2]); + } + v1 = Sort(v1); + v2 = Sort(v2); + for (int i = 0, j = 0; i < v1.size() || j < v2.size(); ) { + if (v1[i] <= v2[j]) { + result.push_back(v1[i]); + ++i; + if (i == v1.size()) { + result.insert(result.end(), v2.begin() + j, v2.end()); + break; + } + } else { + result.push_back(v2[j]); + ++j; + if (j == v2.size()) { + result.insert(result.end(), v1.begin() + i, v1.end()); + break; + } + } + } return result; } \ No newline at end of file From 560a9ab2f6ed94b8659ab36242d3f46f47b859f6 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 4 Mar 2023 09:02:25 +0000 Subject: [PATCH 03/33] task2 --- task_02/src/heap.cpp | 53 ++++++++++++++++++++++++++++++++++++++++---- task_02/src/heap.hpp | 12 +++++++--- task_02/src/test.cpp | 38 +++++++++++++++---------------- 3 files changed, 77 insertions(+), 26 deletions(-) diff --git a/task_02/src/heap.cpp b/task_02/src/heap.cpp index d80490ec..ce67af85 100644 --- a/task_02/src/heap.cpp +++ b/task_02/src/heap.cpp @@ -3,13 +3,58 @@ #include #include -void MinHeap::Push(int n) { data_.push(n); } +void MinHeap::SiftDown() { + if (data_.size() <= 1) return; + int ind_num_down = 0; //start on the top + for (int i = 0; i < high_ - 1; ++i) { + if (data_[2 * i + 1] < data_[2 * i + 2] && data_[2 * i + 1] < data_[i]) { + int temp = data_[ind_num_down]; + data_[ind_num_down] = data_[2 * i + 1]; + data_[2 * i + 1] = temp; + ind_num_down = 2 * i + 1; + } else if (data_[2 * i + 1] > data_[2 * i + 2] && data_[2 * i + 2] < data_[i]) { + int temp = data_[ind_num_down]; + data_[ind_num_down] = data_[2 * i + 2]; + data_[2 * i + 2] = temp; + ind_num_down = 2 * i + 2; + } else break; // as low as possible + } +} + +void MinHeap::SiftUp(int ind_num_up) { + if (data_.size() <= 1) return; + for (int i = high_; i > 0; ++i) { + if (data_[ind_num_up] < data_[(ind_num_up - 1) / 2]) { + int temp = data_[ind_num_up]; + data_[ind_num_up] = data_[(ind_num_up - 1) / 2]; + data_[(ind_num_up - 1) / 2] = temp; + ind_num_up = (ind_num_up - 1) / 2; + } else break; + } +} -int MinHeap::Pop() { +void MinHeap::Push(int n) { + data_.push_back(n); + ChangeHigh(); + SiftUp(data_.size() - 1); +} + +int MinHeap::PopMin() { if (data_.empty()) throw std::out_of_range("Empty heap"); - auto result = data_.top(); - data_.pop(); + auto result = data_[0]; + data_[0] = data_[data_.size() - 1]; + data_.pop_back(); + ChangeHigh(); + SiftDown(); return result; } size_t MinHeap::Size() { return data_.size(); } + +void MinHeap::ChangeHigh() { + if (data_.size() <= 2) { + high_ = data_.size(); + return; + } + high_ = static_cast(floor(log2(data_.size() - 1)) + 1); +} \ No newline at end of file diff --git a/task_02/src/heap.hpp b/task_02/src/heap.hpp index 65b67d2e..b8954436 100644 --- a/task_02/src/heap.hpp +++ b/task_02/src/heap.hpp @@ -2,15 +2,21 @@ #include #include -#include #include +#include class MinHeap { public: void Push(int value); - int Pop(); + int PopMin(); + int GetMin(); size_t Size(); + private: - std::priority_queue, std::greater> data_; + std::vector data_ ; + void SiftUp(int); + void SiftDown(); + int high_ = 0; + void ChangeHigh(); }; diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 9bd764d1..a56f5427 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -6,7 +6,7 @@ TEST(HeapTest, Simple) { MinHeap heap; heap.Push(1); - ASSERT_EQ(heap.Pop(), 1); + ASSERT_EQ(heap.PopMin(), 1); } TEST(HeapTest, Basic) { @@ -14,17 +14,17 @@ TEST(HeapTest, Basic) { heap.Push(1); heap.Push(2); heap.Push(3); // [1, 2, 3] - ASSERT_EQ(heap.Pop(), 1); // [2, 3] -> 1 - ASSERT_EQ(heap.Pop(), 2); // [3] -> 2 - ASSERT_EQ(heap.Pop(), 3); // [] -> 3 + ASSERT_EQ(heap.PopMin(), 1); // [2, 3] -> 1 + ASSERT_EQ(heap.PopMin(), 2); // [3] -> 2 + ASSERT_EQ(heap.PopMin(), 3); // [] -> 3 } TEST(HeapTest, Empty) { MinHeap heap; - ASSERT_ANY_THROW(heap.Pop()); + ASSERT_ANY_THROW(heap.PopMin()); heap.Push(1); - ASSERT_EQ(heap.Pop(), 1); // [(1), 2, 3] - ASSERT_ANY_THROW(heap.Pop()); + ASSERT_EQ(heap.PopMin(), 1); // [(1), 2, 3] + ASSERT_ANY_THROW(heap.PopMin()); } TEST(HeapTest, Complex) { @@ -35,22 +35,22 @@ TEST(HeapTest, Complex) { heap.Push(3); // [1, 2, 3, 6] heap.Push(5); // [1, 2, 3, 5, 6] heap.Push(4); // [1, 2, 3, 4, 5, 6] - ASSERT_EQ(heap.Pop(), 1); // [2, 3, 4, 5, 6] -> 1 - ASSERT_EQ(heap.Pop(), 2); // [3, 4, 5, 6] -> 2 - ASSERT_EQ(heap.Pop(), 3); // [4, 5, 6] -> 3 - ASSERT_EQ(heap.Pop(), 4); // [5, 6] -> 4 - ASSERT_EQ(heap.Pop(), 5); // [6] -> 5 + ASSERT_EQ(heap.PopMin(), 1); // [2, 3, 4, 5, 6] -> 1 + ASSERT_EQ(heap.PopMin(), 2); // [3, 4, 5, 6] -> 2 + ASSERT_EQ(heap.PopMin(), 3); // [4, 5, 6] -> 3 + ASSERT_EQ(heap.PopMin(), 4); // [5, 6] -> 4 + ASSERT_EQ(heap.PopMin(), 5); // [6] -> 5 heap.Push(7); // [6, 7] heap.Push(1); // [1, 6, 7] heap.Push(2); // [1, 2, 6, 7] - ASSERT_EQ(heap.Pop(), 1); // [2, 6, 7] -> 1 + ASSERT_EQ(heap.PopMin(), 1); // [2, 6, 7] -> 1 heap.Push(3); // [2, 3, 6, 7] heap.Push(5); // [2, 3, 5, 6, 7] heap.Push(4); // [2, 3, 4, 5, 6, 7] - ASSERT_EQ(heap.Pop(), 2); // [3, 4, 5, 6, 7] -> 2 - ASSERT_EQ(heap.Pop(), 3); // [4, 5, 6, 7] -> 3 - ASSERT_EQ(heap.Pop(), 4); // [5, 6, 7] -> 4 - ASSERT_EQ(heap.Pop(), 5); // [6, 7] -> 5 - ASSERT_EQ(heap.Pop(), 6); // [7] -> 6 - ASSERT_EQ(heap.Pop(), 7); // [] -> 7 + ASSERT_EQ(heap.PopMin(), 2); // [3, 4, 5, 6, 7] -> 2 + ASSERT_EQ(heap.PopMin(), 3); // [4, 5, 6, 7] -> 3 + ASSERT_EQ(heap.PopMin(), 4); // [5, 6, 7] -> 4 + ASSERT_EQ(heap.PopMin(), 5); // [6, 7] -> 5 + ASSERT_EQ(heap.PopMin(), 6); // [7] -> 6 + ASSERT_EQ(heap.PopMin(), 7); // [] -> 7 } \ No newline at end of file From f3fe94420f6eb4587fc007ae27b34b9978e647aa Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 4 Mar 2023 09:27:52 +0000 Subject: [PATCH 04/33] additional test --- task_02/src/test.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index a56f5427..4c763264 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -53,4 +53,12 @@ TEST(HeapTest, Complex) { ASSERT_EQ(heap.PopMin(), 5); // [6, 7] -> 5 ASSERT_EQ(heap.PopMin(), 6); // [7] -> 6 ASSERT_EQ(heap.PopMin(), 7); // [] -> 7 +} + +TEST(HeapTest, Additional) { + MinHeap heap; + heap.Push(0); // [0] + heap.Push(-1); // [-1, 0] + heap.Push(-10); // [-10, -1, 0] + ASSERT_EQ(heap.PopMin(), -10); // [-1, 0] } \ No newline at end of file From 7e5a3f221c35dfd7215da645f5c9284127434c65 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:04:02 +0000 Subject: [PATCH 05/33] template was added --- task_02/src/heap.cpp | 60 ----------------------------------- task_02/src/heap.hpp | 75 +++++++++++++++++++++++++++++++++++++++++--- task_02/src/test.cpp | 24 +++++++++++--- 3 files changed, 90 insertions(+), 69 deletions(-) delete mode 100644 task_02/src/heap.cpp diff --git a/task_02/src/heap.cpp b/task_02/src/heap.cpp deleted file mode 100644 index ce67af85..00000000 --- a/task_02/src/heap.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "heap.hpp" - -#include -#include - -void MinHeap::SiftDown() { - if (data_.size() <= 1) return; - int ind_num_down = 0; //start on the top - for (int i = 0; i < high_ - 1; ++i) { - if (data_[2 * i + 1] < data_[2 * i + 2] && data_[2 * i + 1] < data_[i]) { - int temp = data_[ind_num_down]; - data_[ind_num_down] = data_[2 * i + 1]; - data_[2 * i + 1] = temp; - ind_num_down = 2 * i + 1; - } else if (data_[2 * i + 1] > data_[2 * i + 2] && data_[2 * i + 2] < data_[i]) { - int temp = data_[ind_num_down]; - data_[ind_num_down] = data_[2 * i + 2]; - data_[2 * i + 2] = temp; - ind_num_down = 2 * i + 2; - } else break; // as low as possible - } -} - -void MinHeap::SiftUp(int ind_num_up) { - if (data_.size() <= 1) return; - for (int i = high_; i > 0; ++i) { - if (data_[ind_num_up] < data_[(ind_num_up - 1) / 2]) { - int temp = data_[ind_num_up]; - data_[ind_num_up] = data_[(ind_num_up - 1) / 2]; - data_[(ind_num_up - 1) / 2] = temp; - ind_num_up = (ind_num_up - 1) / 2; - } else break; - } -} - -void MinHeap::Push(int n) { - data_.push_back(n); - ChangeHigh(); - SiftUp(data_.size() - 1); -} - -int MinHeap::PopMin() { - if (data_.empty()) throw std::out_of_range("Empty heap"); - auto result = data_[0]; - data_[0] = data_[data_.size() - 1]; - data_.pop_back(); - ChangeHigh(); - SiftDown(); - return result; -} - -size_t MinHeap::Size() { return data_.size(); } - -void MinHeap::ChangeHigh() { - if (data_.size() <= 2) { - high_ = data_.size(); - return; - } - high_ = static_cast(floor(log2(data_.size() - 1)) + 1); -} \ No newline at end of file diff --git a/task_02/src/heap.hpp b/task_02/src/heap.hpp index b8954436..260183d9 100644 --- a/task_02/src/heap.hpp +++ b/task_02/src/heap.hpp @@ -4,19 +4,86 @@ #include #include #include +#include +template class MinHeap { public: - void Push(int value); - int PopMin(); - int GetMin(); + void Push(T value); + T PopMin(); + T GetMin(); size_t Size(); private: - std::vector data_ ; + std::vector data_ ; void SiftUp(int); void SiftDown(); int high_ = 0; void ChangeHigh(); }; + +template +void MinHeap::SiftDown() { + if (data_.size() <= 1) return; + int ind_num_down = 0; // start on the top + for (int i = 0; i < high_ - 1; ++i) { + if (data_[2 * i + 1] < data_[2 * i + 2] && data_[2 * i + 1] < data_[i]) { + T temp = data_[ind_num_down]; + data_[ind_num_down] = data_[2 * i + 1]; + data_[2 * i + 1] = temp; + ind_num_down = 2 * i + 1; + } else if (data_[2 * i + 1] > data_[2 * i + 2] && data_[2 * i + 2] < data_[i]) { + T temp = data_[ind_num_down]; + data_[ind_num_down] = data_[2 * i + 2]; + data_[2 * i + 2] = temp; + ind_num_down = 2 * i + 2; + } else break; // as low as possible + } +} + +template +void MinHeap::SiftUp(int ind_num_up) { + if (data_.size() <= 1) return; + for (int i = high_; i > 0; ++i) { + if (data_[ind_num_up] < data_[(ind_num_up - 1) / 2]) { + T temp = data_[ind_num_up]; + data_[ind_num_up] = data_[(ind_num_up - 1) / 2]; + data_[(ind_num_up - 1) / 2] = temp; + ind_num_up = (ind_num_up - 1) / 2; + } else break; + } +} + +template +void MinHeap::Push(T n) { + data_.push_back(n); + ChangeHigh(); + SiftUp(data_.size() - 1); +} + +template +T MinHeap::PopMin() { + if (data_.empty()) throw std::out_of_range("Empty heap"); + auto result = data_[0]; + data_[0] = data_[data_.size() - 1]; + data_.pop_back(); + ChangeHigh(); + SiftDown(); + return result; +} + +template +size_t MinHeap::Size() { return data_.size(); } + +template +void MinHeap::ChangeHigh() { + if (data_.size() <= 2) { + high_ = data_.size(); + return; + } + high_ = static_cast(floor(log2(data_.size() - 1)) + 1); +} + +template +T MinHeap::GetMin() { return data_[0]; } \ No newline at end of file diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 4c763264..7692b148 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -4,13 +4,13 @@ #include "heap.hpp" TEST(HeapTest, Simple) { - MinHeap heap; + MinHeap heap; heap.Push(1); ASSERT_EQ(heap.PopMin(), 1); } TEST(HeapTest, Basic) { - MinHeap heap; + MinHeap heap; heap.Push(1); heap.Push(2); heap.Push(3); // [1, 2, 3] @@ -20,7 +20,7 @@ TEST(HeapTest, Basic) { } TEST(HeapTest, Empty) { - MinHeap heap; + MinHeap heap; ASSERT_ANY_THROW(heap.PopMin()); heap.Push(1); ASSERT_EQ(heap.PopMin(), 1); // [(1), 2, 3] @@ -28,7 +28,7 @@ TEST(HeapTest, Empty) { } TEST(HeapTest, Complex) { - MinHeap heap; + MinHeap heap; heap.Push(6); // [6] heap.Push(1); // [1, 6] heap.Push(2); // [1, 2, 6] @@ -56,9 +56,23 @@ TEST(HeapTest, Complex) { } TEST(HeapTest, Additional) { - MinHeap heap; + MinHeap heap; heap.Push(0); // [0] heap.Push(-1); // [-1, 0] heap.Push(-10); // [-10, -1, 0] + ASSERT_EQ(heap.Size(), 3); ASSERT_EQ(heap.PopMin(), -10); // [-1, 0] +} + +TEST(HeapTest, Double) { + MinHeap heap; + heap.Push(0.1); // [0.1] + heap.Push(-1.9); // [-1.9, 0.1] + heap.Push(-10.8); // [-10.8, -1.9, 0.1] + ASSERT_EQ(heap.Size(), 3); + ASSERT_EQ(heap.PopMin(), -10.8); // [-1.9, 0.1] + ASSERT_EQ(heap.PopMin(), -1.9); // [0.1] + ASSERT_EQ(heap.GetMin(), 0.1); // [0.1] + ASSERT_EQ(heap.PopMin(), 0.1); // [] + ASSERT_ANY_THROW(heap.PopMin()); } \ No newline at end of file From 11e0a5020fd34ef0d3276d5fd27abbb26256d570 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Tue, 7 Mar 2023 16:09:16 +0000 Subject: [PATCH 06/33] minstack with constant time --- task_01/src/stack.cpp | 18 ++++++++++++++++-- task_01/src/stack.hpp | 5 ++++- task_01/src/test.cpp | 8 ++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/task_01/src/stack.cpp b/task_01/src/stack.cpp index 56a1e6b7..3fab51a4 100644 --- a/task_01/src/stack.cpp +++ b/task_01/src/stack.cpp @@ -10,12 +10,26 @@ int Stack::Pop() { return result; } -void MinStack::Push(int value) { data_.push_back(value); } +bool Stack::IsEmpty() { return data_.empty(); } + +void MinStack::Push(int value) { + if (minimums_.IsEmpty()) { + minimums_.Push(value); + } else if (value <= data_.back()) { + minimums_.Push(value); + } + data_.push_back(value); + } int MinStack::Pop() { auto result = data_.back(); data_.pop_back(); + if (result == minimums_.GetLast()) { + minimums_.Pop(); + } return result; } -int MinStack::GetMin() { return *std::min_element(data_.begin(), data_.end()); } \ No newline at end of file +int Stack::GetLast() { return data_.back(); } + +int MinStack::GetMin() { return minimums_.GetLast(); } \ No newline at end of file diff --git a/task_01/src/stack.hpp b/task_01/src/stack.hpp index 774a58b5..6eeda9c1 100644 --- a/task_01/src/stack.hpp +++ b/task_01/src/stack.hpp @@ -6,12 +6,14 @@ class Stack { public: void Push(int value); int Pop(); + bool IsEmpty(); + int GetLast(); private: std::vector data_; }; -class MinStack { +class MinStack : Stack{ public: void Push(int value); int Pop(); @@ -19,4 +21,5 @@ class MinStack { private: std::vector data_; + Stack minimums_; }; \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index 54e7ce90..d87c95ea 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -39,4 +39,12 @@ TEST(MinStackTest, Simple) { ASSERT_EQ(stack.GetMin(), 1); ASSERT_EQ(stack.Pop(), 3); // Stack [1] ASSERT_EQ(stack.Pop(), 1); // Stack [] +} + +TEST(StackTest, Additional) { + Stack stack; + stack.Push(9); // Stack [9] + stack.Push(-3); // Stack [9, -3] + stack.Push(2); // Stack [9, 3, 2] + ASSERT_EQ(stack.GetLast(), 2); // Stack [9, 3, 2] } \ No newline at end of file From 19da0c0c29b789c868b7e7fd9fd047ea90a80a45 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 11 Mar 2023 09:46:18 +0000 Subject: [PATCH 07/33] format changes --- task_01/src/stack.cpp | 6 +++--- task_01/src/stack.hpp | 3 ++- task_01/src/test.cpp | 8 ++++---- task_02/src/heap.hpp | 30 ++++++++++++++++----------- task_02/src/test.cpp | 48 +++++++++++++++++++++---------------------- task_03/src/sort.cpp | 8 +++----- 6 files changed, 54 insertions(+), 49 deletions(-) diff --git a/task_01/src/stack.cpp b/task_01/src/stack.cpp index 3fab51a4..34fda5c2 100644 --- a/task_01/src/stack.cpp +++ b/task_01/src/stack.cpp @@ -12,19 +12,19 @@ int Stack::Pop() { bool Stack::IsEmpty() { return data_.empty(); } -void MinStack::Push(int value) { +void MinStack::Push(int value) { if (minimums_.IsEmpty()) { minimums_.Push(value); } else if (value <= data_.back()) { minimums_.Push(value); } data_.push_back(value); - } +} int MinStack::Pop() { auto result = data_.back(); data_.pop_back(); - if (result == minimums_.GetLast()) { + if (result == minimums_.GetLast()) { minimums_.Pop(); } return result; diff --git a/task_01/src/stack.hpp b/task_01/src/stack.hpp index 6eeda9c1..a8af7363 100644 --- a/task_01/src/stack.hpp +++ b/task_01/src/stack.hpp @@ -7,13 +7,14 @@ class Stack { void Push(int value); int Pop(); bool IsEmpty(); + int GetLast(); private: std::vector data_; }; -class MinStack : Stack{ +class MinStack : Stack { public: void Push(int value); int Pop(); diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index d87c95ea..fd509836 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -43,8 +43,8 @@ TEST(MinStackTest, Simple) { TEST(StackTest, Additional) { Stack stack; - stack.Push(9); // Stack [9] - stack.Push(-3); // Stack [9, -3] - stack.Push(2); // Stack [9, 3, 2] - ASSERT_EQ(stack.GetLast(), 2); // Stack [9, 3, 2] + stack.Push(9); // Stack [9] + stack.Push(-3); // Stack [9, -3] + stack.Push(2); // Stack [9, 3, 2] + ASSERT_EQ(stack.GetLast(), 2); // Stack [9, 3, 2] } \ No newline at end of file diff --git a/task_02/src/heap.hpp b/task_02/src/heap.hpp index 260183d9..8750de00 100644 --- a/task_02/src/heap.hpp +++ b/task_02/src/heap.hpp @@ -1,10 +1,10 @@ #pragma once +#include #include #include -#include -#include #include +#include template class MinHeap { @@ -14,9 +14,8 @@ class MinHeap { T GetMin(); size_t Size(); - private: - std::vector data_ ; + std::vector data_; void SiftUp(int); void SiftDown(); int high_ = 0; @@ -26,19 +25,21 @@ class MinHeap { template void MinHeap::SiftDown() { if (data_.size() <= 1) return; - int ind_num_down = 0; // start on the top + int ind_num_down = 0; // start on the top for (int i = 0; i < high_ - 1; ++i) { if (data_[2 * i + 1] < data_[2 * i + 2] && data_[2 * i + 1] < data_[i]) { T temp = data_[ind_num_down]; data_[ind_num_down] = data_[2 * i + 1]; data_[2 * i + 1] = temp; ind_num_down = 2 * i + 1; - } else if (data_[2 * i + 1] > data_[2 * i + 2] && data_[2 * i + 2] < data_[i]) { + } else if (data_[2 * i + 1] > data_[2 * i + 2] && + data_[2 * i + 2] < data_[i]) { T temp = data_[ind_num_down]; data_[ind_num_down] = data_[2 * i + 2]; data_[2 * i + 2] = temp; ind_num_down = 2 * i + 2; - } else break; // as low as possible + } else + break; // as low as possible } } @@ -51,12 +52,13 @@ void MinHeap::SiftUp(int ind_num_up) { data_[ind_num_up] = data_[(ind_num_up - 1) / 2]; data_[(ind_num_up - 1) / 2] = temp; ind_num_up = (ind_num_up - 1) / 2; - } else break; + } else + break; } } template -void MinHeap::Push(T n) { +void MinHeap::Push(T n) { data_.push_back(n); ChangeHigh(); SiftUp(data_.size() - 1); @@ -74,7 +76,9 @@ T MinHeap::PopMin() { } template -size_t MinHeap::Size() { return data_.size(); } +size_t MinHeap::Size() { + return data_.size(); +} template void MinHeap::ChangeHigh() { @@ -82,8 +86,10 @@ void MinHeap::ChangeHigh() { high_ = data_.size(); return; } - high_ = static_cast(floor(log2(data_.size() - 1)) + 1); + high_ = static_cast(floor(log2(data_.size() - 1)) + 1); } template -T MinHeap::GetMin() { return data_[0]; } \ No newline at end of file +T MinHeap::GetMin() { + return data_[0]; +} \ No newline at end of file diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 7692b148..067375e7 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -13,7 +13,7 @@ TEST(HeapTest, Basic) { MinHeap heap; heap.Push(1); heap.Push(2); - heap.Push(3); // [1, 2, 3] + heap.Push(3); // [1, 2, 3] ASSERT_EQ(heap.PopMin(), 1); // [2, 3] -> 1 ASSERT_EQ(heap.PopMin(), 2); // [3] -> 2 ASSERT_EQ(heap.PopMin(), 3); // [] -> 3 @@ -29,24 +29,24 @@ TEST(HeapTest, Empty) { TEST(HeapTest, Complex) { MinHeap heap; - heap.Push(6); // [6] - heap.Push(1); // [1, 6] - heap.Push(2); // [1, 2, 6] - heap.Push(3); // [1, 2, 3, 6] - heap.Push(5); // [1, 2, 3, 5, 6] - heap.Push(4); // [1, 2, 3, 4, 5, 6] + heap.Push(6); // [6] + heap.Push(1); // [1, 6] + heap.Push(2); // [1, 2, 6] + heap.Push(3); // [1, 2, 3, 6] + heap.Push(5); // [1, 2, 3, 5, 6] + heap.Push(4); // [1, 2, 3, 4, 5, 6] ASSERT_EQ(heap.PopMin(), 1); // [2, 3, 4, 5, 6] -> 1 ASSERT_EQ(heap.PopMin(), 2); // [3, 4, 5, 6] -> 2 ASSERT_EQ(heap.PopMin(), 3); // [4, 5, 6] -> 3 ASSERT_EQ(heap.PopMin(), 4); // [5, 6] -> 4 ASSERT_EQ(heap.PopMin(), 5); // [6] -> 5 - heap.Push(7); // [6, 7] - heap.Push(1); // [1, 6, 7] - heap.Push(2); // [1, 2, 6, 7] + heap.Push(7); // [6, 7] + heap.Push(1); // [1, 6, 7] + heap.Push(2); // [1, 2, 6, 7] ASSERT_EQ(heap.PopMin(), 1); // [2, 6, 7] -> 1 - heap.Push(3); // [2, 3, 6, 7] - heap.Push(5); // [2, 3, 5, 6, 7] - heap.Push(4); // [2, 3, 4, 5, 6, 7] + heap.Push(3); // [2, 3, 6, 7] + heap.Push(5); // [2, 3, 5, 6, 7] + heap.Push(4); // [2, 3, 4, 5, 6, 7] ASSERT_EQ(heap.PopMin(), 2); // [3, 4, 5, 6, 7] -> 2 ASSERT_EQ(heap.PopMin(), 3); // [4, 5, 6, 7] -> 3 ASSERT_EQ(heap.PopMin(), 4); // [5, 6, 7] -> 4 @@ -57,22 +57,22 @@ TEST(HeapTest, Complex) { TEST(HeapTest, Additional) { MinHeap heap; - heap.Push(0); // [0] - heap.Push(-1); // [-1, 0] - heap.Push(-10); // [-10, -1, 0] + heap.Push(0); // [0] + heap.Push(-1); // [-1, 0] + heap.Push(-10); // [-10, -1, 0] ASSERT_EQ(heap.Size(), 3); - ASSERT_EQ(heap.PopMin(), -10); // [-1, 0] + ASSERT_EQ(heap.PopMin(), -10); // [-1, 0] } TEST(HeapTest, Double) { MinHeap heap; - heap.Push(0.1); // [0.1] - heap.Push(-1.9); // [-1.9, 0.1] - heap.Push(-10.8); // [-10.8, -1.9, 0.1] + heap.Push(0.1); // [0.1] + heap.Push(-1.9); // [-1.9, 0.1] + heap.Push(-10.8); // [-10.8, -1.9, 0.1] ASSERT_EQ(heap.Size(), 3); - ASSERT_EQ(heap.PopMin(), -10.8); // [-1.9, 0.1] - ASSERT_EQ(heap.PopMin(), -1.9); // [0.1] - ASSERT_EQ(heap.GetMin(), 0.1); // [0.1] - ASSERT_EQ(heap.PopMin(), 0.1); // [] + ASSERT_EQ(heap.PopMin(), -10.8); // [-1.9, 0.1] + ASSERT_EQ(heap.PopMin(), -1.9); // [0.1] + ASSERT_EQ(heap.GetMin(), 0.1); // [0.1] + ASSERT_EQ(heap.PopMin(), 0.1); // [] ASSERT_ANY_THROW(heap.PopMin()); } \ No newline at end of file diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 03e2fc33..6baf1b5b 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -2,10 +2,8 @@ #include - std::vector Sort(const std::vector& data) { - if (data.size() <= 1) - return data; + if (data.size() <= 1) return data; std::vector result; std::vector v1; std::vector v2; @@ -19,7 +17,7 @@ std::vector Sort(const std::vector& data) { } v1 = Sort(v1); v2 = Sort(v2); - for (int i = 0, j = 0; i < v1.size() || j < v2.size(); ) { + for (int i = 0, j = 0; i < v1.size() || j < v2.size();) { if (v1[i] <= v2[j]) { result.push_back(v1[i]); ++i; @@ -31,7 +29,7 @@ std::vector Sort(const std::vector& data) { result.push_back(v2[j]); ++j; if (j == v2.size()) { - result.insert(result.end(), v1.begin() + i, v1.end()); + result.insert(result.end(), v1.begin() + i, v1.end()); break; } } From b0a9dbc33d0d80a425c8090d07e9fe6994f233ec Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 11 Mar 2023 10:03:38 +0000 Subject: [PATCH 08/33] add task --- additional_tasks/temperature/src/utils.cpp | 35 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/additional_tasks/temperature/src/utils.cpp b/additional_tasks/temperature/src/utils.cpp index e2f83df3..ba62fe18 100644 --- a/additional_tasks/temperature/src/utils.cpp +++ b/additional_tasks/temperature/src/utils.cpp @@ -1,8 +1,39 @@ #include "utils.hpp" #include +#include +#include std::vector CalculateDaysBeforWarmup( - const std::vector& temerature) { - return {}; + const std::vector& temperature) { + int n = temperature.size(); + std::stack> add_stack; + std::vector res(n); + + for (int i = n - 1; i > -1; --i) { + if (add_stack.empty()) { + add_stack.push(std::pair (temperature.at(i), i)); + res[i] = 0; + continue; + } + if (temperature[i] < add_stack.top().first) { + res[i] = add_stack.top().second - i; + add_stack.push(std::pair (temperature.at(i), i)); + continue; + } + if (temperature[i] >= add_stack.top().first) { + while (!add_stack.empty() && temperature[i] > add_stack.top().first) { + add_stack.pop(); + } + if (add_stack.empty()) { + add_stack.push(std::pair (temperature.at(i), i)); + res[i] = 0; + continue; + } + res[i] = add_stack.top().second - i; + add_stack.push(std::pair (temperature.at(i), i)); + continue; + } + } + return res; } From d40d1a2c2cc37fd89f46375ecfcd4644e53982c2 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 11 Mar 2023 11:13:52 +0000 Subject: [PATCH 09/33] sandbox --- sandbox/src/main.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 0e4393ba..1c46cf1e 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -1,3 +1,20 @@ #include +#include -int main() { return 0; } +int divCount (int n) { + for (int j = 2; j <= n / 5 + 1; ++j) { + if (n % static_cast(pow(5, j)) != 0) return j - 1; + } + return n / 5; +} + +int main() { + int n; + std::cin >> n; + int res = 0; + for (int i = 5; i <= n; i += 5) { + res += divCount(i); + } + std::cout << res; + return 0; +} From 5c64c415ca78acd472bd45adf1d76679f3fc2649 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 11 Mar 2023 11:30:24 +0000 Subject: [PATCH 10/33] format_3 --- sandbox/src/main.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index e612c32d..a2affcf9 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -1,24 +1,22 @@ -#include -<<<<<<< HEAD #include -======= + +#include #include ->>>>>>> ccdf496ec5018f0db7f3af688634a35ad2d83c2a -int divCount (int n) { - for (int j = 2; j <= n / 5 + 1; ++j) { - if (n % static_cast(pow(5, j)) != 0) return j - 1; - } - return n / 5; +int divCount(int n) { + for (int j = 2; j <= n / 5 + 1; ++j) { + if (n % static_cast(pow(5, j)) != 0) return j - 1; + } + return n / 5; } int main() { - int n; - std::cin >> n; - int res = 0; - for (int i = 5; i <= n; i += 5) { - res += divCount(i); - } - std::cout << res; - return 0; + int n; + std::cin >> n; + int res = 0; + for (int i = 5; i <= n; i += 5) { + res += divCount(i); + } + std::cout << res; + return 0; } From 51fcd436f9cbd5ddfca9b4a88aa71ae5c137b5ff Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 18 Mar 2023 16:20:12 +0000 Subject: [PATCH 11/33] task4 --- task_04/src/order_statistics.cpp | 64 +++++++++++++++++++++++++++++--- task_04/src/order_statistics.hpp | 2 +- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/task_04/src/order_statistics.cpp b/task_04/src/order_statistics.cpp index 94e0defd..8d512a53 100644 --- a/task_04/src/order_statistics.cpp +++ b/task_04/src/order_statistics.cpp @@ -1,11 +1,65 @@ #include "order_statistics.hpp" -#include +#include -int GetOrderStatistics(const std::vector& data, size_t n) { - std::vector tmp(data); +int GenRandInd(const int n) { + std::random_device rd; // non-deterministic generator + std::mt19937 gen(rd()); // to seed mersenne twister. + std::uniform_int_distribution<> dist(0, n - 1); + return dist(gen); +} + +int QuickSelect(const std::vector& d, const size_t n) { + std::vector data{d}; + if (data.size() == 1) return data[0]; + int pivot = GenRandInd(data.size()); + + int i = 0; + int j = data.size() - 1; + while (i < pivot || j > pivot) { + while (i < pivot && data[i] <= data[pivot]) + ++i; + while (data[j] >= data[pivot] && j > pivot) + --j; + if (i < pivot && j > pivot) { + int temp = data[i]; + data[i] = data[j]; + data[j] = temp; + } + if (i == pivot && j > pivot) { + int temp = data[pivot]; + data[pivot] = data[j]; + data[j] = temp; + i = 0; + } + + if (j == pivot && i < pivot) { + int temp = data[pivot]; + data[pivot] = data[i]; + data[i] = temp; + j = data.size() - 1; + } + + } - std::sort(tmp.begin(), tmp.end()); + if (pivot == n) return data[pivot]; + + if (n < pivot) { + std::vector temp(pivot); + std::copy(data.begin(), data.begin() + pivot, temp.begin()); + return QuickSelect(temp, n); + } + + if (n > pivot) { + std::vector temp(d.size() - pivot - 1); + std::copy(data.begin() + pivot + 1, data.end(), temp.begin()); + return QuickSelect(temp, n - pivot - 1); + } + +} + +int GetOrderStatistics(const std::vector& data, size_t n) { + int temp = QuickSelect(data, n); - return tmp[n]; + return temp; } diff --git a/task_04/src/order_statistics.hpp b/task_04/src/order_statistics.hpp index 05332cdd..d4294670 100644 --- a/task_04/src/order_statistics.hpp +++ b/task_04/src/order_statistics.hpp @@ -2,4 +2,4 @@ #include -int GetOrderStatistics(const std::vector& data, size_t n); +int GetOrderStatistics(const std::vector& data, const size_t n); From 197af9aa299cc8e8a975d67b89d3329306200dd7 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 18 Mar 2023 16:24:47 +0000 Subject: [PATCH 12/33] format --- task_04/src/order_statistics.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/task_04/src/order_statistics.cpp b/task_04/src/order_statistics.cpp index 8d512a53..83ed2a01 100644 --- a/task_04/src/order_statistics.cpp +++ b/task_04/src/order_statistics.cpp @@ -17,10 +17,8 @@ int QuickSelect(const std::vector& d, const size_t n) { int i = 0; int j = data.size() - 1; while (i < pivot || j > pivot) { - while (i < pivot && data[i] <= data[pivot]) - ++i; - while (data[j] >= data[pivot] && j > pivot) - --j; + while (i < pivot && data[i] <= data[pivot]) ++i; + while (data[j] >= data[pivot] && j > pivot) --j; if (i < pivot && j > pivot) { int temp = data[i]; data[i] = data[j]; @@ -39,23 +37,21 @@ int QuickSelect(const std::vector& d, const size_t n) { data[i] = temp; j = data.size() - 1; } - - } + } - if (pivot == n) return data[pivot]; + if (pivot == n) return data[pivot]; - if (n < pivot) { - std::vector temp(pivot); - std::copy(data.begin(), data.begin() + pivot, temp.begin()); - return QuickSelect(temp, n); - } + if (n < pivot) { + std::vector temp(pivot); + std::copy(data.begin(), data.begin() + pivot, temp.begin()); + return QuickSelect(temp, n); + } - if (n > pivot) { - std::vector temp(d.size() - pivot - 1); - std::copy(data.begin() + pivot + 1, data.end(), temp.begin()); - return QuickSelect(temp, n - pivot - 1); - } - + if (n > pivot) { + std::vector temp(d.size() - pivot - 1); + std::copy(data.begin() + pivot + 1, data.end(), temp.begin()); + return QuickSelect(temp, n - pivot - 1); + } } int GetOrderStatistics(const std::vector& data, size_t n) { From 1b876fcea6e9823753c37131af644ce2c11b61cc Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 25 Mar 2023 20:34:14 +0000 Subject: [PATCH 13/33] correction --- task_02/src/heap.hpp | 35 +++++++++++++------------- task_03/src/sort.cpp | 43 ++++++++++++++++---------------- task_04/src/order_statistics.cpp | 8 +++--- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/task_02/src/heap.hpp b/task_02/src/heap.hpp index 8750de00..f2943976 100644 --- a/task_02/src/heap.hpp +++ b/task_02/src/heap.hpp @@ -18,26 +18,25 @@ class MinHeap { std::vector data_; void SiftUp(int); void SiftDown(); - int high_ = 0; + int height_ = 0; void ChangeHigh(); }; template void MinHeap::SiftDown() { if (data_.size() <= 1) return; - int ind_num_down = 0; // start on the top - for (int i = 0; i < high_ - 1; ++i) { - if (data_[2 * i + 1] < data_[2 * i + 2] && data_[2 * i + 1] < data_[i]) { - T temp = data_[ind_num_down]; - data_[ind_num_down] = data_[2 * i + 1]; - data_[2 * i + 1] = temp; - ind_num_down = 2 * i + 1; - } else if (data_[2 * i + 1] > data_[2 * i + 2] && - data_[2 * i + 2] < data_[i]) { - T temp = data_[ind_num_down]; - data_[ind_num_down] = data_[2 * i + 2]; - data_[2 * i + 2] = temp; - ind_num_down = 2 * i + 2; + int curr_ind = 0; // start on the top + for (int i = 0, j = 1; i < height_ - 1; ++i, j = 2 * i + 1) { + if (data_[j] < data_[j + 1] && data_[j] < data_[i]) { + T temp = data_[curr_ind]; + data_[curr_ind] = data_[j]; + data_[j] = temp; + curr_ind = j; + } else if (data_[j] > data_[j + 1] && data_[j + 1] < data_[i]) { + T temp = data_[curr_ind]; + data_[curr_ind] = data_[j + 1]; + data_[j + 1] = temp; + curr_ind = j + 1; } else break; // as low as possible } @@ -46,7 +45,7 @@ void MinHeap::SiftDown() { template void MinHeap::SiftUp(int ind_num_up) { if (data_.size() <= 1) return; - for (int i = high_; i > 0; ++i) { + for (int i = height_; i > 0; ++i) { if (data_[ind_num_up] < data_[(ind_num_up - 1) / 2]) { T temp = data_[ind_num_up]; data_[ind_num_up] = data_[(ind_num_up - 1) / 2]; @@ -68,7 +67,7 @@ template T MinHeap::PopMin() { if (data_.empty()) throw std::out_of_range("Empty heap"); auto result = data_[0]; - data_[0] = data_[data_.size() - 1]; + data_[0] = data_.back(); data_.pop_back(); ChangeHigh(); SiftDown(); @@ -83,10 +82,10 @@ size_t MinHeap::Size() { template void MinHeap::ChangeHigh() { if (data_.size() <= 2) { - high_ = data_.size(); + height_ = data_.size(); return; } - high_ = static_cast(floor(log2(data_.size() - 1)) + 1); + height_ = static_cast(floor(log2(data_.size() - 1)) + 1); } template diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 6baf1b5b..13ff0dec 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -2,37 +2,38 @@ #include -std::vector Sort(const std::vector& data) { - if (data.size() <= 1) return data; - std::vector result; - std::vector v1; - std::vector v2; - for (int i = 0; i < data.size() / 2 + data.size() % 2; ++i) { - if (i == data.size() / 2 && data.size() % 2 == 1) { - v1.push_back(data[i]); +std::vector Sort(const std::vector& input) { + if (input.size() <= 1) return input; + std::vector output; + std::vector smaller_part; + std::vector bigger_part; + for (int i = 0; i < input.size() / 2 + input.size() % 2; ++i) { + if (i == input.size() / 2 && input.size() % 2 == 1) { + smaller_part.push_back(input[i]); break; } - v1.push_back(data[i]); - v2.push_back(data[i + data.size() / 2 + data.size() % 2]); + smaller_part.push_back(input[i]); + bigger_part.push_back(input[i + input.size() / 2 + input.size() % 2]); } - v1 = Sort(v1); - v2 = Sort(v2); - for (int i = 0, j = 0; i < v1.size() || j < v2.size();) { - if (v1[i] <= v2[j]) { - result.push_back(v1[i]); + smaller_part = Sort(smaller_part); + bigger_part = Sort(bigger_part); + for (int i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { + if (smaller_part[i] <= bigger_part[j]) { + output.push_back(smaller_part[i]); ++i; - if (i == v1.size()) { - result.insert(result.end(), v2.begin() + j, v2.end()); + if (i == smaller_part.size()) { + output.insert(output.end(), bigger_part.begin() + j, bigger_part.end()); break; } } else { - result.push_back(v2[j]); + output.push_back(bigger_part[j]); ++j; - if (j == v2.size()) { - result.insert(result.end(), v1.begin() + i, v1.end()); + if (j == bigger_part.size()) { + output.insert(output.end(), smaller_part.begin() + i, + smaller_part.end()); break; } } } - return result; + return output; } \ No newline at end of file diff --git a/task_04/src/order_statistics.cpp b/task_04/src/order_statistics.cpp index 83ed2a01..5449ca33 100644 --- a/task_04/src/order_statistics.cpp +++ b/task_04/src/order_statistics.cpp @@ -2,15 +2,15 @@ #include -int GenRandInd(const int n) { +size_t GenRandInd(const size_t n) { std::random_device rd; // non-deterministic generator std::mt19937 gen(rd()); // to seed mersenne twister. std::uniform_int_distribution<> dist(0, n - 1); return dist(gen); } -int QuickSelect(const std::vector& d, const size_t n) { - std::vector data{d}; +int QuickSelect(const std::vector& input, const size_t n) { + std::vector data{input}; if (data.size() == 1) return data[0]; int pivot = GenRandInd(data.size()); @@ -48,7 +48,7 @@ int QuickSelect(const std::vector& d, const size_t n) { } if (n > pivot) { - std::vector temp(d.size() - pivot - 1); + std::vector temp(input.size() - pivot - 1); std::copy(data.begin() + pivot + 1, data.end(), temp.begin()); return QuickSelect(temp, n - pivot - 1); } From ffc652effbfdc5b34b086bebfd3f72ff0940a7ba Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 8 Apr 2023 19:52:14 +0000 Subject: [PATCH 14/33] task 5 and 6 doesn't finished --- task_05/src/test.cpp | 6 +++++- task_05/src/tree.cpp | 38 ++++++++++++++++++++++++++++++++++--- task_05/src/tree.hpp | 7 +++++++ task_06/src/hashtable.cpp | 40 +++++++++++++++++++++++++++++++-------- task_06/src/hashtable.hpp | 9 ++++++--- 5 files changed, 85 insertions(+), 15 deletions(-) diff --git a/task_05/src/test.cpp b/task_05/src/test.cpp index 8ca70db8..e6b8bbe2 100644 --- a/task_05/src/test.cpp +++ b/task_05/src/test.cpp @@ -5,4 +5,8 @@ #include "tree.hpp" -TEST(Tree, Simple) { Tree tree; } \ No newline at end of file +TEST(Tree, Simple) { + Tree tree; + ASSERT_EQ(tree.Insert(1, 2), true); // 1: 2 + ASSERT_EQ(tree.Insert(1, 2), false); // 1: 2 +} \ No newline at end of file diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index 052bb00c..5b426edf 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -1,9 +1,41 @@ #include "tree.hpp" +#include "exception" + +struct Tree::Node { + std::shared_ptr parent = nullptr; + std::shared_ptr leftChild = nullptr; + std::shared_ptr rightChild = nullptr; + int value; + int key; + Node(int k, int v) : key{k}, value{v} {} +}; + Tree::Tree() {} -bool Tree::Insert(int key, int value) { return true; } +bool Tree::Insert(int key, int value) { + if (root == nullptr) { + root = std::shared_ptr(std::make_shared(Node(key, value))); + return true; + } + if (root->key < key) return Tree(root->leftChild).Insert(key, value); + if (root->key > key) return Tree(root->rightChild).Insert(key, value); + if (root->key == key) return false; +} + +void Tree::InsertOrUpdate(int key, int value) { + if (root == nullptr) + root = std::shared_ptr(std::make_shared(Node(key, value))); + if (root->key < key) Tree(root->leftChild).InsertOrUpdate(key, value); + if (root->key > key) Tree(root->rightChild).InsertOrUpdate(key, value); + if (root->key == key) root->value = value; +} -void Tree::InsertOrUpdate(int key, int value) {} +int Tree::Find(int key) const { + if (root == nullptr) throw std::out_of_range("No such key\n"); + if (root->key < key) Tree(root->leftChild).Find(key); + if (root->key > key) Tree(root->rightChild).Find(key); + if (root->key == key) return root->value; +} -int Tree::Find(int key) const { return 0; } +void Tree::Delete(int key) {} \ No newline at end of file diff --git a/task_05/src/tree.hpp b/task_05/src/tree.hpp index 584309c0..79f81efd 100644 --- a/task_05/src/tree.hpp +++ b/task_05/src/tree.hpp @@ -1,9 +1,16 @@ #pragma once +#include class Tree { + struct Node; + public: + std::shared_ptr root = nullptr; Tree(); + Tree(std::shared_ptr r) : root{r} {}; + bool Insert(int key, int value); void InsertOrUpdate(int key, int value); int Find(int key) const; + void Delete(int key); }; diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 87d5f1e0..0e8dca0a 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -1,25 +1,49 @@ #include "hashtable.hpp" #include +#include +#include #include -HashTable::HashTable() {} +HashTable::HashTable(int k) : data_(k) {} + +bool HashTable::Contains(const std::string& key) { + int hash = hasher_(key) % data_.size(); + if (data_[hash].empty()) return false; + for (int i = 0; i < data_[hash].size(); ++i) + if (data_[hash][i].first == key) return true; + return false; +} bool HashTable::Insert(const std::string& key, int value) { - if (data_.contains(key)) return false; - data_[key] = value; + int hash = hasher_(key) % data_.size(); + if (Contains(key)) return false; + data_[hash].push_back(std::pair(key, value)); + keys_count_ += 1; return true; } void HashTable::InsertOrUpdate(const std::string& key, int value) { - data_[key] = value; + int hash = hasher_(key) % data_.size(); + if (!Contains(key)) { + keys_count_ += 1; + data_[hash].push_back(std::pair(key, value)); + } else + for (int i = 0; i < data_[hash].size(); ++i) + if (data_[hash][i].first == key) data_[hash][i].second = value; } void HashTable::Remove(const std::string& key) { - auto it = data_.find(key); - data_.erase(it); + int hash = hasher_(key) % data_.size(); + if (!Contains(key)) throw std::out_of_range("no such key"); + for (int i = 0; i < data_[hash].size(); ++i) + if (data_[hash][i].first == key) data_[hash].erase(data_[hash].begin() + i); } -int HashTable::Find(const std::string& key) const { return data_.at(key); } +int HashTable::Find(const std::string& key) const { + int hash = hasher_(key) % data_.size(); + for (int i = 0; i < data_[hash].size(); ++i) + if (data_[hash][i].first == key) return data_.at(hash).at(i).second; +} -size_t HashTable::Size() const { return data_.size(); } +size_t HashTable::Size() const { return keys_count_; } diff --git a/task_06/src/hashtable.hpp b/task_06/src/hashtable.hpp index 743b3f98..a65af76a 100644 --- a/task_06/src/hashtable.hpp +++ b/task_06/src/hashtable.hpp @@ -2,17 +2,20 @@ #include #include -#include +#include class HashTable { public: - HashTable(); + HashTable(int k = 20); bool Insert(const std::string& key, int value); void InsertOrUpdate(const std::string& key, int value); void Remove(const std::string& key); int Find(const std::string& key) const; size_t Size() const; + bool Contains(const std::string& key); private: - std::unordered_map data_; + std::vector>> data_; + std::hash hasher_; + size_t keys_count_ = 0; }; From dbec1b557725113ce7a1f06ee42c121995b14e12 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 8 Apr 2023 20:50:43 +0000 Subject: [PATCH 15/33] format --- task_06/src/hashtable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 0e8dca0a..5d2595fa 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -13,7 +13,7 @@ bool HashTable::Contains(const std::string& key) { for (int i = 0; i < data_[hash].size(); ++i) if (data_[hash][i].first == key) return true; return false; -} +} bool HashTable::Insert(const std::string& key, int value) { int hash = hasher_(key) % data_.size(); From 50f698b2727ff15217fba84c68f70233fb2ccbb8 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:29:09 +0000 Subject: [PATCH 16/33] correction error --- additional_tasks/temperature/src/utils.cpp | 33 +++++++++++++--------- task_01/src/stack.hpp | 11 ++++---- task_01/src/test.cpp | 5 ++++ task_02/src/heap.hpp | 32 ++++++++++----------- task_02/src/test.cpp | 1 + task_03/src/sort.cpp | 5 ++-- task_04/src/order_statistics.cpp | 4 +-- 7 files changed, 52 insertions(+), 39 deletions(-) diff --git a/additional_tasks/temperature/src/utils.cpp b/additional_tasks/temperature/src/utils.cpp index 6fa88b57..53092986 100644 --- a/additional_tasks/temperature/src/utils.cpp +++ b/additional_tasks/temperature/src/utils.cpp @@ -4,34 +4,41 @@ #include #include +struct Day { + float temperature; + int index; + Day(float temperature, int index) : temperature{temperature}, index{index} {} +}; + std::vector CalculateDaysBeforWarmup( const std::vector& temperature) { - int n = temperature.size(); - std::stack> add_stack; - std::vector res(n); + size_t size = temperature.size(); + std::stack add_stack; + std::vector res(size); - for (int i = n - 1; i > -1; --i) { + for (int i = size - 1; i > -1; --i) { if (add_stack.empty()) { - add_stack.push(std::pair(temperature.at(i), i)); + add_stack.push(Day(temperature.at(i), i)); res[i] = 0; continue; } - if (temperature[i] < add_stack.top().first) { - res[i] = add_stack.top().second - i; - add_stack.push(std::pair(temperature.at(i), i)); + if (temperature[i] < add_stack.top().temperature) { + res[i] = add_stack.top().index - i; + add_stack.push(Day(temperature.at(i), i)); continue; } - if (temperature[i] >= add_stack.top().first) { - while (!add_stack.empty() && temperature[i] > add_stack.top().first) { + if (temperature[i] >= add_stack.top().temperature) { + while (!add_stack.empty() && + temperature[i] > add_stack.top().temperature) { add_stack.pop(); } if (add_stack.empty()) { - add_stack.push(std::pair(temperature.at(i), i)); + add_stack.push(Day(temperature.at(i), i)); res[i] = 0; continue; } - res[i] = add_stack.top().second - i; - add_stack.push(std::pair(temperature.at(i), i)); + res[i] = add_stack.top().index - i; + add_stack.push(Day(temperature.at(i), i)); continue; } } diff --git a/task_01/src/stack.hpp b/task_01/src/stack.hpp index a8af7363..985cf767 100644 --- a/task_01/src/stack.hpp +++ b/task_01/src/stack.hpp @@ -4,23 +4,22 @@ class Stack { public: - void Push(int value); - int Pop(); + virtual void Push(int value); + virtual int Pop(); bool IsEmpty(); int GetLast(); - private: + protected: std::vector data_; }; class MinStack : Stack { public: - void Push(int value); - int Pop(); + void Push(int value) override; + int Pop() override; int GetMin(); private: - std::vector data_; Stack minimums_; }; \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index fd509836..102fa0e2 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -47,4 +47,9 @@ TEST(StackTest, Additional) { stack.Push(-3); // Stack [9, -3] stack.Push(2); // Stack [9, 3, 2] ASSERT_EQ(stack.GetLast(), 2); // Stack [9, 3, 2] + stack.Push(5); + stack.Push(4); + stack.Push(4); + stack.Pop(); + ASSERT_EQ(stack.GetLast(), 4); } \ No newline at end of file diff --git a/task_02/src/heap.hpp b/task_02/src/heap.hpp index f2943976..5fabe96d 100644 --- a/task_02/src/heap.hpp +++ b/task_02/src/heap.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -16,41 +17,39 @@ class MinHeap { private: std::vector data_; - void SiftUp(int); + void SiftUp(int currindex); void SiftDown(); - int height_ = 0; void ChangeHigh(); + size_t height_ = 0; }; template void MinHeap::SiftDown() { if (data_.size() <= 1) return; - int curr_ind = 0; // start on the top + int curr_index = 0; // start on the top for (int i = 0, j = 1; i < height_ - 1; ++i, j = 2 * i + 1) { if (data_[j] < data_[j + 1] && data_[j] < data_[i]) { - T temp = data_[curr_ind]; - data_[curr_ind] = data_[j]; - data_[j] = temp; - curr_ind = j; + std::swap(data_[curr_index], data_[j]); + curr_index = j; } else if (data_[j] > data_[j + 1] && data_[j + 1] < data_[i]) { - T temp = data_[curr_ind]; - data_[curr_ind] = data_[j + 1]; + T temp = data_[curr_index]; + data_[curr_index] = data_[j + 1]; data_[j + 1] = temp; - curr_ind = j + 1; + curr_index = j + 1; } else break; // as low as possible } } template -void MinHeap::SiftUp(int ind_num_up) { +void MinHeap::SiftUp(int curr_index) { if (data_.size() <= 1) return; for (int i = height_; i > 0; ++i) { - if (data_[ind_num_up] < data_[(ind_num_up - 1) / 2]) { - T temp = data_[ind_num_up]; - data_[ind_num_up] = data_[(ind_num_up - 1) / 2]; - data_[(ind_num_up - 1) / 2] = temp; - ind_num_up = (ind_num_up - 1) / 2; + if (data_[curr_index] < data_[(curr_index - 1) / 2]) { + T temp = data_[curr_index]; + data_[curr_index] = data_[(curr_index - 1) / 2]; + data_[(curr_index - 1) / 2] = temp; + curr_index = (curr_index - 1) / 2; } else break; } @@ -90,5 +89,6 @@ void MinHeap::ChangeHigh() { template T MinHeap::GetMin() { + if (data_.empty()) throw std::out_of_range("Empty heap"); return data_[0]; } \ No newline at end of file diff --git a/task_02/src/test.cpp b/task_02/src/test.cpp index 067375e7..57f93455 100644 --- a/task_02/src/test.cpp +++ b/task_02/src/test.cpp @@ -25,6 +25,7 @@ TEST(HeapTest, Empty) { heap.Push(1); ASSERT_EQ(heap.PopMin(), 1); // [(1), 2, 3] ASSERT_ANY_THROW(heap.PopMin()); + ASSERT_ANY_THROW(heap.GetMin()); } TEST(HeapTest, Complex) { diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 13ff0dec..e83adca7 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -7,13 +7,14 @@ std::vector Sort(const std::vector& input) { std::vector output; std::vector smaller_part; std::vector bigger_part; - for (int i = 0; i < input.size() / 2 + input.size() % 2; ++i) { + size_t middle = input.size() / 2 + input.size() % 2; + for (int i = 0; i < middle; ++i) { if (i == input.size() / 2 && input.size() % 2 == 1) { smaller_part.push_back(input[i]); break; } smaller_part.push_back(input[i]); - bigger_part.push_back(input[i + input.size() / 2 + input.size() % 2]); + bigger_part.push_back(input[i + middle]); } smaller_part = Sort(smaller_part); bigger_part = Sort(bigger_part); diff --git a/task_04/src/order_statistics.cpp b/task_04/src/order_statistics.cpp index 5449ca33..d431c920 100644 --- a/task_04/src/order_statistics.cpp +++ b/task_04/src/order_statistics.cpp @@ -2,7 +2,7 @@ #include -size_t GenRandInd(const size_t n) { +size_t GenRandIndex(const size_t n) { std::random_device rd; // non-deterministic generator std::mt19937 gen(rd()); // to seed mersenne twister. std::uniform_int_distribution<> dist(0, n - 1); @@ -12,7 +12,7 @@ size_t GenRandInd(const size_t n) { int QuickSelect(const std::vector& input, const size_t n) { std::vector data{input}; if (data.size() == 1) return data[0]; - int pivot = GenRandInd(data.size()); + int pivot = GenRandIndex(data.size()); int i = 0; int j = data.size() - 1; From 8a44013fcd5282dc1c0a178c9a3d1445204c0267 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 22 Apr 2023 15:15:32 +0000 Subject: [PATCH 17/33] without deleting and tests --- task_05/src/tree.cpp | 143 +++++++++++++++++++++++++++++++++++++------ task_05/src/tree.hpp | 33 +++++++++- 2 files changed, 155 insertions(+), 21 deletions(-) diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index 5b426edf..decacfca 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -2,40 +2,145 @@ #include "exception" -struct Tree::Node { - std::shared_ptr parent = nullptr; - std::shared_ptr leftChild = nullptr; - std::shared_ptr rightChild = nullptr; - int value; - int key; - Node(int k, int v) : key{k}, value{v} {} -}; +bool operator==(const Tree::Node &left, const Tree::Node &right) { + if (left.parent != right.parent) return false; + if (left.leftChild != right.leftChild) return false; + if (left.rightChild != right.rightChild) return false; + return true; +} + +bool operator!=(const Tree::Node &left, const Tree::Node &right) { + return !(left == right); +} + +Tree::Node::Node(Node &node) + : parent{node.parent}, + leftChild{node.leftChild}, + rightChild{node.rightChild}, + key{node.key}, + value{node.value} {} Tree::Tree() {} +Tree::Node::Node(int k, int v) : key{k}, value{v} {} + bool Tree::Insert(int key, int value) { + auto result = InsertWithoutSplay(key, value); + Splay(result.first); + return result.second; +} + +std::pair Tree::InsertWithoutSplay(int key, int value) { if (root == nullptr) { root = std::shared_ptr(std::make_shared(Node(key, value))); - return true; + return std::pair(*(root.get()), true); } - if (root->key < key) return Tree(root->leftChild).Insert(key, value); - if (root->key > key) return Tree(root->rightChild).Insert(key, value); - if (root->key == key) return false; + if (root->key < key) + return (Tree(root->leftChild).InsertWithoutSplay(key, value)); + if (root->key > key) + return Tree(root->rightChild).InsertWithoutSplay(key, value); + if (root->key == key) + return std::pair(*(root.get()), false); } void Tree::InsertOrUpdate(int key, int value) { - if (root == nullptr) + auto result = InsertOrUpdateWithoutSplay(key, value); + Splay(result); +} + +Tree::Node &Tree::InsertOrUpdateWithoutSplay(int key, int value) { + if (root == nullptr) { root = std::shared_ptr(std::make_shared(Node(key, value))); - if (root->key < key) Tree(root->leftChild).InsertOrUpdate(key, value); - if (root->key > key) Tree(root->rightChild).InsertOrUpdate(key, value); - if (root->key == key) root->value = value; + return *(root.get()); + } + if (root->key < key) + return Tree(root->leftChild).InsertOrUpdateWithoutSplay(key, value); + if (root->key > key) + return Tree(root->rightChild).InsertOrUpdateWithoutSplay(key, value); + if (root->key == key) { + root->value = value; + return *(root.get()); + } } -int Tree::Find(int key) const { +int Tree::Find(int key) { + auto result = FindWithoutSplay(key); + Splay(result.first); + return result.second; +} + +std::pair Tree::FindWithoutSplay(int key) { if (root == nullptr) throw std::out_of_range("No such key\n"); if (root->key < key) Tree(root->leftChild).Find(key); if (root->key > key) Tree(root->rightChild).Find(key); - if (root->key == key) return root->value; + if (root->key == key) + return std::pair(*(root.get()), root->value); +} + +void Tree::Delete(int key) {} + +void Tree::Zig(Node &element) { + if (!element.parent) return; + if (element == *(element.parent->leftChild.get())) { + auto old_parent = element.parent; + auto old_left_child = element.leftChild; + auto old_right_child = element.rightChild; + *(element.parent->parent) = element; + element.rightChild = element.parent; + element.parent = old_parent->parent; + *(old_parent->leftChild) = element; + old_parent->leftChild = old_right_child; + old_right_child->parent = old_parent; + } + if (element == *(element.parent->rightChild.get())) { + auto old_parent = element.parent; + auto old_right_child = element.rightChild; + auto old_left_child = element.leftChild; + *(element.parent->parent) = element; + element.leftChild = element.parent; + element.parent = old_parent->parent; + *(old_parent->rightChild) = element; + old_parent->rightChild = old_left_child; + old_left_child->parent = old_parent; + } +} + +void Tree::ZigZig(Node &element) { + Zig(*element.parent); + Zig(element); +} + +void Tree::ZigZag(Node &element) { + Zig(element); + Zig(element); +} + +void Tree::Splay(Node &element) { + while (element != *(root.get())) { + if (this->HaveGrandpa(element) && IsDirectChild(element)) ZigZig(element); + if (this->HaveGrandpa(element) && !IsDirectChild(element)) ZigZag(element); + if (!(this->HaveGrandpa(element))) Zig(element); + } +} + +bool Tree::IsDirectChild(const Node &element) { + if (IsLeftChild(element) && IsLeftChild(*(element.parent))) return true; + if (IsRightChild(element) && IsRightChild(*(element.parent))) return true; + return false; +} + +bool Tree::IsLeftChild(const Node &element) { + if (element == *(element.parent->leftChild.get())) return true; + return false; +} + +bool Tree::IsRightChild(const Node &element) { + if (element == *(element.parent->rightChild.get())) return true; + return false; } -void Tree::Delete(int key) {} \ No newline at end of file +bool Tree::HaveGrandpa(const Node &element) { + if (element.parent == nullptr) return false; + if (element.parent->parent != nullptr) return true; + return false; +} \ No newline at end of file diff --git a/task_05/src/tree.hpp b/task_05/src/tree.hpp index 79f81efd..9c73ce93 100644 --- a/task_05/src/tree.hpp +++ b/task_05/src/tree.hpp @@ -2,7 +2,31 @@ #include class Tree { - struct Node; + public: + struct Node { + std::shared_ptr parent = nullptr; + std::shared_ptr leftChild = nullptr; + std::shared_ptr rightChild = nullptr; + int value; + int key; + Node(int k, int v); + Node(Node& node); + + Node(const Node& other) = default; + Node& operator=(const Node& rhs) = default; + + Node(Node&& other) = default; + Node& operator=(Node&& rhs) = default; + }; + + private: + void Zig(Node& element); + void ZigZig(Node& element); + void ZigZag(Node& element); + void Splay(Node& element); + std::pair InsertWithoutSplay(int key, int value); + Node& InsertOrUpdateWithoutSplay(int key, int value); + std::pair FindWithoutSplay(int key); public: std::shared_ptr root = nullptr; @@ -11,6 +35,11 @@ class Tree { bool Insert(int key, int value); void InsertOrUpdate(int key, int value); - int Find(int key) const; + int Find(int key); void Delete(int key); + + bool IsLeftChild(const Node& element); + bool IsRightChild(const Node& element); + bool IsDirectChild(const Node& element); + bool HaveGrandpa(const Node& element); }; From 95a7117628e4a605e3fceba944648e689d818522 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 22 Apr 2023 16:14:01 +0000 Subject: [PATCH 18/33] without tests --- task_05/src/tree.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index decacfca..634d7c20 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -77,7 +77,60 @@ std::pair Tree::FindWithoutSplay(int key) { return std::pair(*(root.get()), root->value); } -void Tree::Delete(int key) {} +void Tree::Delete(int key) { + auto deleting_node = FindWithoutSplay(key).first; + if (deleting_node.leftChild == nullptr && + deleting_node.rightChild == nullptr) { + if (*(root.get()) != deleting_node) { + if (IsLeftChild(deleting_node)) + deleting_node.parent->leftChild = nullptr; + else + deleting_node.parent->rightChild = nullptr; + } + } else + root = nullptr; + + if (deleting_node.leftChild == nullptr && + deleting_node.rightChild != nullptr) { + if (*(root.get()) != deleting_node) { + if (IsLeftChild(deleting_node)) + deleting_node.parent->leftChild = deleting_node.rightChild; + else + deleting_node.parent->rightChild = deleting_node.rightChild; + deleting_node.rightChild = deleting_node.parent; + } + } + + if (deleting_node.leftChild != nullptr && + deleting_node.rightChild == nullptr) { + if (*(root.get()) != deleting_node) { + if (IsLeftChild(deleting_node)) + deleting_node.parent->leftChild = deleting_node.leftChild; + else + deleting_node.parent->rightChild = deleting_node.leftChild; + deleting_node.leftChild = deleting_node.parent; + } + } + + if (deleting_node.leftChild != nullptr && + deleting_node.rightChild != nullptr) { + auto new_node = deleting_node.rightChild; + while (new_node->leftChild != nullptr) new_node = new_node->leftChild; + if (IsLeftChild(*(new_node.get()))) + new_node->parent->leftChild = nullptr; + else + new_node->parent->rightChild = nullptr; + new_node->parent = deleting_node.parent; + new_node->leftChild = deleting_node.leftChild; + new_node->rightChild = deleting_node.rightChild; + if (IsLeftChild(*(new_node.get()))) + deleting_node.parent->leftChild = new_node; + else + deleting_node.parent->rightChild = new_node; + deleting_node.leftChild->parent = new_node; + deleting_node.rightChild->parent = new_node; + } +} void Tree::Zig(Node &element) { if (!element.parent) return; From 53f411905934329d3d6e7b66a53ccc6a08245a1e Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 29 Apr 2023 07:55:12 +0000 Subject: [PATCH 19/33] findsum --- additional_tasks/CMakeLists.txt | 1 + .../find_sum_numbers/src/test.cpp | 10 ++- .../find_sum_numbers/src/utils.cpp | 11 +++- .../find_sum_numbers/src/utils.hpp | 1 + additional_tasks/temperature/src/utils.cpp | 2 +- task_01/src/stack.cpp | 5 +- task_02/src/heap.hpp | 8 +-- task_04/src/order_statistics.cpp | 64 ++++++++----------- 8 files changed, 55 insertions(+), 47 deletions(-) diff --git a/additional_tasks/CMakeLists.txt b/additional_tasks/CMakeLists.txt index e298bd2e..f017dfdb 100644 --- a/additional_tasks/CMakeLists.txt +++ b/additional_tasks/CMakeLists.txt @@ -3,3 +3,4 @@ cmake_minimum_required(VERSION 3.10) project(additional_tasks) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/temperature) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/find_sum_numbers) diff --git a/additional_tasks/find_sum_numbers/src/test.cpp b/additional_tasks/find_sum_numbers/src/test.cpp index a017e409..fe734e9e 100644 --- a/additional_tasks/find_sum_numbers/src/test.cpp +++ b/additional_tasks/find_sum_numbers/src/test.cpp @@ -5,4 +5,12 @@ #include "utils.hpp" -TEST(GetNumbersOfSum, Simple) {} \ No newline at end of file +TEST(GetNumbersOfSum, Simple) { + ASSERT_EQ(GetNumbersOfSum(std::vector{-10, -2, 0, 3, 6, 7, 11, 14}, 3), + (std::pair(0, 3))); + ASSERT_ANY_THROW(GetNumbersOfSum(std::vector{}, 3)); + ASSERT_EQ(GetNumbersOfSum(std::vector{0}, 3), + (std::pair(0, 0))); + ASSERT_EQ(GetNumbersOfSum(std::vector{1, 2, 4, 4, 7}, 9), + (std::pair(2, 7))); +} \ No newline at end of file diff --git a/additional_tasks/find_sum_numbers/src/utils.cpp b/additional_tasks/find_sum_numbers/src/utils.cpp index 7ad97cdc..e1ad1e81 100644 --- a/additional_tasks/find_sum_numbers/src/utils.cpp +++ b/additional_tasks/find_sum_numbers/src/utils.cpp @@ -3,5 +3,14 @@ #include std::pair GetNumbersOfSum(const std::vector& numbers, int sum) { - return {0, 0}; + if (numbers.empty()) throw std::out_of_range("Empty list of numbers"); + int i = 0; + int j = numbers.size() - 1; + while (i < j) { + if (numbers.at(i) + numbers.at(j) == sum) + return std::pair(numbers.at(i), numbers.at(j)); + if (numbers.at(i) + numbers.at(j) < sum) ++i; + if (numbers.at(i) + numbers.at(j) > sum) --j; + } + return std::pair(0, 0); } \ No newline at end of file diff --git a/additional_tasks/find_sum_numbers/src/utils.hpp b/additional_tasks/find_sum_numbers/src/utils.hpp index 6b4ca38d..ea688a1c 100644 --- a/additional_tasks/find_sum_numbers/src/utils.hpp +++ b/additional_tasks/find_sum_numbers/src/utils.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include std::pair GetNumbersOfSum(const std::vector& numbers, int sum); diff --git a/additional_tasks/temperature/src/utils.cpp b/additional_tasks/temperature/src/utils.cpp index 53092986..4f149dd8 100644 --- a/additional_tasks/temperature/src/utils.cpp +++ b/additional_tasks/temperature/src/utils.cpp @@ -12,7 +12,7 @@ struct Day { std::vector CalculateDaysBeforWarmup( const std::vector& temperature) { - size_t size = temperature.size(); + const size_t size = temperature.size(); std::stack add_stack; std::vector res(size); diff --git a/task_01/src/stack.cpp b/task_01/src/stack.cpp index 34fda5c2..ac78eeaa 100644 --- a/task_01/src/stack.cpp +++ b/task_01/src/stack.cpp @@ -1,11 +1,13 @@ #include "stack.hpp" #include +#include void Stack::Push(int value) { data_.push_back(value); } int Stack::Pop() { - auto result = data_[data_.size() - 1]; + if (data_.empty()) throw std::out_of_range{"Empty stack"}; + auto result = data_.back(); data_.pop_back(); return result; } @@ -22,6 +24,7 @@ void MinStack::Push(int value) { } int MinStack::Pop() { + if (data_.empty()) throw std::out_of_range{"Empty stack"}; auto result = data_.back(); data_.pop_back(); if (result == minimums_.GetLast()) { diff --git a/task_02/src/heap.hpp b/task_02/src/heap.hpp index 5fabe96d..e6186786 100644 --- a/task_02/src/heap.hpp +++ b/task_02/src/heap.hpp @@ -32,9 +32,7 @@ void MinHeap::SiftDown() { std::swap(data_[curr_index], data_[j]); curr_index = j; } else if (data_[j] > data_[j + 1] && data_[j + 1] < data_[i]) { - T temp = data_[curr_index]; - data_[curr_index] = data_[j + 1]; - data_[j + 1] = temp; + std::swap(data_[curr_index], data_[j + 1]); curr_index = j + 1; } else break; // as low as possible @@ -46,9 +44,7 @@ void MinHeap::SiftUp(int curr_index) { if (data_.size() <= 1) return; for (int i = height_; i > 0; ++i) { if (data_[curr_index] < data_[(curr_index - 1) / 2]) { - T temp = data_[curr_index]; - data_[curr_index] = data_[(curr_index - 1) / 2]; - data_[(curr_index - 1) / 2] = temp; + std::swap(data_[curr_index], data_[(curr_index - 1) / 2]); curr_index = (curr_index - 1) / 2; } else break; diff --git a/task_04/src/order_statistics.cpp b/task_04/src/order_statistics.cpp index d431c920..6b190b18 100644 --- a/task_04/src/order_statistics.cpp +++ b/task_04/src/order_statistics.cpp @@ -9,48 +9,38 @@ size_t GenRandIndex(const size_t n) { return dist(gen); } -int QuickSelect(const std::vector& input, const size_t n) { +int QuickSelect(const std::vector& input, const size_t k) { std::vector data{input}; if (data.size() == 1) return data[0]; int pivot = GenRandIndex(data.size()); - int i = 0; - int j = data.size() - 1; - while (i < pivot || j > pivot) { - while (i < pivot && data[i] <= data[pivot]) ++i; - while (data[j] >= data[pivot] && j > pivot) --j; - if (i < pivot && j > pivot) { - int temp = data[i]; - data[i] = data[j]; - data[j] = temp; + int start = 0; + int end = data.size(); + std::swap(data[pivot], data[end - 1]); + pivot = end - 1; + int i = start, j = start; + + while (true) { + for (; j < end - 1;) { + if (data[j] <= data[pivot]) { + std::swap(data[i], data[j]); + ++i; + ++j; + } + if (data[j] > data[pivot]) ++j; } - if (i == pivot && j > pivot) { - int temp = data[pivot]; - data[pivot] = data[j]; - data[j] = temp; - i = 0; - } - - if (j == pivot && i < pivot) { - int temp = data[pivot]; - data[pivot] = data[i]; - data[i] = temp; - j = data.size() - 1; - } - } - - if (pivot == n) return data[pivot]; - - if (n < pivot) { - std::vector temp(pivot); - std::copy(data.begin(), data.begin() + pivot, temp.begin()); - return QuickSelect(temp, n); - } - - if (n > pivot) { - std::vector temp(input.size() - pivot - 1); - std::copy(data.begin() + pivot + 1, data.end(), temp.begin()); - return QuickSelect(temp, n - pivot - 1); + std::swap(data[i], data[pivot]); + if (i == k) return data[i]; + if (i > k) + end = i; + else if (i < k) + start = i; + + pivot = start + GenRandIndex(end - start); + std::swap(data[pivot], data[end - 1]); + pivot = end - 1; + i = start; + j = start; } } From 838f206011062d0d7ba9525e395f6f740a1b0b55 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Mon, 8 May 2023 17:01:00 +0000 Subject: [PATCH 20/33] add rehash --- task_06/src/hashtable.cpp | 58 +++++++++++++++++++++++++-------------- task_06/src/hashtable.hpp | 5 +++- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 5d2595fa..9abc8e64 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -5,45 +5,63 @@ #include #include -HashTable::HashTable(int k) : data_(k) {} +HashTable::HashTable(size_t width, size_t height) + : width_(width), height_(height) { + for (int i = 0; i < width; ++i) + data_.push_back(std::vector>(height)); +} bool HashTable::Contains(const std::string& key) { - int hash = hasher_(key) % data_.size(); - if (data_[hash].empty()) return false; - for (int i = 0; i < data_[hash].size(); ++i) - if (data_[hash][i].first == key) return true; + int hash_w = hasher_(key) % width_; + int hash_h = hasher_(key) % height_; + if (data_[hash_w][hash_h].first == key) return true; return false; } bool HashTable::Insert(const std::string& key, int value) { - int hash = hasher_(key) % data_.size(); + int hash_w = hasher_(key) % width_; + int hash_h = hasher_(key) % height_; if (Contains(key)) return false; - data_[hash].push_back(std::pair(key, value)); + data_[hash_w][hash_h] = std::pair(key, value); keys_count_ += 1; return true; } void HashTable::InsertOrUpdate(const std::string& key, int value) { - int hash = hasher_(key) % data_.size(); - if (!Contains(key)) { - keys_count_ += 1; - data_[hash].push_back(std::pair(key, value)); - } else - for (int i = 0; i < data_[hash].size(); ++i) - if (data_[hash][i].first == key) data_[hash][i].second = value; + int hash_w = hasher_(key) % width_; + int hash_h = hasher_(key) % height_; + if (!Contains(key)) keys_count_ += 1; + data_[hash_w][hash_h] = std::pair(key, value); } void HashTable::Remove(const std::string& key) { - int hash = hasher_(key) % data_.size(); + int hash_w = hasher_(key) % width_; + int hash_h = hasher_(key) % height_; if (!Contains(key)) throw std::out_of_range("no such key"); - for (int i = 0; i < data_[hash].size(); ++i) - if (data_[hash][i].first == key) data_[hash].erase(data_[hash].begin() + i); + keys_count_ -= 1; + data_[hash_w][hash_h] = std::pair(); } int HashTable::Find(const std::string& key) const { - int hash = hasher_(key) % data_.size(); - for (int i = 0; i < data_[hash].size(); ++i) - if (data_[hash][i].first == key) return data_.at(hash).at(i).second; + int hash_w = hasher_(key) % width_; + int hash_h = hasher_(key) % height_; + return data_.at(hash_w).at(hash_h).second; } size_t HashTable::Size() const { return keys_count_; } + +void HashTable::Rehash() { + if (keys_count_ < width_ * height_ * 0.8 && + (keys_count_ > width_ * height_ * 0.2 || width_ <= 20 && height_ <= 10)) + return; + + HashTable new_hash_table; + if (keys_count_ >= width_ * height_ * 0.8) + new_hash_table = HashTable(width_ * 2, height_ * 2); + + for (int i = 0; i < width_; ++i) + for (int j = 0; j < height_; ++j) + new_hash_table.Insert(data_.at(i).at(j).first, data_.at(i).at(j).second); + + *this = new_hash_table; +} diff --git a/task_06/src/hashtable.hpp b/task_06/src/hashtable.hpp index a65af76a..cfe9d361 100644 --- a/task_06/src/hashtable.hpp +++ b/task_06/src/hashtable.hpp @@ -6,7 +6,7 @@ class HashTable { public: - HashTable(int k = 20); + HashTable(size_t width = 20, size_t height = 10); bool Insert(const std::string& key, int value); void InsertOrUpdate(const std::string& key, int value); void Remove(const std::string& key); @@ -18,4 +18,7 @@ class HashTable { std::vector>> data_; std::hash hasher_; size_t keys_count_ = 0; + size_t width_ = 20; // count of vector in data + size_t height_ = 10; // count of numbers in each vector + void Rehash(); }; From 9ccff5d2e0a48f149f0e218b59633155f928323f Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Mon, 8 May 2023 17:20:29 +0000 Subject: [PATCH 21/33] add rehash --- task_06/src/hashtable.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 9abc8e64..9a5995b6 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -24,6 +24,7 @@ bool HashTable::Insert(const std::string& key, int value) { if (Contains(key)) return false; data_[hash_w][hash_h] = std::pair(key, value); keys_count_ += 1; + this->Rehash(); return true; } @@ -32,6 +33,7 @@ void HashTable::InsertOrUpdate(const std::string& key, int value) { int hash_h = hasher_(key) % height_; if (!Contains(key)) keys_count_ += 1; data_[hash_w][hash_h] = std::pair(key, value); + this->Rehash(); } void HashTable::Remove(const std::string& key) { @@ -40,6 +42,7 @@ void HashTable::Remove(const std::string& key) { if (!Contains(key)) throw std::out_of_range("no such key"); keys_count_ -= 1; data_[hash_w][hash_h] = std::pair(); + this->Rehash(); } int HashTable::Find(const std::string& key) const { @@ -62,6 +65,6 @@ void HashTable::Rehash() { for (int i = 0; i < width_; ++i) for (int j = 0; j < height_; ++j) new_hash_table.Insert(data_.at(i).at(j).first, data_.at(i).at(j).second); - + *this = new_hash_table; -} +} \ No newline at end of file From bb75145c9beb8d2ea404ccd9059a5fb502c00240 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Sat, 17 Jun 2023 17:13:25 +0000 Subject: [PATCH 22/33] correction --- task_03/src/sort.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index e83adca7..71eb6e6d 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -5,17 +5,10 @@ std::vector Sort(const std::vector& input) { if (input.size() <= 1) return input; std::vector output; - std::vector smaller_part; - std::vector bigger_part; size_t middle = input.size() / 2 + input.size() % 2; - for (int i = 0; i < middle; ++i) { - if (i == input.size() / 2 && input.size() % 2 == 1) { - smaller_part.push_back(input[i]); - break; - } - smaller_part.push_back(input[i]); - bigger_part.push_back(input[i + middle]); - } + std::vector smaller_part(input.begin(), input.begin() + middle); + std::vector bigger_part(input.begin() + middle, input.end()); + smaller_part = Sort(smaller_part); bigger_part = Sort(bigger_part); for (int i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { From 69e5c74296d5b166c026c8a5251fbd38943f5ff5 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Tue, 20 Jun 2023 22:21:26 +0000 Subject: [PATCH 23/33] correction --- task_03/src/sort.cpp | 2 +- task_04/src/order_statistics.cpp | 12 +-- task_05/src/tree.cpp | 128 ++++++++++++++++--------------- task_05/src/tree.hpp | 9 +-- task_06/src/hashtable.cpp | 45 ++++++----- task_06/src/hashtable.hpp | 3 +- 6 files changed, 102 insertions(+), 97 deletions(-) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 71eb6e6d..543a651e 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -11,7 +11,7 @@ std::vector Sort(const std::vector& input) { smaller_part = Sort(smaller_part); bigger_part = Sort(bigger_part); - for (int i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { + for (size_t i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { if (smaller_part[i] <= bigger_part[j]) { output.push_back(smaller_part[i]); ++i; diff --git a/task_04/src/order_statistics.cpp b/task_04/src/order_statistics.cpp index 6b190b18..c7866e51 100644 --- a/task_04/src/order_statistics.cpp +++ b/task_04/src/order_statistics.cpp @@ -1,6 +1,7 @@ #include "order_statistics.hpp" #include +#include size_t GenRandIndex(const size_t n) { std::random_device rd; // non-deterministic generator @@ -10,18 +11,19 @@ size_t GenRandIndex(const size_t n) { } int QuickSelect(const std::vector& input, const size_t k) { + if (input.size() < k) throw std::out_of_range("Too big statistic"); std::vector data{input}; if (data.size() == 1) return data[0]; - int pivot = GenRandIndex(data.size()); + size_t pivot = GenRandIndex(data.size()); - int start = 0; - int end = data.size(); + size_t start = 0; + size_t end = data.size(); std::swap(data[pivot], data[end - 1]); pivot = end - 1; - int i = start, j = start; + size_t i = start, j = start; while (true) { - for (; j < end - 1;) { + for (; j + 1 < end;) { if (data[j] <= data[pivot]) { std::swap(data[i], data[j]); ++i; diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index 634d7c20..7dd6249f 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -3,9 +3,9 @@ #include "exception" bool operator==(const Tree::Node &left, const Tree::Node &right) { - if (left.parent != right.parent) return false; - if (left.leftChild != right.leftChild) return false; - if (left.rightChild != right.rightChild) return false; + if (left.parent.lock() != right.parent.lock()) return false; + if (left.left_child != right.left_child) return false; + if (left.right_child != right.right_child) return false; return true; } @@ -13,10 +13,10 @@ bool operator!=(const Tree::Node &left, const Tree::Node &right) { return !(left == right); } -Tree::Node::Node(Node &node) +Tree::Node::Node(const Node &node) : parent{node.parent}, - leftChild{node.leftChild}, - rightChild{node.rightChild}, + left_child{node.left_child}, + right_child{node.right_child}, key{node.key}, value{node.value} {} @@ -32,13 +32,13 @@ bool Tree::Insert(int key, int value) { std::pair Tree::InsertWithoutSplay(int key, int value) { if (root == nullptr) { - root = std::shared_ptr(std::make_shared(Node(key, value))); + root = std::make_shared(Node(key, value)); return std::pair(*(root.get()), true); } if (root->key < key) - return (Tree(root->leftChild).InsertWithoutSplay(key, value)); + return (Tree(root->left_child).InsertWithoutSplay(key, value)); if (root->key > key) - return Tree(root->rightChild).InsertWithoutSplay(key, value); + return Tree(root->right_child).InsertWithoutSplay(key, value); if (root->key == key) return std::pair(*(root.get()), false); } @@ -54,9 +54,9 @@ Tree::Node &Tree::InsertOrUpdateWithoutSplay(int key, int value) { return *(root.get()); } if (root->key < key) - return Tree(root->leftChild).InsertOrUpdateWithoutSplay(key, value); + return Tree(root->left_child).InsertOrUpdateWithoutSplay(key, value); if (root->key > key) - return Tree(root->rightChild).InsertOrUpdateWithoutSplay(key, value); + return Tree(root->right_child).InsertOrUpdateWithoutSplay(key, value); if (root->key == key) { root->value = value; return *(root.get()); @@ -71,95 +71,95 @@ int Tree::Find(int key) { std::pair Tree::FindWithoutSplay(int key) { if (root == nullptr) throw std::out_of_range("No such key\n"); - if (root->key < key) Tree(root->leftChild).Find(key); - if (root->key > key) Tree(root->rightChild).Find(key); + if (root->key < key) Tree(root->left_child).Find(key); + if (root->key > key) Tree(root->right_child).Find(key); if (root->key == key) return std::pair(*(root.get()), root->value); } void Tree::Delete(int key) { auto deleting_node = FindWithoutSplay(key).first; - if (deleting_node.leftChild == nullptr && - deleting_node.rightChild == nullptr) { + if (deleting_node.left_child == nullptr && + deleting_node.right_child == nullptr) { if (*(root.get()) != deleting_node) { if (IsLeftChild(deleting_node)) - deleting_node.parent->leftChild = nullptr; + deleting_node.parent.lock()->left_child = nullptr; else - deleting_node.parent->rightChild = nullptr; + deleting_node.parent.lock()->right_child = nullptr; } } else root = nullptr; - if (deleting_node.leftChild == nullptr && - deleting_node.rightChild != nullptr) { + if (deleting_node.left_child == nullptr && + deleting_node.right_child != nullptr) { if (*(root.get()) != deleting_node) { if (IsLeftChild(deleting_node)) - deleting_node.parent->leftChild = deleting_node.rightChild; + deleting_node.parent.lock()->left_child = deleting_node.right_child; else - deleting_node.parent->rightChild = deleting_node.rightChild; - deleting_node.rightChild = deleting_node.parent; + deleting_node.parent.lock()->right_child = deleting_node.right_child; + deleting_node.right_child = deleting_node.parent.lock(); } } - if (deleting_node.leftChild != nullptr && - deleting_node.rightChild == nullptr) { + if (deleting_node.left_child != nullptr && + deleting_node.right_child == nullptr) { if (*(root.get()) != deleting_node) { if (IsLeftChild(deleting_node)) - deleting_node.parent->leftChild = deleting_node.leftChild; + deleting_node.parent.lock()->left_child = deleting_node.left_child; else - deleting_node.parent->rightChild = deleting_node.leftChild; - deleting_node.leftChild = deleting_node.parent; + deleting_node.parent.lock()->right_child = deleting_node.left_child; + deleting_node.left_child = deleting_node.parent.lock(); } } - if (deleting_node.leftChild != nullptr && - deleting_node.rightChild != nullptr) { - auto new_node = deleting_node.rightChild; - while (new_node->leftChild != nullptr) new_node = new_node->leftChild; + if (deleting_node.left_child != nullptr && + deleting_node.right_child != nullptr) { + auto new_node = deleting_node.right_child; + while (new_node->left_child != nullptr) new_node = new_node->left_child; if (IsLeftChild(*(new_node.get()))) - new_node->parent->leftChild = nullptr; + new_node->parent.lock()->left_child = nullptr; else - new_node->parent->rightChild = nullptr; + new_node->parent.lock()->right_child = nullptr; new_node->parent = deleting_node.parent; - new_node->leftChild = deleting_node.leftChild; - new_node->rightChild = deleting_node.rightChild; + new_node->left_child = deleting_node.left_child; + new_node->right_child = deleting_node.right_child; if (IsLeftChild(*(new_node.get()))) - deleting_node.parent->leftChild = new_node; + deleting_node.parent.lock()->left_child = new_node; else - deleting_node.parent->rightChild = new_node; - deleting_node.leftChild->parent = new_node; - deleting_node.rightChild->parent = new_node; + deleting_node.parent.lock()->right_child = new_node; + deleting_node.left_child->parent = new_node; + deleting_node.right_child->parent = new_node; } } void Tree::Zig(Node &element) { - if (!element.parent) return; - if (element == *(element.parent->leftChild.get())) { + if (!element.parent.lock()) return; + if (element == *(element.parent.lock()->left_child.get())) { auto old_parent = element.parent; - auto old_left_child = element.leftChild; - auto old_right_child = element.rightChild; - *(element.parent->parent) = element; - element.rightChild = element.parent; - element.parent = old_parent->parent; - *(old_parent->leftChild) = element; - old_parent->leftChild = old_right_child; + auto old_left_child = element.left_child; + auto old_right_child = element.right_child; + *(element.parent.lock()->parent.lock()) = element; + element.right_child = element.parent.lock(); + element.parent = old_parent.lock()->parent; + *(old_parent.lock()->left_child) = element; + old_parent.lock()->left_child = old_right_child; old_right_child->parent = old_parent; } - if (element == *(element.parent->rightChild.get())) { + if (element == *(element.parent.lock()->right_child.get())) { auto old_parent = element.parent; - auto old_right_child = element.rightChild; - auto old_left_child = element.leftChild; - *(element.parent->parent) = element; - element.leftChild = element.parent; - element.parent = old_parent->parent; - *(old_parent->rightChild) = element; - old_parent->rightChild = old_left_child; + auto old_right_child = element.right_child; + auto old_left_child = element.left_child; + *(element.parent.lock()->parent.lock()) = element; + element.left_child = element.parent.lock(); + element.parent = old_parent.lock()->parent; + *(old_parent.lock()->right_child) = element; + old_parent.lock()->right_child = old_left_child; old_left_child->parent = old_parent; } } void Tree::ZigZig(Node &element) { - Zig(*element.parent); + Zig(*element.parent.lock()); Zig(element); } @@ -177,23 +177,25 @@ void Tree::Splay(Node &element) { } bool Tree::IsDirectChild(const Node &element) { - if (IsLeftChild(element) && IsLeftChild(*(element.parent))) return true; - if (IsRightChild(element) && IsRightChild(*(element.parent))) return true; + if (IsLeftChild(element) && IsLeftChild(*(element.parent.lock()))) return true; + if (IsRightChild(element) && IsRightChild(*(element.parent.lock()))) return true; return false; } bool Tree::IsLeftChild(const Node &element) { - if (element == *(element.parent->leftChild.get())) return true; + if (element.parent.lock() == nullptr) return false; + if (element == *(element.parent.lock()->left_child.get())) return true; return false; } bool Tree::IsRightChild(const Node &element) { - if (element == *(element.parent->rightChild.get())) return true; + if (element.parent.lock() == nullptr) return false; + if (element == *(element.parent.lock()->right_child.get())) return true; return false; } bool Tree::HaveGrandpa(const Node &element) { - if (element.parent == nullptr) return false; - if (element.parent->parent != nullptr) return true; + if (element.parent.lock() == nullptr) return false; + if (element.parent.lock()->parent.lock() != nullptr) return true; return false; } \ No newline at end of file diff --git a/task_05/src/tree.hpp b/task_05/src/tree.hpp index 9c73ce93..08fdadbe 100644 --- a/task_05/src/tree.hpp +++ b/task_05/src/tree.hpp @@ -4,15 +4,14 @@ class Tree { public: struct Node { - std::shared_ptr parent = nullptr; - std::shared_ptr leftChild = nullptr; - std::shared_ptr rightChild = nullptr; + std::weak_ptr parent; + std::shared_ptr left_child = nullptr; + std::shared_ptr right_child = nullptr; int value; int key; Node(int k, int v); - Node(Node& node); + Node(const Node& node); - Node(const Node& other) = default; Node& operator=(const Node& rhs) = default; Node(Node&& other) = default; diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 9a5995b6..5ddf1222 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -5,24 +5,18 @@ #include #include -HashTable::HashTable(size_t width, size_t height) - : width_(width), height_(height) { - for (int i = 0; i < width; ++i) - data_.push_back(std::vector>(height)); -} +HashTable::HashTable(size_t width) : width_(width) {} bool HashTable::Contains(const std::string& key) { int hash_w = hasher_(key) % width_; - int hash_h = hasher_(key) % height_; - if (data_[hash_w][hash_h].first == key) return true; + if (!data_[hash_w].empty() && data_[hash_w][0].first == key) return true; return false; } bool HashTable::Insert(const std::string& key, int value) { int hash_w = hasher_(key) % width_; - int hash_h = hasher_(key) % height_; if (Contains(key)) return false; - data_[hash_w][hash_h] = std::pair(key, value); + data_[hash_w].push_back(std::pair(key, value)); keys_count_ += 1; this->Rehash(); return true; @@ -30,40 +24,49 @@ bool HashTable::Insert(const std::string& key, int value) { void HashTable::InsertOrUpdate(const std::string& key, int value) { int hash_w = hasher_(key) % width_; - int hash_h = hasher_(key) % height_; if (!Contains(key)) keys_count_ += 1; - data_[hash_w][hash_h] = std::pair(key, value); + for (int i = 0; i < data_[hash_w].size(); ++i) + if (data_[hash_w][i].first == key) { + data_[hash_w][i].second = value; + return; + } + data_[hash_w].push_back(std::pair(key, value)); this->Rehash(); } void HashTable::Remove(const std::string& key) { int hash_w = hasher_(key) % width_; - int hash_h = hasher_(key) % height_; if (!Contains(key)) throw std::out_of_range("no such key"); keys_count_ -= 1; - data_[hash_w][hash_h] = std::pair(); - this->Rehash(); + for (int i = 0; i < data_[hash_w].size(); ++i) + if (data_[hash_w][i].first == key) { + data_[hash_w].erase(data_[hash_w].begin() + i); + this->Rehash(); + return; + } + } int HashTable::Find(const std::string& key) const { int hash_w = hasher_(key) % width_; - int hash_h = hasher_(key) % height_; - return data_.at(hash_w).at(hash_h).second; + for (int i = 0; i < data_[hash_w].size(); ++i) + if (data_[hash_w][i].first == key) + return data_.at(hash_w).at(i).second; } size_t HashTable::Size() const { return keys_count_; } void HashTable::Rehash() { - if (keys_count_ < width_ * height_ * 0.8 && - (keys_count_ > width_ * height_ * 0.2 || width_ <= 20 && height_ <= 10)) + if (keys_count_ < width_ * 0.8 && + (keys_count_ > width_ * 0.2 || width_ <= 20)) return; HashTable new_hash_table; - if (keys_count_ >= width_ * height_ * 0.8) - new_hash_table = HashTable(width_ * 2, height_ * 2); + if (keys_count_ >= width_ * 0.8) + new_hash_table = HashTable(width_ * 2); for (int i = 0; i < width_; ++i) - for (int j = 0; j < height_; ++j) + for (int j = 0; j < data_[i].size(); ++j) new_hash_table.Insert(data_.at(i).at(j).first, data_.at(i).at(j).second); *this = new_hash_table; diff --git a/task_06/src/hashtable.hpp b/task_06/src/hashtable.hpp index cfe9d361..1698ab38 100644 --- a/task_06/src/hashtable.hpp +++ b/task_06/src/hashtable.hpp @@ -6,7 +6,7 @@ class HashTable { public: - HashTable(size_t width = 20, size_t height = 10); + HashTable(size_t width = 20); bool Insert(const std::string& key, int value); void InsertOrUpdate(const std::string& key, int value); void Remove(const std::string& key); @@ -19,6 +19,5 @@ class HashTable { std::hash hasher_; size_t keys_count_ = 0; size_t width_ = 20; // count of vector in data - size_t height_ = 10; // count of numbers in each vector void Rehash(); }; From ee96cb082218197036818d96d5e32483124eda2c Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 07:31:41 +0000 Subject: [PATCH 24/33] without hash matrix --- task_06/src/hashtable.cpp | 43 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 5ddf1222..ea777daa 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -4,43 +4,47 @@ #include #include #include +#include -HashTable::HashTable(size_t width) : width_(width) {} +HashTable::HashTable(size_t width) : width_(width), data_(width) {} bool HashTable::Contains(const std::string& key) { - int hash_w = hasher_(key) % width_; - if (!data_[hash_w].empty() && data_[hash_w][0].first == key) return true; + size_t hash_w = hasher_(key) % width_; + if (data_.at(hash_w).empty()) return false; + for (size_t i = 0; i < data_.at(hash_w).size(); ++i) + if (data_.at(hash_w).at(i).first == key) + return true; return false; } bool HashTable::Insert(const std::string& key, int value) { - int hash_w = hasher_(key) % width_; + size_t hash_w = hasher_(key) % width_; if (Contains(key)) return false; - data_[hash_w].push_back(std::pair(key, value)); + data_.at(hash_w).push_back(std::pair(key, value)); keys_count_ += 1; this->Rehash(); return true; } void HashTable::InsertOrUpdate(const std::string& key, int value) { - int hash_w = hasher_(key) % width_; + size_t hash_w = hasher_(key) % width_; if (!Contains(key)) keys_count_ += 1; - for (int i = 0; i < data_[hash_w].size(); ++i) - if (data_[hash_w][i].first == key) { - data_[hash_w][i].second = value; + for (size_t i = 0; i < data_.at(hash_w).size(); ++i) + if (data_.at(hash_w).at(i).first == key) { + data_.at(hash_w).at(i).second = value; return; } - data_[hash_w].push_back(std::pair(key, value)); + data_.at(hash_w).push_back(std::pair(key, value)); this->Rehash(); } void HashTable::Remove(const std::string& key) { - int hash_w = hasher_(key) % width_; + size_t hash_w = hasher_(key) % width_; if (!Contains(key)) throw std::out_of_range("no such key"); keys_count_ -= 1; - for (int i = 0; i < data_[hash_w].size(); ++i) - if (data_[hash_w][i].first == key) { - data_[hash_w].erase(data_[hash_w].begin() + i); + for (size_t i = 0; i < data_.at(hash_w).size(); ++i) + if (data_.at(hash_w).at(i).first == key) { + data_.at(hash_w).erase(data_.at(hash_w).begin() + i); this->Rehash(); return; } @@ -48,12 +52,13 @@ void HashTable::Remove(const std::string& key) { } int HashTable::Find(const std::string& key) const { - int hash_w = hasher_(key) % width_; - for (int i = 0; i < data_[hash_w].size(); ++i) - if (data_[hash_w][i].first == key) + size_t hash_w = hasher_(key) % width_; + for (size_t i = 0; i < data_.at(hash_w).size(); ++i) + if (data_.at(hash_w).at(i).first == key) return data_.at(hash_w).at(i).second; } + size_t HashTable::Size() const { return keys_count_; } void HashTable::Rehash() { @@ -65,8 +70,8 @@ void HashTable::Rehash() { if (keys_count_ >= width_ * 0.8) new_hash_table = HashTable(width_ * 2); - for (int i = 0; i < width_; ++i) - for (int j = 0; j < data_[i].size(); ++j) + for (size_t i = 0; i < width_; ++i) + for (size_t j = 0; j < data_.at(i).size(); ++j) new_hash_table.Insert(data_.at(i).at(j).first, data_.at(i).at(j).second); *this = new_hash_table; From 5f3f23bf0c574972e7f1c9d450ee4b59f5662c30 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 07:58:30 +0000 Subject: [PATCH 25/33] merge --- .github/workflows/clang-tidy-review.yaml | 2 +- .github/workflows/{cmake.yaml => tests.yaml} | 0 additional_tasks/CMakeLists.txt | 16 ++++++++++++++-- additional_tasks/find_sum_numbers/CMakeLists.txt | 5 +++-- additional_tasks/temperature/CMakeLists.txt | 5 +++-- sandbox/CMakeLists.txt | 11 ++++++----- task_01/CMakeLists.txt | 5 +++-- task_02/CMakeLists.txt | 5 +++-- task_05/CMakeLists.txt | 5 +++-- task_06/CMakeLists.txt | 5 +++-- 10 files changed, 39 insertions(+), 20 deletions(-) rename .github/workflows/{cmake.yaml => tests.yaml} (100%) diff --git a/.github/workflows/clang-tidy-review.yaml b/.github/workflows/clang-tidy-review.yaml index 50c0ad37..b2bde92d 100644 --- a/.github/workflows/clang-tidy-review.yaml +++ b/.github/workflows/clang-tidy-review.yaml @@ -14,7 +14,7 @@ jobs: - uses: ZedThree/clang-tidy-review@v0.10.1 with: - clang_tidy_checks: '-*,performance-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-avoid-magic-numbers' + clang_tidy_checks: '-*,performance-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,mpi-*,misc-*,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-avoid-magic-numbers,-clang-diagnostic-error,-misc-no-recursion' split_workflow: true - uses: actions/upload-artifact@v3 diff --git a/.github/workflows/cmake.yaml b/.github/workflows/tests.yaml similarity index 100% rename from .github/workflows/cmake.yaml rename to .github/workflows/tests.yaml diff --git a/additional_tasks/CMakeLists.txt b/additional_tasks/CMakeLists.txt index f017dfdb..ad72f75c 100644 --- a/additional_tasks/CMakeLists.txt +++ b/additional_tasks/CMakeLists.txt @@ -2,5 +2,17 @@ cmake_minimum_required(VERSION 3.10) project(additional_tasks) -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/temperature) -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/find_sum_numbers) +file(GLOB_RECURSE tasks_dirs LIST_DIRECTORIES true ".") + +MESSAGE("dirs: ${task_dirs}") + +foreach(dir ${tasks_dirs}) + IF(IS_DIRECTORY ${dir}) + IF(NOT ${dir} MATCHES ".*src.*") + MESSAGE("additional task: ${dir}") + add_subdirectory(${dir}) + ENDIF() + ELSE() + CONTINUE() + ENDIF() +endforeach() diff --git a/additional_tasks/find_sum_numbers/CMakeLists.txt b/additional_tasks/find_sum_numbers/CMakeLists.txt index 7eac3977..28c5cb5a 100644 --- a/additional_tasks/find_sum_numbers/CMakeLists.txt +++ b/additional_tasks/find_sum_numbers/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME find_number_sum) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/additional_tasks/temperature/CMakeLists.txt b/additional_tasks/temperature/CMakeLists.txt index 70332489..28c5cb5a 100644 --- a/additional_tasks/temperature/CMakeLists.txt +++ b/additional_tasks/temperature/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME days_before_warmup) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/sandbox/CMakeLists.txt b/sandbox/CMakeLists.txt index 0105689d..28c5cb5a 100644 --- a/sandbox/CMakeLists.txt +++ b/sandbox/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME sandbox) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -24,11 +25,11 @@ find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) # Link runTests with what we want to test and the GTest and pthread library -add_executable(sandbox_tests ${test_source_list}) +add_executable(${PROJECT_NAME}_tests ${test_source_list}) target_link_libraries( - sandbox_tests + ${PROJECT_NAME}_tests GTest::gtest_main ) include(GoogleTest) -gtest_discover_tests(sandbox_tests) +gtest_discover_tests(${PROJECT_NAME}_tests) diff --git a/task_01/CMakeLists.txt b/task_01/CMakeLists.txt index 0d13e1cf..28c5cb5a 100644 --- a/task_01/CMakeLists.txt +++ b/task_01/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME task_01) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/task_02/CMakeLists.txt b/task_02/CMakeLists.txt index 08b6e047..28c5cb5a 100644 --- a/task_02/CMakeLists.txt +++ b/task_02/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME task_02) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/task_05/CMakeLists.txt b/task_05/CMakeLists.txt index b217db8f..28c5cb5a 100644 --- a/task_05/CMakeLists.txt +++ b/task_05/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME task_05) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/task_06/CMakeLists.txt b/task_06/CMakeLists.txt index f3f54c2a..28c5cb5a 100644 --- a/task_06/CMakeLists.txt +++ b/task_06/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.10) -set(PROJECT_NAME task_06) -project(${PROJECT_NAME}) +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME}) +project(${PROJECT_NAME} C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) From b42acafb208a6068f27779f6ba8897d749e8a6a5 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:13:55 +0000 Subject: [PATCH 26/33] format --- task_03/src/sort.cpp | 2 +- task_05/src/tree.cpp | 2 +- task_06/src/hashtable.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 543a651e..d40071ed 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -10,7 +10,7 @@ std::vector Sort(const std::vector& input) { std::vector bigger_part(input.begin() + middle, input.end()); smaller_part = Sort(smaller_part); - bigger_part = Sort(bigger_part); + bigger_part = Sort(bigger_part); for (size_t i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { if (smaller_part[i] <= bigger_part[j]) { output.push_back(smaller_part[i]); diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index 7dd6249f..798b0c93 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -66,7 +66,7 @@ Tree::Node &Tree::InsertOrUpdateWithoutSplay(int key, int value) { int Tree::Find(int key) { auto result = FindWithoutSplay(key); Splay(result.first); - return result.second; + return result.second; } std::pair Tree::FindWithoutSplay(int key) { diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index ea777daa..1dd26736 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -7,7 +7,7 @@ #include HashTable::HashTable(size_t width) : width_(width), data_(width) {} - + bool HashTable::Contains(const std::string& key) { size_t hash_w = hasher_(key) % width_; if (data_.at(hash_w).empty()) return false; From c85a4c0232169f5a66c578f17dad4067518d1f23 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:28:31 +0000 Subject: [PATCH 27/33] format --- task_03/src/sort.cpp | 2 +- task_06/src/hashtable.cpp | 12 ++++-------- task_06/src/hashtable.hpp | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 543a651e..c1fc7853 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -27,7 +27,7 @@ std::vector Sort(const std::vector& input) { smaller_part.end()); break; } - } + } } return output; } \ No newline at end of file diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index ea777daa..751717c5 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -12,8 +12,7 @@ bool HashTable::Contains(const std::string& key) { size_t hash_w = hasher_(key) % width_; if (data_.at(hash_w).empty()) return false; for (size_t i = 0; i < data_.at(hash_w).size(); ++i) - if (data_.at(hash_w).at(i).first == key) - return true; + if (data_.at(hash_w).at(i).first == key) return true; return false; } @@ -48,27 +47,24 @@ void HashTable::Remove(const std::string& key) { this->Rehash(); return; } - } int HashTable::Find(const std::string& key) const { size_t hash_w = hasher_(key) % width_; for (size_t i = 0; i < data_.at(hash_w).size(); ++i) - if (data_.at(hash_w).at(i).first == key) + if (data_.at(hash_w).at(i).first == key) return data_.at(hash_w).at(i).second; } - size_t HashTable::Size() const { return keys_count_; } void HashTable::Rehash() { if (keys_count_ < width_ * 0.8 && - (keys_count_ > width_ * 0.2 || width_ <= 20)) + (keys_count_ > width_ * 0.2 || width_ <= 20)) return; HashTable new_hash_table; - if (keys_count_ >= width_ * 0.8) - new_hash_table = HashTable(width_ * 2); + if (keys_count_ >= width_ * 0.8) new_hash_table = HashTable(width_ * 2); for (size_t i = 0; i < width_; ++i) for (size_t j = 0; j < data_.at(i).size(); ++j) diff --git a/task_06/src/hashtable.hpp b/task_06/src/hashtable.hpp index 1698ab38..53e31ffd 100644 --- a/task_06/src/hashtable.hpp +++ b/task_06/src/hashtable.hpp @@ -13,7 +13,7 @@ class HashTable { int Find(const std::string& key) const; size_t Size() const; bool Contains(const std::string& key); - + private: std::vector>> data_; std::hash hasher_; From 740152a9ccd864b25b0229c4cca3fbc3a3f06c2c Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:38:31 +0000 Subject: [PATCH 28/33] format --- task_03/src/sort.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index c1fc7853..d43a50c2 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -11,7 +11,8 @@ std::vector Sort(const std::vector& input) { smaller_part = Sort(smaller_part); bigger_part = Sort(bigger_part); - for (size_t i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { + for (size_t i = 0, j = 0; + i < smaller_part.size() || j < bigger_part.size();) { if (smaller_part[i] <= bigger_part[j]) { output.push_back(smaller_part[i]); ++i; @@ -27,7 +28,7 @@ std::vector Sort(const std::vector& input) { smaller_part.end()); break; } - } + } } return output; } \ No newline at end of file From a50184966ce3f8d47f492d31f28a015bf66a546b Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:44:41 +0000 Subject: [PATCH 29/33] format --- task_05/src/tree.cpp | 1 + task_06/src/hashtable.cpp | 1 + task_06/src/hashtable.hpp | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index 7dd6249f..eb1f2dae 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -79,6 +79,7 @@ std::pair Tree::FindWithoutSplay(int key) { void Tree::Delete(int key) { auto deleting_node = FindWithoutSplay(key).first; + if (deleting_node.left_child == nullptr && deleting_node.right_child == nullptr) { if (*(root.get()) != deleting_node) { diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 751717c5..c0b28a81 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -11,6 +11,7 @@ HashTable::HashTable(size_t width) : width_(width), data_(width) {} bool HashTable::Contains(const std::string& key) { size_t hash_w = hasher_(key) % width_; if (data_.at(hash_w).empty()) return false; + for (size_t i = 0; i < data_.at(hash_w).size(); ++i) if (data_.at(hash_w).at(i).first == key) return true; return false; diff --git a/task_06/src/hashtable.hpp b/task_06/src/hashtable.hpp index 53e31ffd..4b99d7a7 100644 --- a/task_06/src/hashtable.hpp +++ b/task_06/src/hashtable.hpp @@ -13,11 +13,11 @@ class HashTable { int Find(const std::string& key) const; size_t Size() const; bool Contains(const std::string& key); - + private: std::vector>> data_; std::hash hasher_; size_t keys_count_ = 0; - size_t width_ = 20; // count of vector in data + size_t width_ = 20; // count of vector in data void Rehash(); }; From cf91b0ea2de9f0f984b0d96ed9f6e4d53e9f23d8 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:48:17 +0000 Subject: [PATCH 30/33] format --- task_05/src/tree.cpp | 1 + task_06/src/hashtable.cpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index f55cd410..25d8a599 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -146,6 +146,7 @@ void Tree::Zig(Node &element) { old_parent.lock()->left_child = old_right_child; old_right_child->parent = old_parent; } + if (element == *(element.parent.lock()->right_child.get())) { auto old_parent = element.parent; auto old_right_child = element.right_child; diff --git a/task_06/src/hashtable.cpp b/task_06/src/hashtable.cpp index 6da187c6..602d19b1 100644 --- a/task_06/src/hashtable.cpp +++ b/task_06/src/hashtable.cpp @@ -7,13 +7,14 @@ #include HashTable::HashTable(size_t width) : width_(width), data_(width) {} - + bool HashTable::Contains(const std::string& key) { size_t hash_w = hasher_(key) % width_; if (data_.at(hash_w).empty()) return false; for (size_t i = 0; i < data_.at(hash_w).size(); ++i) if (data_.at(hash_w).at(i).first == key) return true; + return false; } From 0d25690d76f9d8b27692305e51c6af4456e0666d Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:51:53 +0000 Subject: [PATCH 31/33] format --- task_05/src/tree.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index 25d8a599..ba14fbd2 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -146,7 +146,7 @@ void Tree::Zig(Node &element) { old_parent.lock()->left_child = old_right_child; old_right_child->parent = old_parent; } - + if (element == *(element.parent.lock()->right_child.get())) { auto old_parent = element.parent; auto old_right_child = element.right_child; @@ -179,8 +179,10 @@ void Tree::Splay(Node &element) { } bool Tree::IsDirectChild(const Node &element) { - if (IsLeftChild(element) && IsLeftChild(*(element.parent.lock()))) return true; - if (IsRightChild(element) && IsRightChild(*(element.parent.lock()))) return true; + if (IsLeftChild(element) && IsLeftChild(*(element.parent.lock()))) + return true; + if (IsRightChild(element) && IsRightChild(*(element.parent.lock()))) + return true; return false; } From 98645aca21532679e2141562971c011dd3fc1b2e Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 08:58:12 +0000 Subject: [PATCH 32/33] correction --- task_03/src/sort.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/task_03/src/sort.cpp b/task_03/src/sort.cpp index 5136d7ba..20a221ba 100644 --- a/task_03/src/sort.cpp +++ b/task_03/src/sort.cpp @@ -10,6 +10,7 @@ std::vector Sort(const std::vector& input) { std::vector bigger_part(input.begin() + middle, input.end()); bigger_part = Sort(bigger_part); + smaller_part = Sort(smaller_part); for (size_t i = 0, j = 0; i < smaller_part.size() || j < bigger_part.size();) { if (smaller_part[i] <= bigger_part[j]) { From 7a2e5ec9c3e4a3844d0036adbfc9515cec3ace25 Mon Sep 17 00:00:00 2001 From: VariPor <116594957+VariPor@users.noreply.github.com> Date: Wed, 21 Jun 2023 09:02:57 +0000 Subject: [PATCH 33/33] format --- task_05/src/tree.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/task_05/src/tree.cpp b/task_05/src/tree.cpp index ba14fbd2..9f2aa7a8 100644 --- a/task_05/src/tree.cpp +++ b/task_05/src/tree.cpp @@ -66,7 +66,7 @@ Tree::Node &Tree::InsertOrUpdateWithoutSplay(int key, int value) { int Tree::Find(int key) { auto result = FindWithoutSplay(key); Splay(result.first); - return result.second; + return result.second; } std::pair Tree::FindWithoutSplay(int key) { @@ -146,7 +146,6 @@ void Tree::Zig(Node &element) { old_parent.lock()->left_child = old_right_child; old_right_child->parent = old_parent; } - if (element == *(element.parent.lock()->right_child.get())) { auto old_parent = element.parent; auto old_right_child = element.right_child;