Computer Architecture I ShanghaiTech University
HW6 HW7 HW8
In this assignment, you need to reimplement ring buffer in C++. You need to learn about following topics
You can download the template here. We provided a header file for you.
Your task is to implement a ring buffer in C++ using templates. In
ringbuffer.hpp
, we’ve declared all classes you need. You
need to finish the implementation of the class and methods in
ringbuffer.hpp
. All you implementation should be in a file
named ringbuffer.ipp
. If you don’t know what is
ipp
file, here is a brief explanation.
Before templates, you put the declarations of methods in the header file and the implementation went to a
.cpp
file. These files were compiled separately as their own compilation unit.With templates, this is no longer possible almost all template methods need to be defined in the header. To separated them at least on a logical level, some people put the declarations in the header but move all implementations of template methods to
.ipp
files (i
for “inline”) and include the.ipp
file at the end of the header.
Please obey the following rules:
vector
._Tp
has default constructor and
destructor. And it is both copy constructible and assignable.Requirements of implementing the ring buffer is the same as HW2.
read_pos
points to the slot that next read operation
should read from and write_pos
should point to the slot
that next write operation should write to.Other detailed requirements are in ringbuffer.hpp
.
Finish the implementation of
__detail::__iterator<_Tp>
. You have to implement the
following methods:
Constructor: __iterator ()
Default constructor.
Constructor: __iterator ( const __iterator & other )
Copy constructor.
Constructor: __iterator ( _Tp * ptr, RingBuffer<_Tp> * buffer )
Initialize the iterator with a pointer.
Destructor: __iterator ()
Destructor.
You should also overload the following operators:
=
: copy assignment operator.*
: dereference operator.->
: arrow operator.==
, !=
: comparison operator.++
, --
: self increment and decrement
operator.+
, -
: arithmetic operator.+=
, -=
: compound assignment operator.You can refer to std::vector<_Tp>::iterator
for
some detailed implementation.
It is similar to the iterator except for the dereferenced value should be const type (they cannot be modified). All methods for iterator should also be implemented for const iterator. You should also implement one additional method:
Constructor: __const_iterator ( const __iterator & other )
this method convert an iterator into a const iterator. Please notice that the reverse conversion (const iterator to iterator) is not required and you should consider why.
std::vector
is placed at
/usr/include/c++/<GCC version>/
.Archive ringbuffer.ipp
and create hw7.tar
.
Submit hw7.tar
to Autolab.
Last Modified: Fri May 6 01:06:34 CST 2022