PLearn 0.1
BufferedIntVecFile.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 // Copyright (C) 2006 Xavier Saint-Mleux
00007 //
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 
00038 #include "BufferedIntVecFile.h"
00039 
00040 namespace PLearn {
00041 using namespace std;
00042 
00043 /*
00044 BufferedIntVecFile::BufferedIntVecFile(const IntVecFile& other)
00045     : filename(other.filename), f(0), length_(other.length_),
00046       version_number_(other.version_number_), endianness_(other.endianness_)
00047 {
00048     open(filename, false / * readonly * /);
00049 }
00050 */
00051 
00052 void BufferedIntVecFile::open(const string& the_filename, bool readwrite)
00053 {
00054     flush();
00055     inherited::open(the_filename, readwrite);
00056     bufstart= -1;
00057     bufmod= false;
00058 }
00059 
00060 void BufferedIntVecFile::close()
00061 {
00062     flush();
00063     inherited::close();
00064 }
00065 
00066 int BufferedIntVecFile::get(int i) const
00067 {
00068     if(bufstart < 0 || bufstart > i || i >= bufstart+buflen)
00069         const_cast<BufferedIntVecFile*>(this)->getBuf(i);
00070     return buf[i-bufstart];
00071 }
00072 
00073 void BufferedIntVecFile::put(int i, int value)
00074 {
00075     if(bufstart < 0 || bufstart > i || i >= bufstart+buflen)
00076         getBuf(i);
00077     bufmod= true;
00078     buf[i-bufstart]= value;
00079     if(i>=length_)
00080         length_ = i+1;
00081 }
00082 
00083 
00084 BufferedIntVecFile::~BufferedIntVecFile()
00085 {
00086     close();
00087     if(buf) delete[] buf;
00088 }
00089 
00090 void BufferedIntVecFile::getBuf(int bufstart_)
00091 {
00092     flush();
00093     bufstart= bufstart_;
00094     for(int i= 0; i < buflen && i+bufstart < length(); ++i)
00095         buf[i]= inherited::get(i+bufstart);
00096 }
00097 
00098 void BufferedIntVecFile::flush()
00099 {
00100 /*
00101     if(bufmod)
00102         for(int i= 0; i < buflen && i+bufstart < length(); ++i)
00103             inherited::put(i+bufstart, buf[i]);
00104 */
00105 
00106     if(bufmod)
00107     {
00108         int len= min(buflen, length()-bufstart);
00109         seek_to_index(bufstart);
00110         if (byte_order() != endianness_) 
00111         {
00112             TVec<int> new_vec(len);
00113             new_vec.copyFrom(buf, len);
00114             endianswap(new_vec.data(), new_vec.length());
00115             fwrite(new_vec.data(), sizeof(int), new_vec.length(), f);
00116         }
00117         else 
00118         {
00119             fwrite(buf, sizeof(int), len, f);
00120         }
00121     }
00122 
00123     bufmod= false;
00124 }
00125 
00126 
00127 } // end of namespace PLearn
00128 
00129 
00130 
00131 /*
00132   Local Variables:
00133   mode:c++
00134   c-basic-offset:4
00135   c-file-style:"stroustrup"
00136   c-file-offsets:((innamespace . 0)(inline-open . 0))
00137   indent-tabs-mode:nil
00138   fill-column:79
00139   End:
00140 */
00141 // 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