ESyS-Particle  4.0.1
RingBuffer.hpp
00001 
00002 //                                                         //
00003 // Copyright (c) 2003-2011 by The University of Queensland //
00004 // Earth Systems Science Computational Centre (ESSCC)      //
00005 // http://www.uq.edu.au/esscc                              //
00006 //                                                         //
00007 // Primary Business: Brisbane, Queensland, Australia       //
00008 // Licensed under the Open Software License version 3.0    //
00009 // http://www.opensource.org/licenses/osl-3.0.php          //
00010 //                                                         //
00012 
00013 template< typename T>
00014 RingBuffer<T>::RingBuffer(int s)
00015 {
00016   m_buffer=vector<T>(s);
00017   m_idx=0;
00018   m_size=s;
00019 }
00020 
00021 template< typename T>
00022 T& RingBuffer<T>::operator[](int i)
00023 {
00024   int real_idx=(m_idx+i)%m_size;
00025   return m_buffer[real_idx];
00026 }
00027 
00028 template< typename T>
00029 T RingBuffer<T>::operator[] (int i) const
00030 {
00031   int real_idx=(m_idx+i)%m_size;
00032   return m_buffer[real_idx];
00033 }
00034 
00035 template< typename T>
00036 void RingBuffer<T>::insert(const T& data)
00037 {
00038   m_idx=(m_idx+1)%m_size;
00039   m_buffer[m_idx]=data;
00040 }