Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  
circular_buffer.h
1 /***************************************************************************
2  copyright : (C) 2002-2008 by Stefano Barbato
3  email : stefano@codesink.org
4 
5  $Id: circular_buffer.h,v 1.8 2008-10-07 11:06:25 tat Exp $
6  ***************************************************************************/
7 #ifndef _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
8 #define _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
9 #include <string>
10 #include <iostream>
11 
12 namespace mimetic
13 {
14 
15 template<typename T>
16 struct circular_buffer
17 {
18  typedef circular_buffer<T> self_type;
19  typedef T value_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)
23  {
24  m_pItem = new value_type[sz];
25  }
26  ~circular_buffer()
27  {
28  delete[] m_pItem;
29  }
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)
33  {
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];
37  }
38  circular_buffer& operator=(const circular_buffer& r)
39  {
40  m_sz = r.m_sz;
41  m_count = r.m_count;
42  m_first = r.m_first;
43  m_last = r.m_last;
44 
45  if(m_pItem)
46  delete[] m_pItem;
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];
50  return *this;
51  }
52  inline void push_back(const value_type& c)
53  {
54  m_pItem[m_last] = c;
55  m_last = ++m_last % m_sz;
56  m_count += (m_count == m_sz ? 0 : 1);
57  }
58  inline void push_front(const value_type& c)
59  {
60  m_first = (--m_first + m_sz) % m_sz;
61  m_pItem[m_first] = c;
62  m_count += (m_count == m_sz ? 0 : 1);
63  }
64  inline void pop_front()
65  {
66  m_first = ++m_first % m_sz;
67  m_count--;
68  }
69  inline void pop_back()
70  {
71  m_last = (--m_last + m_sz) % m_sz;
72  m_count--;
73  }
74  inline const value_type& front() const
75  {
76  return m_pItem[m_first];
77  }
78  inline const value_type& back() const
79  {
80  int last = (m_last -1 + m_sz) % m_sz;
81  return m_pItem[last];
82  }
83  inline bool operator==(const std::string& r) const
84  {
85  if(m_count < r.length())
86  return false;
87  const self_type& me = *this;
88  for(size_type i = 0; i < m_count; i++)
89  if(me[i] != r[i])
90  return false;
91  return true;
92  }
93  inline bool operator!=(const std::string& r) const
94  {
95  return !operator==(r);
96  }
97  bool compare(size_type off, size_type n0, const std::string& r) const
98  {
99  const self_type& me = *this;
100  for(size_type i = 0; i < n0; i++)
101  if(me[off+i] != r[i])
102  return false;
103  return true;
104  }
105  inline value_type& operator[](unsigned int i) const
106  {
107  unsigned int idx = (m_first + i) % m_sz;
108  return m_pItem[idx];
109  }
110  inline bool empty() const
111  {
112  return m_count == 0;
113  }
114  std::string str() const
115  {
116  std::string result;
117  const self_type& me = *this;
118  for(size_type i = 0; i < m_count; i++)
119  result += me[i];
120  return result;
121  }
122  inline size_type count() const
123  {
124  return m_count;
125  }
126  inline size_type max_size() const
127  {
128  return m_sz;
129  }
130 private:
131  size_type m_sz, m_count;
132  int m_first, m_last;
133  value_type* m_pItem;
134 };
135 
136 }
137 
138 #endif
139 
Definition: body.h:17