1
/***************************************************************************
2
copyright : (C) 2002-2005 by Stefano Barbato
3
email : stefano@codesink.org
5
$Id: circular_buffer.h,v 1.7 2005/02/23 10:26:14 tat Exp $
6
***************************************************************************/
8
/***************************************************************************
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
***************************************************************************/
16
#ifndef _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
17
#define _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
25
struct circular_buffer
27
typedef circular_buffer<T> self_type;
29
typedef unsigned int size_type;
30
circular_buffer(unsigned int sz = 4)
31
: m_sz(sz), m_count(0), m_first(0), m_last(0)
33
m_pItem = new value_type[sz];
39
circular_buffer(const circular_buffer& r)
40
: m_sz(r.m_sz), m_count(r.m_count),
41
m_first(r.m_first) ,m_last(r.m_last)
43
m_pItem = new value_type[m_sz];
44
for(size_type i =0; i < m_sz; i++)
45
m_pItem[i] = r.m_pItem[i];
47
circular_buffer& operator=(const circular_buffer& r)
56
m_pItem = new value_type[m_sz];
57
for(size_type i =0; i < m_sz; i++)
58
m_pItem[i] = r.m_pItem[i];
61
inline void push_back(const value_type& c)
64
m_last = ++m_last % m_sz;
65
m_count += (m_count == m_sz ? 0 : 1);
67
inline void push_front(const value_type& c)
69
m_first = (--m_first + m_sz) % m_sz;
71
m_count += (m_count == m_sz ? 0 : 1);
73
inline void pop_front()
75
m_first = ++m_first % m_sz;
78
inline void pop_back()
80
m_last = (--m_last + m_sz) % m_sz;
83
inline const value_type& front() const
85
return m_pItem[m_first];
87
inline const value_type& back() const
89
int last = (m_last -1 + m_sz) % m_sz;
92
inline bool operator==(const std::string& r) const
94
if(m_count < r.length())
96
const self_type& me = *this;
97
for(size_type i = 0; i < m_count; i++)
102
inline bool operator!=(const std::string& r) const
104
return !operator==(r);
106
bool compare(size_type off, size_type n0, const std::string& r) const
108
const self_type& me = *this;
109
for(size_type i = 0; i < n0; i++)
110
if(me[off+i] != r[i])
114
inline value_type& operator[](unsigned int i) const
116
unsigned int idx = (m_first + i) % m_sz;
119
inline bool empty() const
123
std::string str() const
126
const self_type& me = *this;
127
for(size_type i = 0; i < m_count; i++)
131
inline size_type count() const
135
inline size_type max_size() const
140
size_type m_sz, m_count;