m/b/circular: add pointer rebalancing operation
This commit is contained in:
parent
dd69da09c3
commit
52aa37dbf7
@ -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<char>;
|
||||||
template class util::memory::buffer::circular<uint8_t>;
|
template class util::memory::buffer::circular<uint8_t>;
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
#ifndef __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
|
#ifndef __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
|
||||||
#define __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
|
#define __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
|
||||||
|
|
||||||
|
#include "../../view.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
namespace util::memory::buffer {
|
namespace util::memory::buffer {
|
||||||
@ -49,6 +51,13 @@ namespace util::memory::buffer {
|
|||||||
|
|
||||||
size_t size (void) const;
|
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:
|
private:
|
||||||
value_type *m_begin, *m_end;
|
value_type *m_begin, *m_end;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user