Skip to content

Commit 2f09bdf

Browse files
committed
Add debug print messages to lecture7 code
1 parent d52afdf commit 2f09bdf

File tree

2 files changed

+86
-47
lines changed

2 files changed

+86
-47
lines changed

alexander_stepaniv/lectures/lecture7/code/binary_counter.h

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,83 @@ class binary_counter
1212

1313
public:
1414
// Constructor. Input arguments: op and zero.
15-
binary_counter(const Op& op, const T& zero) :
16-
op(op), zero(zero) {
17-
counter.reserve(24);
15+
binary_counter(const Op &op, const T &zero) : op(op), zero(zero)
16+
{
17+
counter.reserve(24);
1818
}
1919

20+
void print()
21+
{
22+
auto first = counter.begin();
23+
auto last = counter.end();
24+
25+
while (first != last)
26+
{
27+
auto it = *first;
28+
std::cout << *it << ",";
29+
++first;
30+
}
31+
std::cout << std::endl;
32+
}
2033
// add
21-
void add(T x) {
34+
void add(T x)
35+
{
2236
x = add_to_counter(counter.begin(), counter.end(), op, zero, x);
23-
if (x != zero) counter.push_back(x);
37+
if (x != zero)
38+
counter.push_back(x);
2439
}
2540

2641
// reduce
2742
// returns: value of the counter
28-
T reduce() {
43+
T reduce()
44+
{
2945
return reduce_counter(counter.begin(), counter.end(), op, zero);
3046
}
3147
};
3248

3349
template <typename T, typename I, typename Op>
3450
// requires Op is BinaryOperation(T)
35-
// and Op is associative
51+
// and Op is associative
3652
// and I is ForwardIterator and ValueType(I) == T
37-
T add_to_counter(I first, I last, Op op, const T& zero, T carry) {
38-
// precondition: carry != zero
39-
while (first != last) {
40-
if (*first == zero) {
41-
*first = carry;
42-
return zero;
43-
}
44-
carry = op(*first, carry);
45-
*first = zero;
46-
++first;
53+
T add_to_counter(I first, I last, Op op, const T &zero, T carry)
54+
{
55+
// precondition: carry != zero
56+
while (first != last)
57+
{
58+
if (*first == zero)
59+
{
60+
*first = carry;
61+
std::cout << "add_to_counter returning carry: zero" << std::endl;
62+
return zero;
4763
}
48-
return carry;
64+
carry = op(*first, carry);
65+
*first = zero;
66+
++first;
67+
}
68+
std::cout << "add_to_counter returning carry: " << *carry << std::endl;
69+
return carry;
4970
}
5071

5172
template <typename T, typename I, typename Op>
5273
// requires Op is BinaryOperation(T)
53-
// and Op is associative
74+
// and Op is associative
5475
// and I is ForwardIterator and ValueType(I) == T
55-
T reduce_counter(I first, I last, Op op, const T& zero) {
56-
while (first != last && *first == zero) {
57-
++first;
58-
}
59-
if (first == last) return zero;
76+
T reduce_counter(I first, I last, Op op, const T &zero)
77+
{
78+
while (first != last && *first == zero)
79+
{
80+
++first;
81+
}
82+
if (first == last)
83+
return zero;
6084

61-
T result = *first;
62-
while (++first != last) {
63-
if (*first != zero) {
64-
result = op(*first, result);
65-
}
85+
T result = *first;
86+
while (++first != last)
87+
{
88+
if (*first != zero)
89+
{
90+
result = op(*first, result);
6691
}
67-
return result;
68-
}
69-
92+
}
93+
return result;
94+
}

alexander_stepaniv/lectures/lecture7/code/min.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,50 @@
77
// - compares two iterators and returns the one
88
// pointing to the smallest element
99
template <typename Compare>
10-
class min_op
10+
class min_op
1111
{
1212
private:
1313
Compare cmp;
14+
1415
public:
15-
min_op(const Compare& cmp) : cmp(cmp) {}
16+
min_op(const Compare &cmp) : cmp(cmp) {}
1617
template <typename I>
17-
I operator()(const I& x, const I& y) {
18+
I operator()(const I &x, const I &y)
19+
{
20+
// std::cout << "comparing: " << *x << "," << *y << std::endl;
1821
return cmp(*y, *x) ? y : x;
1922
}
2023
};
2124

2225
template <typename I, typename Compare>
2326
// requires I is a ForwardIterator
2427
// and Compare is a StrictWeakOrdering on ValueType(I)
25-
I min_element_binary(I first, I last, Compare cmp) {
28+
I min_element_binary(I first, I last, Compare cmp)
29+
{
2630
binary_counter<I, min_op<Compare> > min_counter(min_op<Compare>(cmp), last);
27-
while (first != last) min_counter.add(first++);
31+
while (first != last)
32+
{
33+
std::cout << "adding " << *first << std::endl;
34+
min_counter.add(first++);
35+
min_counter.print();
36+
}
2837
return min_counter.reduce();
2938
}
3039

31-
int main() {
40+
int main()
41+
{
3242

33-
// plugin whatever numbers you want to test with
34-
int data[] = {9, 13, 7, 124, 32, 17, 8, 32, 237, 417, 41, 42, 13, 14, 15};
35-
int* end = data + sizeof(data)/sizeof(int);
36-
int* min = min_element_binary(data, end, std::less<int>());
37-
if (min == end) {
38-
std::cout << "No elements" << std::endl;
39-
} else {
40-
std::cout << "Min is " << *min << std::endl;
41-
}
43+
// plugin whatever numbers you want to test with
44+
// int data[] = {9, 13, 7, 124, 32, 17, 8, 32, 237, 417, 41, 42, 13, 14, 15};
45+
int data[] = {11, 10, 19, 13, 18, 4, 15};
46+
int *end = data + sizeof(data) / sizeof(int);
47+
int *min = min_element_binary(data, end, std::less<int>());
48+
if (min == end)
49+
{
50+
std::cout << "No elements" << std::endl;
51+
}
52+
else
53+
{
54+
std::cout << "Min is " << *min << std::endl;
55+
}
4256
}

0 commit comments

Comments
 (0)