/[debian]/mimetic/branches/upstream/current/mimetic/circular_buffer.h
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/circular_buffer.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File MIME type: text/plain
File size: 4177 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: circular_buffer.h,v 1.7 2005/02/23 10:26:14 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #ifndef _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
17 #define _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
18 #include <string>
19 #include <iostream>
20
21 namespace mimetic
22 {
23
24 template<typename T>
25 struct circular_buffer
26 {
27 typedef circular_buffer<T> self_type;
28 typedef T value_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)
32 {
33 m_pItem = new value_type[sz];
34 }
35 ~circular_buffer()
36 {
37 delete[] m_pItem;
38 }
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)
42 {
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];
46 }
47 circular_buffer& operator=(const circular_buffer& r)
48 {
49 m_sz = r.m_sz;
50 m_count = r.m_count;
51 m_first = r.m_first;
52 m_last = r.m_last;
53
54 if(m_pItem)
55 delete[] m_pItem;
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];
59 return *this;
60 }
61 inline void push_back(const value_type& c)
62 {
63 m_pItem[m_last] = c;
64 m_last = ++m_last % m_sz;
65 m_count += (m_count == m_sz ? 0 : 1);
66 }
67 inline void push_front(const value_type& c)
68 {
69 m_first = (--m_first + m_sz) % m_sz;
70 m_pItem[m_first] = c;
71 m_count += (m_count == m_sz ? 0 : 1);
72 }
73 inline void pop_front()
74 {
75 m_first = ++m_first % m_sz;
76 m_count--;
77 }
78 inline void pop_back()
79 {
80 m_last = (--m_last + m_sz) % m_sz;
81 m_count--;
82 }
83 inline const value_type& front() const
84 {
85 return m_pItem[m_first];
86 }
87 inline const value_type& back() const
88 {
89 int last = (m_last -1 + m_sz) % m_sz;
90 return m_pItem[last];
91 }
92 inline bool operator==(const std::string& r) const
93 {
94 if(m_count < r.length())
95 return false;
96 const self_type& me = *this;
97 for(size_type i = 0; i < m_count; i++)
98 if(me[i] != r[i])
99 return false;
100 return true;
101 }
102 inline bool operator!=(const std::string& r) const
103 {
104 return !operator==(r);
105 }
106 bool compare(size_type off, size_type n0, const std::string& r) const
107 {
108 const self_type& me = *this;
109 for(size_type i = 0; i < n0; i++)
110 if(me[off+i] != r[i])
111 return false;
112 return true;
113 }
114 inline value_type& operator[](unsigned int i) const
115 {
116 unsigned int idx = (m_first + i) % m_sz;
117 return m_pItem[idx];
118 }
119 inline bool empty() const
120 {
121 return m_count == 0;
122 }
123 std::string str() const
124 {
125 std::string result;
126 const self_type& me = *this;
127 for(size_type i = 0; i < m_count; i++)
128 result += me[i];
129 return result;
130 }
131 inline size_type count() const
132 {
133 return m_count;
134 }
135 inline size_type max_size() const
136 {
137 return m_sz;
138 }
139 private:
140 size_type m_sz, m_count;
141 int m_first, m_last;
142 value_type* m_pItem;
143 };
144
145 }
146
147 #endif
148

  ViewVC Help
Powered by ViewVC 1.1.26