|
8 | 8 | * for all operations including push, pop, top, and retrieving the minimum |
9 | 9 | * value. |
10 | 10 | * |
11 | | - * The implementation uses a modified stack mechanism where the pushed value is |
12 | | - * encoded if it is less than the current minimum. This ensures that when an |
13 | | - * element is popped, the previous minimum can be restored in constant time. |
14 | | - * |
15 | 11 | * ASCII Illustration: |
16 | 12 | * |
17 | 13 | * Stack: [ ... | encoded_value ] |
|
37 | 33 | #include <stack> |
38 | 34 | #include <string> |
39 | 35 |
|
40 | | -namespace { |
41 | | -struct TestRunner { |
42 | | - int total = 0; |
43 | | - int failed = 0; |
44 | | - |
45 | | - template <typename T> |
46 | | - void expectEqual(const std::optional<T> &got, |
47 | | - const std::optional<T> &expected, const std::string &label) { |
48 | | - ++total; |
49 | | - if (got == expected) { |
50 | | - std::cout << "[PASS] " << label << "\n"; |
51 | | - return; |
52 | | - } |
53 | | - ++failed; |
54 | | - std::cout << "[FAIL] " << label << " expected=" << toString(expected) |
55 | | - << " got=" << toString(got) << "\n"; |
56 | | - } |
57 | | - |
58 | | - void summary() const { |
59 | | - std::cout << "Tests: " << total - failed << " passed, " << failed |
60 | | - << " failed, " << total << " total\n"; |
61 | | - } |
62 | | - |
63 | | -private: |
64 | | - template <typename T> |
65 | | - static std::string toString(const std::optional<T> &value) { |
66 | | - if (!value.has_value()) |
67 | | - return "nullopt"; |
68 | | - return std::to_string(*value); |
69 | | - } |
70 | | -}; |
71 | | -} // namespace |
72 | | - |
73 | 36 | // Template class for a stack that tracks the minimum element in O(1) time. |
74 | 37 | template <typename T> class StackWithMin { |
75 | 38 | public: |
@@ -118,6 +81,39 @@ template <typename T> class StackWithMin { |
118 | 81 | std::optional<T> min_value_; |
119 | 82 | }; |
120 | 83 |
|
| 84 | +namespace { |
| 85 | +struct TestRunner { |
| 86 | + int total = 0; |
| 87 | + int failed = 0; |
| 88 | + |
| 89 | + template <typename T> |
| 90 | + void expectEqual(const std::optional<T> &got, |
| 91 | + const std::optional<T> &expected, const std::string &label) { |
| 92 | + ++total; |
| 93 | + if (got == expected) { |
| 94 | + std::cout << "[PASS] " << label << "\n"; |
| 95 | + return; |
| 96 | + } |
| 97 | + ++failed; |
| 98 | + std::cout << "[FAIL] " << label << " expected=" << toString(expected) |
| 99 | + << " got=" << toString(got) << "\n"; |
| 100 | + } |
| 101 | + |
| 102 | + void summary() const { |
| 103 | + std::cout << "Tests: " << total - failed << " passed, " << failed |
| 104 | + << " failed, " << total << " total\n"; |
| 105 | + } |
| 106 | + |
| 107 | +private: |
| 108 | + template <typename T> |
| 109 | + static std::string toString(const std::optional<T> &value) { |
| 110 | + if (!value.has_value()) |
| 111 | + return "nullopt"; |
| 112 | + return std::to_string(*value); |
| 113 | + } |
| 114 | +}; |
| 115 | +} // namespace |
| 116 | + |
121 | 117 | // Test cases to ensure correctness of StackWithMin operations. |
122 | 118 | void test() { |
123 | 119 | TestRunner runner; |
|
0 commit comments