PLearn 0.1
VMatAccessBuffer.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // VMatAccessBuffer.cc
00004 //
00005 // Copyright (C) 2006 Christian Dorion
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Christian Dorion
00036 
00040 #include "VMatAccessBuffer.h"
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00045 VMatAccessBuffer::
00046 VMatAccessBuffer(VMat source, int max_size)
00047     : m_source( source ),
00048       m_current_row( -1 ),
00049       m_max_size( max_size )
00050 {
00051     m_row_buffer.resize(m_source.width());
00052 }
00053 
00054 void
00055 VMatAccessBuffer::
00056 getRow(int row, const Vec& rowbuf)
00057 {    
00058     int last_row = m_current_row + m_cached_rows.size() - 1;        
00059 
00060     bool much_below = false;
00061     if ( row < m_current_row )
00062     {
00063         int new_size = last_row - row + 1;
00064         much_below = ( new_size > m_max_size );
00065         if ( !much_below )
00066             while ( m_current_row > row ) {
00067                 m_source->getRow(--m_current_row, m_row_buffer);
00068                 m_cached_rows.push_front(m_row_buffer.copy());
00069             }
00070     }
00071 
00072     if ( much_below || row > last_row )
00073     {
00074         m_source->getRow(row, m_row_buffer);
00075         
00076         m_current_row = row;
00077         m_cached_rows.clear();
00078         m_cached_rows.push_back(m_row_buffer.copy());        
00079     }
00080     
00081     else
00082     {
00083         for ( ; m_current_row < row; ++m_current_row )
00084             m_cached_rows.pop_front();        
00085         m_row_buffer << m_cached_rows[0];
00086     }
00087     
00088     // Finally    
00089     rowbuf << m_row_buffer;
00090     PLASSERT( m_current_row == row );
00091     PLASSERT( int(m_cached_rows.size()) <= m_max_size );
00092 }
00093 
00094 void
00095 VMatAccessBuffer::
00096 lookAhead(int row, const Vec& rowbuf)
00097 {
00098     PLASSERT( row > m_current_row );
00099     int last_row = m_current_row + m_cached_rows.size() - 1;
00100 
00101     if ( row <= last_row )
00102         m_row_buffer << m_cached_rows[row-m_current_row];
00103     else
00104         while ( last_row < row )
00105         {
00106             m_source->getRow(++last_row, m_row_buffer);
00107             m_cached_rows.push_back(m_row_buffer.copy());
00108         }
00109     
00110     // Finally    
00111     rowbuf << m_row_buffer;
00112     PLASSERT( int(m_cached_rows.size()) <= m_max_size );
00113 }
00114 
00116 VMatAccessBuffer* VMatAccessBuffer::deepCopy(CopiesMap& copies) const
00117 {
00118     CopiesMap::iterator it = copies.find(this);
00119     if(it!=copies.end())  
00120         return (VMatAccessBuffer*) it->second;
00121   
00123     VMatAccessBuffer* deep_copy = new VMatAccessBuffer(*this);
00124 
00125     deepCopyField(this->m_source,      copies);
00126     deepCopyField(this->m_row_buffer,  copies);
00127     deepCopyField(this->m_cached_rows, copies);
00128     
00130     copies[this] = deep_copy;
00131 
00133     return deep_copy;
00134 }
00135 
00136 
00137 } // end of namespace PLearn
00138 
00139 
00140 /*
00141   Local Variables:
00142   mode:c++
00143   c-basic-offset:4
00144   c-file-style:"stroustrup"
00145   c-file-offsets:((innamespace . 0)(inline-open . 0))
00146   indent-tabs-mode:nil
00147   fill-column:79
00148   End:
00149 */
00150 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines