PLearn 0.1
pl_io.h
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,2000 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 // Copyright (C) 2002 Frederic Morin, Xavier Saint-Mleux, Pascal Vincent
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  
00039 
00040 /* *******************************************************      
00041  * $Id: pl_io.h 10372 2011-09-23 19:52:15Z nouiz $
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00045 
00048 #ifndef pl_io_INC
00049 #define pl_io_INC
00050 
00051 #include <cstdio> // needed by g++ 4.5.1
00052 #include <iostream>
00053 #include <map>
00054 #include <plearn/math/pl_math.h>        
00055 
00056 namespace PLearn {
00057 using std::map;
00058 using std::pair;
00059 using std::istream;
00060 using std::ostream;
00061 
00062 // Support for outputing std::map on an ostream.
00063 template <class Key, class Value>
00064 ostream& operator<<(ostream& out, const map<Key, Value>& m)
00065 {
00066     out << "{" << flush;
00067     typename map<Key,Value>::const_iterator it  = m.begin();
00068 
00069     if ( m.size() > 0 )
00070     {
00071         for ( unsigned int elem = 0; elem < m.size()-1; elem++, it++ ) 
00072             out << it->first << " : " << it->second << ", " << flush;
00073 
00074         PLASSERT( it != m.end() );
00075         out << it->first << " : " << it->second << flush;
00076     }
00077   
00078     out << "}" << flush;
00079     return out;
00080 }
00081 
00082 
00083 //*** Binary read and write to/from std::stream ***
00084 
00087 template<class T>
00088 inline void binwrite(ostream& out, const T* x, int n)
00089 { for (int i=0;i<n;i++) binwrite(out,x[i]); }
00090 
00091 template<class T>
00092 inline void binread(istream& in, T* x, int n)
00093 { for (int i=0;i<n;i++) binread(in,x[i]); }
00094 
00095 template<class A,class B>
00096 inline void binwrite(ostream& out, const pair<A,B> x)
00097 { binwrite(out,x.first); binwrite(out,x.second); }
00098 
00099 template<class A,class B>
00100 inline void binread(istream& in, pair<A,B>& x)
00101 { binread(in,x.first); binread(in,x.second); }
00102 
00103 
00105 inline void binwrite(ostream& out, char x) { out.put(x); }
00106 inline void binread(istream& in, char& x) { in.get(x); }
00107 inline void binwrite(ostream& out, unsigned char x) { out.put(x); }
00108 inline void binread(istream& in, unsigned char& x) { in.get((char&)x); }
00109 inline void binwrite(ostream& out, int x) { out.write((char*)&x,sizeof(int)); }
00110 inline void binread(istream& in, int& x) { in.read((char*)&x,sizeof(int));  }
00111 inline void binwrite(ostream& out, unsigned int x) { out.write((char*)&x,sizeof(unsigned int)); }
00112 inline void binread(istream& in, unsigned int& x) { in.read((char*)&x,sizeof(unsigned int));  }
00113 inline void binwrite(ostream& out, short x) { out.write((char*)&x,sizeof(short)); }
00114 inline void binread(istream& in, short& x) { in.read((char*)&x,sizeof(short));  }
00115 inline void binwrite(ostream& out, unsigned short x) { out.write((char*)&x,sizeof(unsigned short)); }
00116 inline void binread(istream& in, unsigned short& x) { in.read((char*)&x,sizeof(unsigned short));  }
00118 inline void binwrite(ostream& out, bool x) { binwrite(out,(unsigned short)x); }
00119 
00120 // norman: usigned short to boolean?? Performance hit!!
00121 //inline void binread(istream& in, bool& x) { unsigned short u; binread(in,u); x=u; }
00122 inline void binread(istream& in, bool& x) { 
00123     unsigned short u; binread(in,u); 
00124     u == 0 ? x = false : x = true;
00125 }
00126 
00127 // The following read/write floats (4 bytes) on disk, regardless of whether the memory-elements are float or double
00128 inline void binwrite(ostream& out, float x) { out.write((char*)&x,sizeof(float)); }
00129 inline void binread(istream& in, float& x) { in.read((char*)&x,sizeof(float));  }
00130 inline void binwrite(ostream& out, double x) { binwrite(out, float(x)); }
00131 inline void binread(istream& in, double& x) { float f; binread(in,f); x = double(f); }
00132 
00133 // The following read/write doubles (8 bytes) on disk, regardless of whether the memory-elements are float or double
00134 inline void binwrite_double(ostream& out, double x) { out.write((char*)&x,sizeof(double)); }
00135 inline void binread_double(istream& in, double& x) { in.read((char*)&x,sizeof(double));  }
00136 inline void binwrite_double(ostream& out, float x) { binwrite_double(out, double(x)); }
00137 inline void binread_double(istream& in, float& x) { double d; binread_double(in,d); x = float(d); }
00138 
00139 
00141 inline void binwrite(ostream& out, const int* x, int n)
00142     { out.write((char*)x, int(n*sizeof(int))); }
00143 inline void binread(istream& in, int* x, int n)
00144     { in.read((char*)x, int(n*sizeof(int))); }
00145 inline void binwrite(ostream& out, const unsigned int* x, int n)
00146     { out.write((char*)x, int(n*sizeof(unsigned int))); }
00147 inline void binread(istream& in, unsigned int* x, int n)
00148     { in.read((char*)x, int(n*sizeof(unsigned int))); }
00149 inline void binwrite(ostream& out, const short* x, int n)
00150     { out.write((char*)x, int(n*sizeof(short))); }
00151 inline void binread(istream& in, short* x, int n)
00152     { in.read((char*)x, int(n*sizeof(short))); }
00153 inline void binwrite(ostream& out, const unsigned short* x, int n)
00154     { out.write((char*)x, int(n*sizeof(unsigned short))); }
00155 inline void binread(istream& in, unsigned short* x, int n)
00156     { in.read((char*)x, int(n*sizeof(unsigned short))); }
00157 
00158 // The followind read/write 4-byte-floats on disk (whether we pass them a float* or a double*)
00159 inline void binwrite(ostream& out, const float *x, int n)
00160     { out.write((char*)x, int(n*sizeof(float))); }
00161 inline void binread(istream& in, float* x, int n)
00162     { in.read((char*)x, int(n*sizeof(float))); }
00163 inline void binwrite(ostream& out, const double* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); }
00164 inline void binread(istream& in, double* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); }
00165 
00166 // The followind read/write 8-byte-doubles on disk (whether we pass them a float* or a double*)
00167 inline void binwrite_double(ostream& out, const double *x, int n)
00168     { out.write((char*)x, int(n*sizeof(double))); }
00169 inline void binread_double(istream& in, double* x, int n)
00170     { in.read((char*)x, int(n*sizeof(double))); }
00171 inline void binwrite_double(ostream& out, const float* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); }
00172 inline void binread_double(istream& in, float* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); }
00173 
00176 void binwrite_compressed(ostream& out, const double* data, int l);
00177 void binread_compressed(istream& in, double* data, int l);
00178 void binwrite_compressed(ostream& out, const float* data, int l);
00179 void binread_compressed(istream& in, float* data, int l);
00180 
00181 
00182 //*** Binary read and write to/from FILE* ***
00183 
00186 template<class T>
00187 inline void binwrite(FILE* out, const T* x, int n)
00188 { for (int i=0;i<n;i++) binwrite(out,x[i]); }
00189 
00190 template<class T>
00191 inline void binread(FILE* in, T* x, int n)
00192 { for (int i=0;i<n;i++) binread(in,x[i]); }
00193 
00194 template<class A,class B>
00195 inline void binwrite(FILE* out, const pair<A,B> x)
00196 { binwrite(out,x.first); binwrite(out,x.second); }
00197 
00198 template<class A,class B>
00199 inline void binread(FILE* in, pair<A,B>& x)
00200 { binread(in,x.first); binread(in,x.second); }
00201 
00202 
00204 inline void binwrite(FILE* out, char x) { putc((unsigned char)x, out); }
00205 inline void binread(FILE* in, char& x) { x = (char)getc(in); }
00206 inline void binwrite(FILE* out, unsigned char x) { putc(x,out); }
00207 inline void binread(FILE* in, unsigned char& x) { x = (unsigned char)getc(in); }
00208 inline void binwrite(FILE* out, int x) { fwrite(&x,sizeof(int),1,out); }
00209 inline void binread(FILE* in, int& x) { fread(&x,sizeof(int),1,in);  }
00210 inline void binwrite(FILE* out, unsigned int x) { fwrite(&x,sizeof(unsigned int),1,out); }
00211 inline void binread(FILE* in, unsigned int& x) { fread(&x,sizeof(unsigned int),1,in);  }
00212 inline void binwrite(FILE* out, short x) { fwrite(&x,sizeof(short),1,out); }
00213 inline void binread(FILE* in, short& x) { fread(&x,sizeof(short),1,in);  }
00214 inline void binwrite(FILE* out, unsigned short x) { fwrite(&x,sizeof(unsigned short),1,out); }
00215 inline void binread(FILE* in, unsigned short& x) { fread(&x,sizeof(unsigned short),1,in);  }
00217 inline void binwrite(FILE* out, bool x) { binwrite(out,(unsigned short)x); }
00218 // norman: usigned short to boolean?? At least use a cast
00219 //inline void binread(FILE* in, bool& x) { unsigned short u; binread(in,u); x=u; }
00220 inline void binread(FILE* in, bool& x) { 
00221     unsigned short u; binread(in,u); 
00222     u == 0 ? x = false : x = true;
00223 }
00224 
00225 // The following read/write floats (4 bytes) on disk, regardless of whether the memory-elements are float or double
00226 inline void binwrite(FILE* out, float x) { fwrite(&x,sizeof(float),1,out); }
00227 inline void binread(FILE* in, float& x) { fread(&x,sizeof(float),1,in);  }
00228 inline void binwrite(FILE* out, double x) { binwrite(out, float(x)); }
00229 inline void binread(FILE* in, double& x) { float f; binread(in,f); x = double(f); }
00230 
00231 // The following read/write doubles (8 bytes) on disk, regardless of whether the memory-elements are float or double
00232 inline void binwrite_double(FILE* out, double x) { fwrite(&x,sizeof(double),1,out); }
00233 inline void binread_double(FILE* in, double& x) { fread(&x,sizeof(double),1,in);  }
00234 inline void binwrite_double(FILE* out, float x) { binwrite_double(out, double(x)); }
00235 inline void binread_double(FILE* in, float& x) { double d; binread_double(in,d); x = float(d); }
00236 
00237 
00239 inline void binwrite(FILE* out, const int* x, int n) { fwrite(x, sizeof(int),n,out); }
00240 inline void binread(FILE* in, int* x, int n) { fread(x, sizeof(int),n,in);  }
00241 inline void binwrite(FILE* out, const unsigned int* x, int n) { fwrite(x, sizeof(unsigned int),n,out); }
00242 inline void binread(FILE* in, unsigned int* x, int n)  { fread(x, sizeof(unsigned int),n,in);  }
00243 inline void binwrite(FILE* out, const short* x, int n) { fwrite(x, sizeof(short),n,out); }
00244 inline void binread(FILE* in, short* x, int n) { fread(x, sizeof(short),n,in);  }
00245 inline void binwrite(FILE* out, const unsigned short* x, int n) { fwrite(x, sizeof(unsigned short),n,out); }
00246 inline void binread(FILE* in, unsigned short* x, int n) { fread(x, sizeof(unsigned short),n,in);  }
00247 
00248 // The followind read/write 4-byte-floats on disk (whether we pass them a float* or a double*)
00249 inline void binwrite(FILE* out, const float *x, int n) { fwrite(x, sizeof(float),n,out); }
00250 inline void binread(FILE* in, float* x, int n) { fread(x, sizeof(float),n,in);  }
00251 inline void binwrite(FILE* out, const double* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); }
00252 inline void binread(FILE* in, double* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); }
00253 
00254 // The followind read/write 8-byte-doubles on disk (whether we pass them a float* or a double*)
00255 inline void binwrite_double(FILE* out, const double *x, int n) { fwrite(x, sizeof(double),n,out); }
00256 inline void binread_double(FILE* in, double* x, int n) { fread(x, sizeof(double),n,in);  }
00257 inline void binwrite_double(FILE* out, const float* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); }
00258 inline void binread_double(FILE* in, float* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); }
00259 
00260 void binwrite_compressed(FILE* out, const double* data, int l);
00261 void binread_compressed(FILE* in, double* data, int l);
00262 void binwrite_compressed(FILE* out, const float* data, int l);
00263 void binread_compressed(FILE* in, float* data, int l);
00264 
00266 inline void read_compr_mode_and_size_ptr(char*& in, unsigned char& mode, int& size);
00267 void compress_vec(char* comprbuf, const double* data, int l, bool double_stored_as_float=false);
00268 void uncompress_vec(char* comprbuf, double* data, int l, bool double_stored_as_float=false);
00269 
00270 
00271 // *********       NEW COMPRESSION FORMAT        **********
00272 // (see its description in .cc)
00273 
00278 size_t new_write_compressed(FILE* out, real* vec, int l, double tolerance=1e-6, bool swap_endians=false);
00279 
00284 size_t new_read_compressed(FILE* in, real* vec, int l, bool swap_endians=false);
00285 
00286 
00287 
00288 } // end of namespace PLearn
00289 
00290 
00291 #endif
00292 
00293 
00294 /*
00295   Local Variables:
00296   mode:c++
00297   c-basic-offset:4
00298   c-file-style:"stroustrup"
00299   c-file-offsets:((innamespace . 0)(inline-open . 0))
00300   indent-tabs-mode:nil
00301   fill-column:79
00302   End:
00303 */
00304 // 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