| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "rozenberg_a_quicksort_simple_merge/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <stack> | ||
| 4 | #include <utility> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "rozenberg_a_quicksort_simple_merge/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace rozenberg_a_quicksort_simple_merge { | ||
| 10 | |||
| 11 | 72 | RozenbergAQuicksortSimpleMergeSEQ::RozenbergAQuicksortSimpleMergeSEQ(const InType &in) { | |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | |||
| 14 | InType empty; | ||
| 15 | GetInput().swap(empty); | ||
| 16 | |||
| 17 |
2/2✓ Branch 0 taken 5416 times.
✓ Branch 1 taken 72 times.
|
5488 | for (const auto &elem : in) { |
| 18 | GetInput().push_back(elem); | ||
| 19 | } | ||
| 20 | |||
| 21 | GetOutput().clear(); | ||
| 22 | 72 | } | |
| 23 | |||
| 24 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | bool RozenbergAQuicksortSimpleMergeSEQ::ValidationImpl() { |
| 25 |
2/4✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 72 times.
|
72 | return (!(GetInput().empty())) && (GetOutput().empty()); |
| 26 | } | ||
| 27 | |||
| 28 | 72 | bool RozenbergAQuicksortSimpleMergeSEQ::PreProcessingImpl() { | |
| 29 | 72 | GetOutput().resize(GetInput().size()); | |
| 30 | 72 | return GetOutput().size() == GetInput().size(); | |
| 31 | } | ||
| 32 | |||
| 33 | 4520 | std::pair<int, int> RozenbergAQuicksortSimpleMergeSEQ::Partition(InType &data, int left, int right) { | |
| 34 | 4520 | const int pivot = data[left + ((right - left) / 2)]; | |
| 35 | int i = left; | ||
| 36 | int j = right; | ||
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 14088 times.
✓ Branch 1 taken 4520 times.
|
23128 | while (i <= j) { |
| 39 |
2/2✓ Branch 0 taken 13840 times.
✓ Branch 1 taken 14088 times.
|
27928 | while (data[i] < pivot) { |
| 40 | 13840 | i++; | |
| 41 | } | ||
| 42 |
2/2✓ Branch 0 taken 12928 times.
✓ Branch 1 taken 14088 times.
|
27016 | while (data[j] > pivot) { |
| 43 | 12928 | j--; | |
| 44 | } | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 1232 times.
✓ Branch 1 taken 12856 times.
|
14088 | if (i <= j) { |
| 47 | std::swap(data[i], data[j]); | ||
| 48 | 12856 | i++; | |
| 49 | 12856 | j--; | |
| 50 | } | ||
| 51 | } | ||
| 52 | 4520 | return {i, j}; | |
| 53 | } | ||
| 54 | |||
| 55 | 4520 | void RozenbergAQuicksortSimpleMergeSEQ::PushSubarrays(std::stack<std::pair<int, int>> &stack, int left, int right, | |
| 56 | int i, int j) { | ||
| 57 |
2/2✓ Branch 0 taken 1216 times.
✓ Branch 1 taken 3304 times.
|
4520 | if (j - left > right - i) { |
| 58 |
1/2✓ Branch 0 taken 1216 times.
✗ Branch 1 not taken.
|
1216 | if (left < j) { |
| 59 | stack.emplace(left, j); | ||
| 60 | } | ||
| 61 |
2/2✓ Branch 0 taken 648 times.
✓ Branch 1 taken 568 times.
|
1216 | if (i < right) { |
| 62 | stack.emplace(i, right); | ||
| 63 | } | ||
| 64 | } else { | ||
| 65 |
2/2✓ Branch 0 taken 1584 times.
✓ Branch 1 taken 1720 times.
|
3304 | if (i < right) { |
| 66 | stack.emplace(i, right); | ||
| 67 | } | ||
| 68 |
2/2✓ Branch 0 taken 1008 times.
✓ Branch 1 taken 2296 times.
|
3304 | if (left < j) { |
| 69 | stack.emplace(left, j); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | 4520 | } | |
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 64 times.
|
72 | void RozenbergAQuicksortSimpleMergeSEQ::Quicksort(InType &data) { |
| 75 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 64 times.
|
72 | if (data.size() < 2) { |
| 76 | 8 | return; | |
| 77 | } | ||
| 78 | |||
| 79 | std::stack<std::pair<int, int>> stack; | ||
| 80 | |||
| 81 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | stack.emplace(0, data.size() - 1); |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 4520 times.
✓ Branch 1 taken 64 times.
|
4584 | while (!stack.empty()) { |
| 84 | const auto [left, right] = stack.top(); | ||
| 85 | stack.pop(); | ||
| 86 | |||
| 87 |
1/2✓ Branch 0 taken 4520 times.
✗ Branch 1 not taken.
|
4520 | if (left < right) { |
| 88 | 4520 | const auto [i, j] = Partition(data, left, right); | |
| 89 |
1/2✓ Branch 1 taken 4520 times.
✗ Branch 2 not taken.
|
4520 | PushSubarrays(stack, left, right, i, j); |
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | 72 | bool RozenbergAQuicksortSimpleMergeSEQ::RunImpl() { | |
| 95 | 72 | InType data = GetInput(); | |
| 96 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | Quicksort(data); |
| 97 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | GetOutput() = data; |
| 98 | 72 | return true; | |
| 99 | } | ||
| 100 | |||
| 101 | 72 | bool RozenbergAQuicksortSimpleMergeSEQ::PostProcessingImpl() { | |
| 102 | 72 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | } // namespace rozenberg_a_quicksort_simple_merge | ||
| 106 |