Main Page   Class Hierarchy   Alphabetical List   Compound List   Examples  

circular_buffer.h

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