Everything is a buffer
This commit is contained in:
+48
-31
@@ -39,21 +39,53 @@ class Buffer
|
||||
{
|
||||
uint m_index;
|
||||
uint m_mask;
|
||||
int m_offset;
|
||||
uint m_increment;
|
||||
size_t m_capacity;
|
||||
vector<T> m_buffer;
|
||||
size_t m_allocated_capacity;
|
||||
|
||||
std::vector<T> m_buffer;
|
||||
|
||||
public:
|
||||
Buffer(size_t capacity)
|
||||
: m_index(0)
|
||||
, m_mask(0)
|
||||
, m_offset(0)
|
||||
, m_increment(1)
|
||||
, m_capacity(0)
|
||||
, m_buffer()
|
||||
, m_allocated_capacity(1)
|
||||
{
|
||||
resize(capacity);
|
||||
m_buffer.assign(m_allocated_capacity, T());
|
||||
}
|
||||
|
||||
Buffer(const Buffer<T> &buffer, int offset=0, uint increment=1)
|
||||
: m_buffer(buffer.m_buffer)
|
||||
, m_index(buffer.m_index)
|
||||
, m_mask(buffer.m_mask)
|
||||
, m_offset(offset)
|
||||
, m_increment(increment)
|
||||
, m_capacity(buffer.m_capacity)
|
||||
, m_allocated_capacity(buffer.m_allocated_capacity)
|
||||
{
|
||||
}
|
||||
|
||||
Buffer(const std::vector<T> &v)
|
||||
: m_index(0)
|
||||
, m_mask(0)
|
||||
, m_offset(0)
|
||||
, m_increment(1)
|
||||
, m_capacity(v.capacity())
|
||||
, m_buffer(v)
|
||||
, m_allocated_capacity(1)
|
||||
{
|
||||
resize(v.capacity());
|
||||
}
|
||||
|
||||
size_t capacity()
|
||||
{
|
||||
return m_buffer.capacity();
|
||||
return m_capacity/m_increment;
|
||||
}
|
||||
|
||||
void reset()
|
||||
@@ -61,16 +93,16 @@ class Buffer
|
||||
m_index = 0;
|
||||
}
|
||||
|
||||
void resize(int capacity, T value=T())
|
||||
void resize(uint capacity)
|
||||
{
|
||||
m_capacity = 1;
|
||||
while (m_capacity < capacity)
|
||||
m_allocated_capacity = 1;
|
||||
while (m_allocated_capacity < capacity)
|
||||
{
|
||||
m_capacity *= 2;
|
||||
m_allocated_capacity *= 2;
|
||||
}
|
||||
|
||||
m_buffer.assign(m_capacity, value);
|
||||
m_mask = unsigned(m_capacity - 1);
|
||||
m_capacity = capacity;
|
||||
m_mask = unsigned(m_allocated_capacity - 1);
|
||||
m_index = 0;
|
||||
}
|
||||
|
||||
@@ -112,41 +144,26 @@ class Buffer
|
||||
return *this;
|
||||
}
|
||||
|
||||
typedef std::vector<T>::iterator iterator;
|
||||
typedef std::vector<T>::const_iterator const_iterator;
|
||||
iterator begin()
|
||||
bool operator == (const Buffer &other) const
|
||||
{
|
||||
return m_buffer.begin();
|
||||
return m_buffer == other.m_buffer;
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return m_buffer.end();
|
||||
}
|
||||
|
||||
const_iterator begin() const
|
||||
{
|
||||
return m_buffer.begin();
|
||||
}
|
||||
|
||||
const_iterator end() const
|
||||
{
|
||||
return m_buffer.end();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual T & at(int offset)
|
||||
|
||||
T& at(int offset)
|
||||
{
|
||||
int i = (m_index + (uint)offset) & m_mask;
|
||||
int i = (m_offset + m_index + (uint)offset*m_increment) & m_mask;
|
||||
return m_buffer[i];
|
||||
}
|
||||
|
||||
virtual const T & at(int offset) const
|
||||
T const & at(int offset) const
|
||||
{
|
||||
return at(offset);
|
||||
int i = (m_offset + m_index + (uint)offset*m_increment) & m_mask;
|
||||
return m_buffer[i];
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user