PLearn 0.1
general.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-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037  
00038 
00039 /* *******************************************************      
00040  * $Id: general.h 8247 2007-11-12 20:22:12Z nouiz $
00041  * AUTHORS: Pascal Vincent & Yoshua Bengio
00042  * This file is part of the PLearn library.
00043  ******************************************************* */
00044 
00045 
00048 #ifndef GENERAL_INC
00049 #define GENERAL_INC
00050 
00051 #if !(!defined(_MSC_VER) && !defined(_MINGW_))
00052 #include <io.h>
00053 #include <float.h>
00054 #else
00055 #include <unistd.h>
00056 #endif
00057 
00058 //#include <cstdlib>
00059 //#include <cstdio>
00060 //#include <cmath>
00061 //#include <cstring>
00062 //#include <cstdarg>
00063 //#include <cstddef>
00064 //#include <cfloat>
00065 //#include <cctype>
00066 //#include <iostream>
00067 //#include <fstream>
00068 //#include <strstream>
00069 #include <iomanip>
00070 //#include <cerrno>
00071 //#include <sys/types.h>
00072 //#include <sys/stat.h>
00073 //#include <fcntl.h>
00074 //#include <string>
00075 //#include <vector>
00076 #include <map>
00077 //#include <set>
00078 //#include <algorithm>
00079 //#include <functional>
00080 
00081 #ifdef SPARC
00082 #include <ieeefp.h>
00083 #endif
00084 
00085 //#include "plerror.h"
00086 #include <plearn/math/pl_math.h>
00087 //#include "pl_io.h"
00088 #include <plearn/io/pl_io_deprecated.h>
00089 //#include "plstreams.h"
00090 //#include "stringutils.h"
00091 //#include "pl_hash_fun.h"
00092 //#include "TypeTraits.h"
00093 //#include "CopiesMap.h"
00094 
00095 // Include directive to be removed ultimately
00096 #include "ProgressBar.h"
00097 
00098 
00101 #define DATAFILE_HEADERLENGTH 64
00102 
00103 
00104 namespace std {
00105   
00107 
00108 inline float* copy(float* first, float* last, float* dest)
00109 { size_t n = last-first; memcpy(dest, first, n*sizeof(float)); return dest+n; }
00110 
00111 inline double* copy(double* first, double* last, double* dest)
00112 { size_t n = last-first; memcpy(dest, first, n*sizeof(double)); return dest+n; }
00113 
00114 inline bool* copy(bool* first, bool* last, bool* dest)
00115 { size_t n = last-first; memcpy(dest, first, n*sizeof(bool)); return dest+n; }
00116 
00117 inline char* copy(char* first, char* last, char* dest)
00118 { size_t n = last-first; memcpy(dest, first, n*sizeof(char)); return dest+n; }
00119 
00120 inline unsigned char* copy(unsigned char* first, unsigned char* last, unsigned char* dest)
00121 { size_t n = last-first; memcpy(dest, first, n*sizeof(unsigned char)); return dest+n; }
00122 
00123 inline short* copy(short* first, short* last, short* dest)
00124 { size_t n = last-first; memcpy(dest, first, n*sizeof(short)); return dest+n; }
00125 
00126 inline unsigned short* copy(unsigned short* first, unsigned short* last, unsigned short* dest)
00127 { size_t n = last-first; memcpy(dest, first, n*sizeof(unsigned short)); return dest+n; }
00128 
00129 inline int* copy(int* first, int* last, int* dest)
00130 { size_t n = last-first; memcpy(dest, first, n*sizeof(int)); return dest+n; }
00131 
00132 inline unsigned int* copy(unsigned int* first, unsigned int* last, unsigned int* dest)
00133 { size_t n = last-first; memcpy(dest, first, n*sizeof(unsigned int)); return dest+n; }
00134 
00135 inline long* copy(long* first, long* last, long* dest)
00136 { size_t n = last-first; memcpy(dest, first, n*sizeof(long)); return dest+n; }
00137 
00138 inline unsigned long* copy(unsigned long* first, unsigned long* last, unsigned long* dest)
00139 { size_t n = last-first; memcpy(dest, first, n*sizeof(unsigned long)); return dest+n; }
00140 
00141 } // end of namespace std
00142 
00143 
00144 namespace PLearn {
00145 using namespace std;
00146 
00147 #if (defined(_MINGW_) || defined(WIN32)) && !defined(__CYGWIN__)
00148 #define sleep(t) _sleep(t*1000)
00149 #endif
00150 
00151 using std::min;
00152 using std::max;
00153 
00155 template<class In, class Out>
00156 inline Out copy_cast(In first, In last, Out res)
00157 {
00158     typedef typename iterator_traits<Out>::value_type out_t;
00159     for(; first!=last; ++first, ++res)
00160         *res = out_t(*first);
00161     return res;
00162 }
00163 
00164 
00167 
00170 template<class T>
00171 inline void clear_1(T& x)
00172 { x = T(); }
00173 
00174 inline void clear_1(char& x) { x = 0; }
00175 inline void clear_1(unsigned char& x) { x = 0; }
00176 inline void clear_1(signed char& x) { x = 0; }
00177 inline void clear_1(short& x) { x = 0; }
00178 inline void clear_1(unsigned short& x) { x = 0; }
00179 inline void clear_1(int& x) { x = 0; }
00180 inline void clear_1(unsigned int& x) { x = 0; }
00181 inline void clear_1(long& x) { x = 0; }
00182 inline void clear_1(unsigned long& x) { x = 0; }
00183 inline void clear_1(float& x) { x = 0; }
00184 inline void clear_1(double& x) { x = 0; }
00185 inline void clear_1(bool& x) { x = false; }
00186 
00188 template<class For>
00189 inline void clear_n(For begin, int n)
00190 {
00191     while(n--)
00192     {
00193         clear_1(*begin);
00194         ++begin;
00195     }
00196 }
00197 
00199 inline void clear_n(float* begin, int n)
00200 { memset(begin,0,n*sizeof(float)); }
00201 
00202 inline void clear_n(double* begin, int n)
00203 { memset(begin,0,n*sizeof(double)); }
00204 
00205 inline void clear_n(bool* begin, int n)
00206 { memset(begin,0,n*sizeof(bool)); }
00207 
00208 inline void clear_n(char* begin, int n)
00209 { memset(begin,0,n*sizeof(char)); }
00210 
00211 inline void clear_n(unsigned char* begin, int n)
00212 { memset(begin,0,n*sizeof(unsigned char)); }
00213 
00214 inline void clear_n(short* begin, int n)
00215 { memset(begin,0,n*sizeof(short)); }
00216 
00217 inline void clear_n(unsigned short* begin, int n)
00218 { memset(begin,0,n*sizeof(unsigned short)); }
00219 
00220 inline void clear_n(int* begin, int n)
00221 { memset(begin,0,n*sizeof(int)); }
00222 
00223 inline void clear_n(unsigned int* begin, int n)
00224 { memset(begin,0,n*sizeof(unsigned int)); }
00225 
00226 inline void clear_n(long* begin, int n)
00227 { memset(begin,0,n*sizeof(long)); }  
00228 
00229 inline void clear_n(unsigned long* begin, int n)
00230 { memset(begin,0,n*sizeof(unsigned long)); }  
00231 
00232 typedef int (*compare_function)(const void *, const void *);
00233 
00236 template<class T>
00237 inline void pl_swap(T& a, T& b)
00238 { 
00239     T tmp; 
00240     tmp = a;
00241     a = b;
00242     b = tmp;
00243 }
00244 
00246 char* strcopy(char* s);
00247 
00249 void pretty_print_number(char* buffer, real number);
00250 
00252 bool isMapKeysAreInt(map<real,int>& m);
00253 
00254 // return the name of host, e.g. with getenv("HOSTNAME") or getenv("HOST")
00255 string hostname();
00256 
00257 // static object used to perform PLearn unconditionnal initializations
00258 class PLearnInit
00259 {
00260 public:
00261     PLearnInit();
00262     ~PLearnInit();
00263 };
00264 
00265 //static PLearnInit _plearn_init_;
00266 
00267 // set or get (if argument is "") the program name, as given by argv[0] of main
00268 string prgname(const string& setname = "");
00269 
00270 inline int sizeInBytes(int x) { return sizeof(int); }
00271 inline int sizeInBytes(float x) { return sizeof(float); }
00272 inline int sizeInBytes(double x) { return sizeof(double); }
00273 inline int sizeInBytes(long x) { return sizeof(long); }
00274 inline int sizeInBytes(char x) { return sizeof(char); }
00275 inline int sizeInBytes(string x) { return int(x.length()); }
00276 template <class T>
00277 inline int sizeInBytes(T* x) { 
00278     int n = sizeof(T*); 
00279     if (x) 
00280         n+= sizeInBytes(*x);  
00281     return n;
00282 }
00283 template <class T1, class T2>
00284 inline int sizeInBytes(pair<T1,T2> x) { return sizeInBytes(x.first)+sizeInBytes(x.second); }
00285 
00286 
00287 } // end of namespace PLearn
00288 
00289 #endif
00290 
00291 
00292 /*
00293   Local Variables:
00294   mode:c++
00295   c-basic-offset:4
00296   c-file-style:"stroustrup"
00297   c-file-offsets:((innamespace . 0)(inline-open . 0))
00298   indent-tabs-mode:nil
00299   fill-column:79
00300   End:
00301 */
00302 // 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