diff --git a/alexander_stepaniv/lectures/lecture7/code/binary_counter.h b/alexander_stepaniv/lectures/lecture7/code/binary_counter.h index 864712f..6997a40 100755 --- a/alexander_stepaniv/lectures/lecture7/code/binary_counter.h +++ b/alexander_stepaniv/lectures/lecture7/code/binary_counter.h @@ -12,58 +12,83 @@ class binary_counter public: // Constructor. Input arguments: op and zero. - binary_counter(const Op& op, const T& zero) : - op(op), zero(zero) { - counter.reserve(24); + binary_counter(const Op &op, const T &zero) : op(op), zero(zero) + { + counter.reserve(24); } + void print() + { + auto first = counter.begin(); + auto last = counter.end(); + + while (first != last) + { + auto it = *first; + std::cout << *it << ","; + ++first; + } + std::cout << std::endl; + } // add - void add(T x) { + void add(T x) + { x = add_to_counter(counter.begin(), counter.end(), op, zero, x); - if (x != zero) counter.push_back(x); + if (x != zero) + counter.push_back(x); } // reduce // returns: value of the counter - T reduce() { + T reduce() + { return reduce_counter(counter.begin(), counter.end(), op, zero); } }; template // requires Op is BinaryOperation(T) -// and Op is associative +// and Op is associative // and I is ForwardIterator and ValueType(I) == T -T add_to_counter(I first, I last, Op op, const T& zero, T carry) { - // precondition: carry != zero - while (first != last) { - if (*first == zero) { - *first = carry; - return zero; - } - carry = op(*first, carry); - *first = zero; - ++first; +T add_to_counter(I first, I last, Op op, const T &zero, T carry) +{ + // precondition: carry != zero + while (first != last) + { + if (*first == zero) + { + *first = carry; + std::cout << "add_to_counter returning carry: zero" << std::endl; + return zero; } - return carry; + carry = op(*first, carry); + *first = zero; + ++first; + } + std::cout << "add_to_counter returning carry: " << *carry << std::endl; + return carry; } template // requires Op is BinaryOperation(T) -// and Op is associative +// and Op is associative // and I is ForwardIterator and ValueType(I) == T -T reduce_counter(I first, I last, Op op, const T& zero) { - while (first != last && *first == zero) { - ++first; - } - if (first == last) return zero; +T reduce_counter(I first, I last, Op op, const T &zero) +{ + while (first != last && *first == zero) + { + ++first; + } + if (first == last) + return zero; - T result = *first; - while (++first != last) { - if (*first != zero) { - result = op(*first, result); - } + T result = *first; + while (++first != last) + { + if (*first != zero) + { + result = op(*first, result); } - return result; -} - + } + return result; +} \ No newline at end of file diff --git a/alexander_stepaniv/lectures/lecture7/code/min.cpp b/alexander_stepaniv/lectures/lecture7/code/min.cpp index 0f2e144..fc2bdca 100755 --- a/alexander_stepaniv/lectures/lecture7/code/min.cpp +++ b/alexander_stepaniv/lectures/lecture7/code/min.cpp @@ -7,14 +7,17 @@ // - compares two iterators and returns the one // pointing to the smallest element template -class min_op +class min_op { private: Compare cmp; + public: - min_op(const Compare& cmp) : cmp(cmp) {} + min_op(const Compare &cmp) : cmp(cmp) {} template - I operator()(const I& x, const I& y) { + I operator()(const I &x, const I &y) + { + // std::cout << "comparing: " << *x << "," << *y << std::endl; return cmp(*y, *x) ? y : x; } }; @@ -22,21 +25,32 @@ class min_op template // requires I is a ForwardIterator // and Compare is a StrictWeakOrdering on ValueType(I) -I min_element_binary(I first, I last, Compare cmp) { +I min_element_binary(I first, I last, Compare cmp) +{ binary_counter > min_counter(min_op(cmp), last); - while (first != last) min_counter.add(first++); + while (first != last) + { + std::cout << "adding " << *first << std::endl; + min_counter.add(first++); + min_counter.print(); + } return min_counter.reduce(); } -int main() { +int main() +{ - // plugin whatever numbers you want to test with - int data[] = {9, 13, 7, 124, 32, 17, 8, 32, 237, 417, 41, 42, 13, 14, 15}; - int* end = data + sizeof(data)/sizeof(int); - int* min = min_element_binary(data, end, std::less()); - if (min == end) { - std::cout << "No elements" << std::endl; - } else { - std::cout << "Min is " << *min << std::endl; - } + // plugin whatever numbers you want to test with + // int data[] = {9, 13, 7, 124, 32, 17, 8, 32, 237, 417, 41, 42, 13, 14, 15}; + int data[] = {11, 10, 19, 13, 18, 4, 15}; + int *end = data + sizeof(data) / sizeof(int); + int *min = min_element_binary(data, end, std::less()); + if (min == end) + { + std::cout << "No elements" << std::endl; + } + else + { + std::cout << "Min is " << *min << std::endl; + } }