Ex W7 |home |print view |recent changes |changed April 11, 2018 | exact |You are 3.237.186.116 <- set your identity!

### Line Iterator^

In many of the exercises in the CPlusPlus module you had to process input line by line. It would have been convenient to have a line iterator in order to use standard algorithms instead of the `while` loops used:

• Implement a line input iterator that that wraps an input stream.
• Use your line input iterator to improve your existing solution of one of the line-oriented CPlusPlus exercises.

You can use boost to simplify your solution.

### Fibonacci Iterator^

In this first exercise you will implement a simple iterator similar to the iterator for integers from the lecture.

Instead of just counting your iterator will iterate through the Fibonacci numbers (https://en.wikipedia.org/wiki/Fibonacci_number). The given test cases expect your Fibonacci sequence to start with the value `0` for the zeroth number, `1` for the first number, `1` for the second number, and so on.

You need to implement the following members of the Fibonacci iterator:

• Constructor initializing the iterator with the `n`th element (default is `0`).
• `operator*` for accessing the current element (Fibonacci number) of the iterator.
• `operator==` to see whether two iterators have the same current element.
• `operator!=` the inverse of the `operator==`.
• `operator++` prefix and postfix increment.

We suggest you store the index (`i`th number) and not the Fibonacci value itself.

Here are the test cases: https:files/Test.cpp

You shall implement two versions of this iterator:

1. Implement all operators yourself.
2. Use the facilities of boost iterators (i.e. based on a counting iterator).

Note: Don't worry about efficiency in this exercise.

### TESTAT 2: BoundedBuffer with iterators (const and non-const) and allowing non-default-constructible elements ^

Due date: Tuesday 1. May 2018 23:59 (CEST)

### Iterator for Dynamic Bounded Buffer^

Note: The task of this exercise will be part of Testat 2 (next week). Last week you have implemented a dynamic version of the bounded buffer. Finish that exercise first if you have not already completed it.

In this exercise you have to implement `begin` and `end` for your dynamic bounded buffer. This requires your own iterator type, which can cope with the non-consecutive nature of the elements in your heap memory. The implementation of your iterator must be robust, i.e. accesses outside the range specified by `begin` (inclusive) and `end` (exclusive) have to throw exceptions. It is not allowed to increment/decrement your iterator beyond these points (`begin`/`end`).

Create constant and non-constant iterators. Const iterators provide read-only access to the elements, while the non-const iterators allow changing the elements through the iterator. Donâ€™t just copy-waste your const iterator implementation! Try to find a smart way to reuse the parts of your implementation and let the compiler do the work of duplicating your code.

Hints:

• Define the iterator type as member of the bounded buffer.
• You are allowed to have two iterator classes (one for `const_iterator` and one for non-const `iterator`).
• Create corresponding type alias members in `BoundedBuffer`(`iterator` and `const_iterator`).
• If you struggle to figure out why certain tests are not green you may implement the << operator for your iterator type in the test cases to visualize its state.
• Use boost/operators.hpp to shorten your implementation.

Here are some test cases as suggestion to verify the functionality of your iterators. Note: It is just the suite for iterators, which you need to register in the `Test.cpp` file first:

If you struggle to get it all up and running at once, try to satisfy one test case after another. Comment out all test contents first. Add the contents of the first test (start at the top of the tests), make it compile, make it green, repeat with the next tests.

#### Non-Default-Constructible Elements^

Adapt your dynamic bounded buffer to allow non-default-constructible types as element type. Use the approach you have seen in the lecture last week (allocating a `std::byte[size]` array and use placement new to move/copy the elements to the corresponding location).

Make sure that elements in the buffer get destructed exactly once (no undestroyed elements and no double deletes)!

Since you do not allocate arrays of the element type anymore the test cases for `new` and `delete` change, i.e. those operators of the element type are not used anymore. The `bounded_buffer_heap_memory_suite` has been adapted accordingly.

Here are the test cases: https:files/NonDefaultConstructibleTypeTests.zip

Hints:

• You need a destructor
• You have to adapt the constructors
• You have to adapt the `push` and `pop` member functions
• You will not work with a pointer to the element type anymore but rather a pointer to a `std::byte` array (or `std::unique_ptr`). For accessing the elements in the memory, it is convenient to have a means for converting this `std::byte` pointer to a pointer to element type, e.g., by taking the address of the front() element.

 |home |print view |recent changes |changed April 11, 2018 | exact |You are 3.237.186.116 <- set your identity! Ex W7