Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

circular_buffer.h

00001 /***************************************************************************
00002     copyright            : (C) 2002-2008 by Stefano Barbato
00003     email                : stefano@codesink.org
00004 
00005     $Id: circular_buffer.h,v 1.8 2008-10-07 11:06:25 tat Exp $
00006  ***************************************************************************/
00007 #ifndef _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
00008 #define _MIMETIC_CODEC_CIRCULAR_BUFFER_H_
00009 #include <string>
00010 #include <iostream>
00011 
00012 namespace mimetic
00013 {
00014 
00015 template<typename T>
00016 struct circular_buffer
00017 {
00018     typedef circular_buffer<T> self_type;
00019     typedef T value_type;
00020     typedef unsigned int size_type;
00021     circular_buffer(unsigned int sz = 4)
00022     : m_sz(sz), m_count(0), m_first(0), m_last(0)
00023     {
00024         m_pItem = new value_type[sz];
00025     }
00026     ~circular_buffer()
00027     {
00028         delete[]  m_pItem;
00029     }
00030     circular_buffer(const circular_buffer& r)
00031     : m_sz(r.m_sz), m_count(r.m_count),
00032       m_first(r.m_first) ,m_last(r.m_last)
00033     {
00034          m_pItem = new value_type[m_sz];
00035         for(size_type i =0; i < m_sz; i++)
00036             m_pItem[i] = r.m_pItem[i];
00037     }
00038     circular_buffer& operator=(const circular_buffer& r)
00039     {
00040         m_sz = r.m_sz;
00041         m_count = r.m_count;
00042           m_first = r.m_first;
00043         m_last = r.m_last;
00044 
00045         if(m_pItem)
00046             delete[] m_pItem;
00047          m_pItem = new value_type[m_sz];
00048         for(size_type i =0; i < m_sz; i++)
00049             m_pItem[i] = r.m_pItem[i];
00050         return *this;
00051     }
00052     inline void push_back(const value_type& c)
00053     {
00054         m_pItem[m_last] = c;    
00055         m_last = ++m_last % m_sz;
00056         m_count += (m_count == m_sz ? 0 : 1);
00057     }
00058     inline void push_front(const value_type& c)
00059     {
00060         m_first = (--m_first + m_sz) % m_sz;        
00061         m_pItem[m_first] = c;    
00062         m_count += (m_count == m_sz ? 0 : 1);
00063     }
00064     inline void pop_front()
00065     {
00066         m_first = ++m_first % m_sz;        
00067         m_count--;
00068     }
00069     inline void pop_back()
00070     {
00071         m_last = (--m_last + m_sz) % m_sz;
00072         m_count--;
00073     }
00074     inline const value_type& front() const
00075     {
00076         return m_pItem[m_first];
00077     }
00078     inline const value_type& back() const
00079     {
00080         int last = (m_last -1 + m_sz) % m_sz;
00081         return m_pItem[last];
00082     }
00083     inline bool operator==(const std::string& r) const
00084     {
00085         if(m_count < r.length())
00086             return false;
00087         const self_type& me = *this;
00088         for(size_type i = 0; i < m_count; i++)
00089             if(me[i] != r[i])
00090                 return false;
00091         return true;
00092     }
00093     inline bool operator!=(const std::string& r) const
00094     {
00095         return !operator==(r);
00096     }    
00097     bool compare(size_type off, size_type n0, const std::string& r) const
00098     {
00099         const self_type& me = *this;
00100         for(size_type i = 0; i < n0; i++)
00101             if(me[off+i] != r[i])
00102                 return false;
00103         return true;
00104     }
00105     inline value_type& operator[](unsigned int i) const
00106     {
00107         unsigned int idx = (m_first + i) % m_sz;
00108         return m_pItem[idx];
00109     }
00110     inline bool empty() const
00111     {
00112         return m_count == 0;
00113     }
00114     std::string str() const
00115     {
00116         std::string result;
00117         const self_type& me = *this;
00118         for(size_type i = 0; i < m_count; i++)
00119             result += me[i];
00120         return result;
00121     }
00122     inline size_type count() const
00123     {
00124         return m_count;
00125     }
00126     inline size_type max_size() const
00127     {
00128         return m_sz;
00129     }
00130 private:
00131     size_type m_sz, m_count;
00132     int m_first, m_last;
00133     value_type* m_pItem;
00134 };
00135 
00136 }
00137 
00138 #endif
00139