Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

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,v 1.12 2004/07/21 16:30:51 chrish42 Exp $ 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 <cctype> 00052 #include <iostream> 00053 //#include <iomanip> 00054 //#include <fstream> 00055 //#include <string> 00056 //#include <vector> 00057 //#include <map> 00058 //#include <set> 00059 00060 //#include "PStream.h" 00061 //#include "fileutils.h" 00062 #include <plearn/math/pl_math.h> 00063 //#include "stringutils.h" 00064 00065 namespace PLearn { 00066 using namespace std; 00067 00068 00069 //*** Binary read and write to/from std::stream *** 00070 00073 template<class T> 00074 inline void binwrite(ostream& out, const T* x, int n) 00075 { for (int i=0;i<n;i++) binwrite(out,x[i]); } 00076 00077 template<class T> 00078 inline void binread(istream& in, T* x, int n) 00079 { for (int i=0;i<n;i++) binread(in,x[i]); } 00080 00081 template<class A,class B> 00082 inline void binwrite(ostream& out, const pair<A,B> x) 00083 { binwrite(out,x.first); binwrite(out,x.second); } 00084 00085 template<class A,class B> 00086 inline void binread(istream& in, pair<A,B>& x) 00087 { binread(in,x.first); binread(in,x.second); } 00088 00089 00091 inline void binwrite(ostream& out, char x) { out.put(x); } 00092 inline void binread(istream& in, char& x) { in.get(x); } 00093 inline void binwrite(ostream& out, unsigned char x) { out.put(x); } 00094 inline void binread(istream& in, unsigned char& x) { in.get((char&)x); } 00095 inline void binwrite(ostream& out, int x) { out.write((char*)&x,sizeof(int)); } 00096 inline void binread(istream& in, int& x) { in.read((char*)&x,sizeof(int)); } 00097 inline void binwrite(ostream& out, unsigned int x) { out.write((char*)&x,sizeof(unsigned int)); } 00098 inline void binread(istream& in, unsigned int& x) { in.read((char*)&x,sizeof(unsigned int)); } 00099 inline void binwrite(ostream& out, short x) { out.write((char*)&x,sizeof(short)); } 00100 inline void binread(istream& in, short& x) { in.read((char*)&x,sizeof(short)); } 00101 inline void binwrite(ostream& out, unsigned short x) { out.write((char*)&x,sizeof(unsigned short)); } 00102 inline void binread(istream& in, unsigned short& x) { in.read((char*)&x,sizeof(unsigned short)); } 00104 inline void binwrite(ostream& out, bool x) { binwrite(out,(unsigned short)x); } 00105 00106 // norman: usigned short to boolean?? Performance hit!! 00107 //inline void binread(istream& in, bool& x) { unsigned short u; binread(in,u); x=u; } 00108 inline void binread(istream& in, bool& x) { 00109 unsigned short u; binread(in,u); 00110 u == 0 ? x = false : x = true; 00111 } 00112 00113 // The following read/write floats (4 bytes) on disk, regardless of whether the memory-elements are float or double 00114 inline void binwrite(ostream& out, float x) { out.write((char*)&x,sizeof(float)); } 00115 inline void binread(istream& in, float& x) { in.read((char*)&x,sizeof(float)); } 00116 inline void binwrite(ostream& out, double x) { binwrite(out, float(x)); } 00117 inline void binread(istream& in, double& x) { float f; binread(in,f); x = double(f); } 00118 00119 // The following read/write doubles (8 bytes) on disk, regardless of whether the memory-elements are float or double 00120 inline void binwrite_double(ostream& out, double x) { out.write((char*)&x,sizeof(double)); } 00121 inline void binread_double(istream& in, double& x) { in.read((char*)&x,sizeof(double)); } 00122 inline void binwrite_double(ostream& out, float x) { binwrite_double(out, double(x)); } 00123 inline void binread_double(istream& in, float& x) { double d; binread_double(in,d); x = float(d); } 00124 00125 00127 inline void binwrite(ostream& out, const int* x, int n) { out.write((char*)x, n*sizeof(int)); } 00128 inline void binread(istream& in, int* x, int n) { in.read((char*)x, n*sizeof(int)); } 00129 inline void binwrite(ostream& out, const unsigned int* x, int n) { out.write((char*)x, n*sizeof(unsigned int)); } 00130 inline void binread(istream& in, unsigned int* x, int n) { in.read((char*)x, n*sizeof(unsigned int)); } 00131 inline void binwrite(ostream& out, const short* x, int n) { out.write((char*)x, n*sizeof(short)); } 00132 inline void binread(istream& in, short* x, int n) { in.read((char*)x, n*sizeof(short)); } 00133 inline void binwrite(ostream& out, const unsigned short* x, int n) { out.write((char*)x, n*sizeof(unsigned short)); } 00134 inline void binread(istream& in, unsigned short* x, int n) { in.read((char*)x, n*sizeof(unsigned short)); } 00135 00136 // The followind read/write 4-byte-floats on disk (whether we pass them a float* or a double*) 00137 inline void binwrite(ostream& out, const float *x, int n) { out.write((char*)x, n*sizeof(float)); } 00138 inline void binread(istream& in, float* x, int n) { in.read((char*)x, n*sizeof(float)); } 00139 inline void binwrite(ostream& out, const double* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); } 00140 inline void binread(istream& in, double* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); } 00141 00142 // The followind read/write 8-byte-doubles on disk (whether we pass them a float* or a double*) 00143 inline void binwrite_double(ostream& out, const double *x, int n) { out.write((char*)x, n*sizeof(double)); } 00144 inline void binread_double(istream& in, double* x, int n) { in.read((char*)x, n*sizeof(double)); } 00145 inline void binwrite_double(ostream& out, const float* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); } 00146 inline void binread_double(istream& in, float* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); } 00147 00150 void binwrite_compressed(ostream& out, const double* data, int l); 00151 void binread_compressed(istream& in, double* data, int l); 00152 void binwrite_compressed(ostream& out, const float* data, int l); 00153 void binread_compressed(istream& in, float* data, int l); 00154 00155 00156 //*** Binary read and write to/from FILE* *** 00157 00160 template<class T> 00161 inline void binwrite(FILE* out, const T* x, int n) 00162 { for (int i=0;i<n;i++) binwrite(out,x[i]); } 00163 00164 template<class T> 00165 inline void binread(FILE* in, T* x, int n) 00166 { for (int i=0;i<n;i++) binread(in,x[i]); } 00167 00168 template<class A,class B> 00169 inline void binwrite(FILE* out, const pair<A,B> x) 00170 { binwrite(out,x.first); binwrite(out,x.second); } 00171 00172 template<class A,class B> 00173 inline void binread(FILE* in, pair<A,B>& x) 00174 { binread(in,x.first); binread(in,x.second); } 00175 00176 00178 inline void binwrite(FILE* out, char x) { putc((unsigned char)x, out); } 00179 inline void binread(FILE* in, char& x) { x = (char)getc(in); } 00180 inline void binwrite(FILE* out, unsigned char x) { putc(x,out); } 00181 inline void binread(FILE* in, unsigned char& x) { x = (unsigned char)getc(in); } 00182 inline void binwrite(FILE* out, int x) { fwrite(&x,sizeof(int),1,out); } 00183 inline void binread(FILE* in, int& x) { fread(&x,sizeof(int),1,in); } 00184 inline void binwrite(FILE* out, unsigned int x) { fwrite(&x,sizeof(unsigned int),1,out); } 00185 inline void binread(FILE* in, unsigned int& x) { fread(&x,sizeof(unsigned int),1,in); } 00186 inline void binwrite(FILE* out, short x) { fwrite(&x,sizeof(short),1,out); } 00187 inline void binread(FILE* in, short& x) { fread(&x,sizeof(short),1,in); } 00188 inline void binwrite(FILE* out, unsigned short x) { fwrite(&x,sizeof(unsigned short),1,out); } 00189 inline void binread(FILE* in, unsigned short& x) { fread(&x,sizeof(unsigned short),1,in); } 00191 inline void binwrite(FILE* out, bool x) { binwrite(out,(unsigned short)x); } 00192 // norman: usigned short to boolean?? At least use a cast 00193 //inline void binread(FILE* in, bool& x) { unsigned short u; binread(in,u); x=u; } 00194 inline void binread(FILE* in, bool& x) { 00195 unsigned short u; binread(in,u); 00196 u == 0 ? x = false : x = true; 00197 } 00198 00199 // The following read/write floats (4 bytes) on disk, regardless of whether the memory-elements are float or double 00200 inline void binwrite(FILE* out, float x) { fwrite(&x,sizeof(float),1,out); } 00201 inline void binread(FILE* in, float& x) { fread(&x,sizeof(float),1,in); } 00202 inline void binwrite(FILE* out, double x) { binwrite(out, float(x)); } 00203 inline void binread(FILE* in, double& x) { float f; binread(in,f); x = double(f); } 00204 00205 // The following read/write doubles (8 bytes) on disk, regardless of whether the memory-elements are float or double 00206 inline void binwrite_double(FILE* out, double x) { fwrite(&x,sizeof(double),1,out); } 00207 inline void binread_double(FILE* in, double& x) { fread(&x,sizeof(double),1,in); } 00208 inline void binwrite_double(FILE* out, float x) { binwrite_double(out, double(x)); } 00209 inline void binread_double(FILE* in, float& x) { double d; binread_double(in,d); x = float(d); } 00210 00211 00213 inline void binwrite(FILE* out, const int* x, int n) { fwrite(x, sizeof(int),n,out); } 00214 inline void binread(FILE* in, int* x, int n) { fread(x, sizeof(int),n,in); } 00215 inline void binwrite(FILE* out, const unsigned int* x, int n) { fwrite(x, sizeof(unsigned int),n,out); } 00216 inline void binread(FILE* in, unsigned int* x, int n) { fread(x, sizeof(unsigned int),n,in); } 00217 inline void binwrite(FILE* out, const short* x, int n) { fwrite(x, sizeof(short),n,out); } 00218 inline void binread(FILE* in, short* x, int n) { fread(x, sizeof(short),n,in); } 00219 inline void binwrite(FILE* out, const unsigned short* x, int n) { fwrite(x, sizeof(unsigned short),n,out); } 00220 inline void binread(FILE* in, unsigned short* x, int n) { fread(x, sizeof(unsigned short),n,in); } 00221 00222 // The followind read/write 4-byte-floats on disk (whether we pass them a float* or a double*) 00223 inline void binwrite(FILE* out, const float *x, int n) { fwrite(x, sizeof(float),n,out); } 00224 inline void binread(FILE* in, float* x, int n) { fread(x, sizeof(float),n,in); } 00225 inline void binwrite(FILE* out, const double* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); } 00226 inline void binread(FILE* in, double* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); } 00227 00228 // The followind read/write 8-byte-doubles on disk (whether we pass them a float* or a double*) 00229 inline void binwrite_double(FILE* out, const double *x, int n) { fwrite(x, sizeof(double),n,out); } 00230 inline void binread_double(FILE* in, double* x, int n) { fread(x, sizeof(double),n,in); } 00231 inline void binwrite_double(FILE* out, const float* x, int n) { for(int i=0; i<n; i++) binwrite(out,x[i]); } 00232 inline void binread_double(FILE* in, float* x, int n) { for(int i=0; i<n; i++) binread(in,x[i]); } 00233 00234 void binwrite_compressed(FILE* out, const double* data, int l); 00235 void binread_compressed(FILE* in, double* data, int l); 00236 void binwrite_compressed(FILE* out, const float* data, int l); 00237 void binread_compressed(FILE* in, float* data, int l); 00238 00240 inline void read_compr_mode_and_size_ptr(char*& in, unsigned char& mode, int& size); 00241 void compress_vec(char* comprbuf, const double* data, int l, bool double_stored_as_float=false); 00242 void uncompress_vec(char* comprbuf, double* data, int l, bool double_stored_as_float=false); 00243 00244 00245 // ********* NEW COMPRESSION FORMAT ********** 00246 // (see its description in .cc) 00247 00252 size_t new_write_compressed(FILE* out, real* vec, int l, double tolerance=1e-6, bool swap_endians=false); 00253 00258 size_t new_read_compressed(FILE* in, real* vec, int l, bool swap_endians=false); 00259 00260 00261 00262 } // end of namespace PLearn 00263 00264 00265 #endif

Generated on Tue Aug 17 16:01:25 2004 for PLearn by doxygen 1.3.7