00001 00012 #include "CircularBuffer.h" 00013 00014 #include "DataSourceException.h" 00015 00016 using std::string; 00017 00018 CircularBuffer::CircularBuffer ( const std::string & filename ) 00019 : NTuple ( filename ), 00020 m_capacity ( 0 ), 00021 m_next_row ( 0 ), 00022 m_has_filled ( false ) 00023 { 00024 } 00025 00026 CircularBuffer::CircularBuffer ( const std::vector< std::string > & v ) 00027 : NTuple ( v ), 00028 m_capacity ( 0 ), 00029 m_next_row ( 0 ), 00030 m_has_filled ( false ) 00031 { 00032 } 00033 00034 CircularBuffer::CircularBuffer ( const CircularBuffer & nt ) 00035 : NTuple ( nt ), 00036 m_capacity ( nt.m_capacity ), 00037 m_next_row ( nt.m_next_row ), 00038 m_has_filled ( nt.m_has_filled ) 00039 { 00040 } 00041 00042 CircularBuffer::CircularBuffer ( unsigned int n ) 00043 : NTuple ( n ), 00044 m_capacity ( 0 ), 00045 m_next_row ( 0 ), 00046 m_has_filled ( false ) 00047 { 00048 } 00049 00050 CircularBuffer::CircularBuffer () 00051 : NTuple (), 00052 m_capacity ( 0 ), 00053 m_next_row ( 0 ), 00054 m_has_filled ( false ) 00055 { 00056 } 00057 00058 void CircularBuffer::clear() 00059 { 00060 m_next_row = 0; 00061 m_has_filled = false; 00062 00063 NTuple::clear (); 00064 } 00065 00066 void 00067 CircularBuffer:: 00068 incrementRowIndex () 00069 { 00070 m_next_row++; 00071 if ( m_next_row == m_capacity ) { 00072 m_next_row = 0; 00073 m_has_filled = true; 00074 } 00075 } 00076 00077 void 00078 CircularBuffer:: 00079 addRow ( const std::vector< double > & v ) 00080 { 00081 if ( m_has_filled ) { 00082 NTuple::replaceRow ( m_next_row, v ); 00083 } 00084 else { 00085 NTuple::addRow ( v ); 00086 } 00087 00088 incrementRowIndex ( ); 00089 } 00090 00091 void CircularBuffer::reserve ( unsigned int count ) 00092 { 00093 if ( empty () == false ) { 00094 const string what ( "CircularBuffer: Attempt to set the capacity of " 00095 "non-empty buffer is not allowed" ); 00096 throw DataSourceException ( what ); 00097 } 00098 00099 NTuple::reserve ( count ); 00100 m_capacity = count; 00101 }