Skip to content

Commit 0ef8e2b

Browse files
committed
task_04 fixes
1 parent bf06d08 commit 0ef8e2b

File tree

2 files changed

+69
-69
lines changed

2 files changed

+69
-69
lines changed

task_04/src/heap.hpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@ concept comparing = requires(CustomType a, CustomType b, Function f) {
3434
template <constructable CustomType,
3535
comparing<CustomType> Function =
3636
std::function<bool(const CustomType&, const CustomType&)>>
37-
class heap {
37+
class Heap {
3838
public:
3939
// default constructor works only if your custom type is comparable!
40-
heap();
40+
Heap();
4141
// this constructor works only if your custom type is comparable with a
4242
// function
43-
heap(std::function<bool(const CustomType&, const CustomType&)>
43+
Heap(std::function<bool(const CustomType&, const CustomType&)>
4444
comparing_function_);
45-
explicit heap(std::initializer_list<CustomType> initializer_list);
46-
explicit heap(std::function<bool(const CustomType&, const CustomType&)>
45+
explicit Heap(std::initializer_list<CustomType> initializer_list);
46+
explicit Heap(std::function<bool(const CustomType&, const CustomType&)>
4747
comparing_function_,
4848
std::initializer_list<CustomType> initializer_list);
4949
// push an element into heap, O(logn) time complexity
50-
void push(CustomType element);
50+
void Push(CustomType element);
5151
// pop a minimal element from heap, O(logn) time complexity
52-
CustomType pop_bottom();
52+
CustomType PopBottom();
5353
// retrieve a minimal element from heap, O(1) time complexity
54-
CustomType bottom();
54+
CustomType Bottom();
5555
// return size of heap
56-
size_t size() { return heap_size; }
56+
size_t Size() { return heap_size; }
5757
// check if heap is empty
58-
bool empty() { return heap_size == 0; }
58+
bool Empty() { return heap_size == 0; }
5959

6060
private:
6161
// bring minimal element back to place if element with given index is
@@ -71,15 +71,15 @@ class heap {
7171

7272
// time complexity - O(1)
7373
template <constructable CustomType, comparing<CustomType> Function>
74-
heap<CustomType, Function>::heap() : data{}, heap_size{0} {
74+
Heap<CustomType, Function>::Heap() : data{}, heap_size{0} {
7575
comparing_function = [](const CustomType& a, const CustomType& b) {
7676
return a < b;
7777
};
7878
}
7979

8080
// time complexity - O(1)
8181
template <constructable CustomType, comparing<CustomType> Function>
82-
heap<CustomType, Function>::heap(
82+
Heap<CustomType, Function>::Heap(
8383
std::function<bool(const CustomType&, const CustomType&)>
8484
comparing_function_)
8585
: data{}, heap_size{0} {
@@ -88,29 +88,29 @@ heap<CustomType, Function>::heap(
8888

8989
// time complexity - O(nlogn)
9090
template <constructable CustomType, comparing<CustomType> Function>
91-
heap<CustomType, Function>::heap(
91+
Heap<CustomType, Function>::Heap(
9292
std::initializer_list<CustomType> initializer_list)
93-
: heap() {
93+
: Heap() {
9494
comparing_function = [](const CustomType& a, const CustomType& b) {
9595
return a < b;
9696
};
9797

98-
for (const CustomType& value : initializer_list) push(value);
98+
for (const CustomType& value : initializer_list) Push(value);
9999
}
100100

101101
// time complexity - O(nlogn)
102102
template <constructable CustomType, comparing<CustomType> Function>
103-
heap<CustomType, Function>::heap(
103+
Heap<CustomType, Function>::Heap(
104104
std::function<bool(const CustomType&, const CustomType&)>
105105
comparing_function_,
106106
std::initializer_list<CustomType> initializer_list)
107107
: comparing_function{comparing_function_} {
108-
for (const CustomType& value : initializer_list) push(value);
108+
for (const CustomType& value : initializer_list) Push(value);
109109
}
110110

111111
// time complexity - O(logn)
112112
template <constructable CustomType, comparing<CustomType> Function>
113-
void heap<CustomType, Function>::sift_down(size_t index) {
113+
void Heap<CustomType, Function>::sift_down(size_t index) {
114114
size_t& index_1 = index; // for code to be more readable
115115

116116
while (2 * index_1 + 1 < heap_size) {
@@ -132,7 +132,7 @@ void heap<CustomType, Function>::sift_down(size_t index) {
132132

133133
// time complexity - O(logn)
134134
template <constructable CustomType, comparing<CustomType> Function>
135-
void heap<CustomType, Function>::sift_up(size_t index) {
135+
void Heap<CustomType, Function>::sift_up(size_t index) {
136136
while (comparing_function(data[index], data[(index - 1) / 2])) {
137137
std::swap(data[index], data[(index - 1) / 2]);
138138
index = (index - 1) / 2;
@@ -141,15 +141,15 @@ void heap<CustomType, Function>::sift_up(size_t index) {
141141

142142
// time complexity - O(1)
143143
template <constructable CustomType, comparing<CustomType> Function>
144-
CustomType heap<CustomType, Function>::bottom() {
145-
if (empty()) throw std::runtime_error("heap is empty");
144+
CustomType Heap<CustomType, Function>::Bottom() {
145+
if (Empty()) throw std::runtime_error("heap is empty");
146146
return data[0];
147147
}
148148

149149
// time complexity - O(logn)
150150
template <constructable CustomType, comparing<CustomType> Function>
151-
CustomType heap<CustomType, Function>::pop_bottom() {
152-
CustomType bottom_elem = bottom();
151+
CustomType Heap<CustomType, Function>::PopBottom() {
152+
CustomType bottom_elem = Bottom();
153153
std::swap(data[0], data.back());
154154
data.pop_back();
155155
heap_size--;
@@ -159,7 +159,7 @@ CustomType heap<CustomType, Function>::pop_bottom() {
159159

160160
// time complexity - O(logn)
161161
template <constructable CustomType, comparing<CustomType> Function>
162-
void heap<CustomType, Function>::push(CustomType element) {
162+
void Heap<CustomType, Function>::Push(CustomType element) {
163163
data.push_back(element);
164164
heap_size++;
165165
if (heap_size >= 2) sift_up(heap_size - 1);

task_04/src/test.cpp

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,60 @@ using std::string;
99

1010
#include "heap.hpp"
1111

12-
TEST(heap, empty) {
13-
heap<int> hp1;
14-
ASSERT_EQ(hp1.empty(), true);
15-
ASSERT_EQ(hp1.size(), 0);
16-
hp1.push(1);
17-
ASSERT_EQ(hp1.empty(), false);
18-
ASSERT_EQ(hp1.size(), 1);
19-
ASSERT_EQ(hp1.pop_bottom(), 1);
20-
EXPECT_THROW(hp1.bottom(), std::runtime_error);
21-
EXPECT_THROW(hp1.pop_bottom(), std::runtime_error);
12+
TEST(Heap, Empty) {
13+
Heap<int> hp1;
14+
ASSERT_EQ(hp1.Empty(), true);
15+
ASSERT_EQ(hp1.Size(), 0);
16+
hp1.Push(1);
17+
ASSERT_EQ(hp1.Empty(), false);
18+
ASSERT_EQ(hp1.Size(), 1);
19+
ASSERT_EQ(hp1.PopBottom(), 1);
20+
EXPECT_THROW(hp1.Bottom(), std::runtime_error);
21+
EXPECT_THROW(hp1.PopBottom(), std::runtime_error);
2222
}
23-
TEST(heap, comparable_1) {
24-
heap<std::array<int, 5>> hp2;
25-
hp2.push({1, 1, 1, 1, 1});
23+
TEST(Heap, Comparable1) {
24+
Heap<std::array<int, 5>> hp2;
25+
hp2.Push({1, 1, 1, 1, 1});
2626
std::array<int, 5> test_array_1 = {1, 1, 1, 1, 1};
2727
std::array<int, 5> test_array_2 = {1, 1, 1, 1, 0};
28-
ASSERT_EQ(hp2.bottom(), test_array_1);
29-
ASSERT_EQ(hp2.pop_bottom() == test_array_2, false);
28+
ASSERT_EQ(hp2.Bottom(), test_array_1);
29+
ASSERT_EQ(hp2.PopBottom() == test_array_2, false);
3030
}
3131

32-
TEST(heap, without_duplicates) {
33-
heap<int> hp3{};
34-
for (int value : {3, 1, 2, 4, 5}) hp3.push(value);
35-
ASSERT_EQ(hp3.pop_bottom(), 1);
36-
ASSERT_EQ(hp3.pop_bottom(), 2);
37-
ASSERT_EQ(hp3.pop_bottom(), 3);
38-
ASSERT_EQ(hp3.pop_bottom(), 4);
39-
ASSERT_EQ(hp3.pop_bottom(), 5);
32+
TEST(Heap, WithoutDuplicates) {
33+
Heap<int> hp3{};
34+
for (int value : {3, 1, 2, 4, 5}) hp3.Push(value);
35+
ASSERT_EQ(hp3.PopBottom(), 1);
36+
ASSERT_EQ(hp3.PopBottom(), 2);
37+
ASSERT_EQ(hp3.PopBottom(), 3);
38+
ASSERT_EQ(hp3.PopBottom(), 4);
39+
ASSERT_EQ(hp3.PopBottom(), 5);
4040
}
4141

42-
TEST(heap, with_duplicates) {
43-
heap<int> hp4{};
44-
for (int value : {6, 7, 7, 3, 3, 3, 1, 2}) hp4.push(value);
45-
ASSERT_EQ(hp4.pop_bottom(), 1);
46-
ASSERT_EQ(hp4.pop_bottom(), 2);
47-
ASSERT_EQ(hp4.pop_bottom(), 3);
48-
ASSERT_EQ(hp4.pop_bottom(), 3);
49-
ASSERT_EQ(hp4.pop_bottom(), 3);
50-
ASSERT_EQ(hp4.pop_bottom(), 6);
51-
ASSERT_EQ(hp4.pop_bottom(), 7);
52-
ASSERT_EQ(hp4.pop_bottom(), 7);
42+
TEST(Heap, WithDuplicates) {
43+
Heap<int> hp4{};
44+
for (int value : {6, 7, 7, 3, 3, 3, 1, 2}) hp4.Push(value);
45+
ASSERT_EQ(hp4.PopBottom(), 1);
46+
ASSERT_EQ(hp4.PopBottom(), 2);
47+
ASSERT_EQ(hp4.PopBottom(), 3);
48+
ASSERT_EQ(hp4.PopBottom(), 3);
49+
ASSERT_EQ(hp4.PopBottom(), 3);
50+
ASSERT_EQ(hp4.PopBottom(), 6);
51+
ASSERT_EQ(hp4.PopBottom(), 7);
52+
ASSERT_EQ(hp4.PopBottom(), 7);
5353
}
5454

55-
TEST(heap, init_list_constructor) {
56-
heap<int> hp5{1, 2, 3};
57-
ASSERT_EQ(hp5.pop_bottom(), 1);
58-
ASSERT_EQ(hp5.pop_bottom(), 2);
59-
ASSERT_EQ(hp5.pop_bottom(), 3);
55+
TEST(Heap, InitListConstructor) {
56+
Heap<int> hp5{1, 2, 3};
57+
ASSERT_EQ(hp5.PopBottom(), 1);
58+
ASSERT_EQ(hp5.PopBottom(), 2);
59+
ASSERT_EQ(hp5.PopBottom(), 3);
6060
}
6161

62-
TEST(string_heap, simple_test) {
63-
heap<string> hp6{"azz", "bzz", "zaa", "zab"};
64-
ASSERT_EQ(hp6.pop_bottom(), "azz");
65-
ASSERT_EQ(hp6.pop_bottom(), "bzz");
66-
ASSERT_EQ(hp6.pop_bottom(), "zaa");
67-
ASSERT_EQ(hp6.pop_bottom(), "zab");
62+
TEST(StringHeap, SimpleTest) {
63+
Heap<string> hp6{"azz", "bzz", "zaa", "zab"};
64+
ASSERT_EQ(hp6.PopBottom(), "azz");
65+
ASSERT_EQ(hp6.PopBottom(), "bzz");
66+
ASSERT_EQ(hp6.PopBottom(), "zaa");
67+
ASSERT_EQ(hp6.PopBottom(), "zab");
6868
}

0 commit comments

Comments
 (0)