m/b/circular: add pointer rebalancing operation

This commit is contained in:
Danny Robson 2017-12-30 13:39:42 +11:00
parent dd69da09c3
commit 52aa37dbf7
2 changed files with 38 additions and 0 deletions

View File

@ -152,6 +152,35 @@ circular<ValueT>::size (void) const
}
//-----------------------------------------------------------------------------
template <typename ValueT>
typename circular<ValueT>::iterator
circular<ValueT>::constrain (iterator cursor)
{
CHECK_LIMIT (cursor, m_begin, m_begin + 2 * size ());
return m_begin + (cursor - m_begin) % size ();
}
//-----------------------------------------------------------------------------
template <typename ValueT>
util::view<typename circular<ValueT>::iterator>
circular<ValueT>::constrain (util::view<iterator> window)
{
CHECK_LIMIT (window.begin (), m_begin, m_begin + 2 * size ());
CHECK_LIMIT (window.end (), m_begin, m_begin + 2 * size ());
auto first = window.begin ();
auto last = first + window.size ();
util::view res { first, last };
CHECK_EQ (res.size (), window.size ());
CHECK_LE (res.begin (), res.end ());
return res;
}
///////////////////////////////////////////////////////////////////////////////
template class util::memory::buffer::circular<char>;
template class util::memory::buffer::circular<uint8_t>;

View File

@ -17,6 +17,8 @@
#ifndef __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
#define __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
#include "../../view.hpp"
#include <cstddef>
namespace util::memory::buffer {
@ -49,6 +51,13 @@ namespace util::memory::buffer {
size_t size (void) const;
/// rebases the pointer so that it points to within the first
/// repetition of the data buffer.
iterator constrain (iterator);
/// rebases a pair of pointers so they fall within a contiguous span of
/// the data buffer.
util::view<iterator> constrain (util::view<iterator>);
private:
value_type *m_begin, *m_end;