7 #ifndef _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
8 #define _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
16 struct circular_buffer
18 typedef circular_buffer<T> self_type;
20 typedef unsigned int size_type;
21 circular_buffer(
unsigned int sz = 4)
22 : m_sz(sz), m_count(0), m_first(0), m_last(0)
24 m_pItem =
new value_type[sz];
30 circular_buffer(
const circular_buffer& r)
31 : m_sz(r.m_sz), m_count(r.m_count),
32 m_first(r.m_first) ,m_last(r.m_last)
34 m_pItem =
new value_type[m_sz];
35 for(size_type i =0; i < m_sz; i++)
36 m_pItem[i] = r.m_pItem[i];
38 circular_buffer& operator=(
const circular_buffer& r)
47 m_pItem =
new value_type[m_sz];
48 for(size_type i =0; i < m_sz; i++)
49 m_pItem[i] = r.m_pItem[i];
52 inline void push_back(
const value_type& c)
55 m_last = ++m_last % m_sz;
56 m_count += (m_count == m_sz ? 0 : 1);
58 inline void push_front(
const value_type& c)
60 m_first = (--m_first + m_sz) % m_sz;
62 m_count += (m_count == m_sz ? 0 : 1);
64 inline void pop_front()
66 m_first = ++m_first % m_sz;
69 inline void pop_back()
71 m_last = (--m_last + m_sz) % m_sz;
74 inline const value_type& front()
const
76 return m_pItem[m_first];
78 inline const value_type& back()
const
80 int last = (m_last -1 + m_sz) % m_sz;
83 inline bool operator==(
const std::string& r)
const
85 if(m_count < r.length())
87 const self_type& me = *
this;
88 for(size_type i = 0; i < m_count; i++)
93 inline bool operator!=(
const std::string& r)
const
95 return !operator==(r);
97 bool compare(size_type off, size_type n0,
const std::string& r)
const
99 const self_type& me = *
this;
100 for(size_type i = 0; i < n0; i++)
101 if(me[off+i] != r[i])
105 inline value_type& operator[](
unsigned int i)
const
107 unsigned int idx = (m_first + i) % m_sz;
110 inline bool empty()
const
114 std::string str()
const
117 const self_type& me = *
this;
118 for(size_type i = 0; i < m_count; i++)
122 inline size_type count()
const
126 inline size_type max_size()
const
131 size_type m_sz, m_count;