savecover.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// @file savecover.h
00004 ///
00005 /// Saving a cover to a file.
00006 /// This file contains the definition of a function that saves a cover
00007 /// to a text file.
00008 ///
00009 /// @author Pawel Pilarczyk
00010 ///
00011 /////////////////////////////////////////////////////////////////////////////
00012 
00013 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00014 //
00015 // This file is part of my research software package.  This is free software;
00016 // you can redistribute it and/or modify it under the terms of the GNU
00017 // General Public License as published by the Free Software Foundation;
00018 // either version 2 of the License, or (at your option) any later version.
00019 //
00020 // This software is distributed in the hope that it will be useful,
00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 // GNU General Public License for more details.
00024 //
00025 // You should have received a copy of the GNU General Public License along
00026 // with this software; see the file "license.txt".  If not, write to the
00027 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00028 // MA 02111-1307, USA.
00029 
00030 // Started on March 19, 2009. Last revision: March 19, 2009.
00031 
00032 
00033 #ifndef _FINRESDYN_SAVECOVER_H_
00034 #define _FINRESDYN_SAVECOVER_H_
00035 
00036 
00037 // include some standard C++ header files
00038 #include <iostream>
00039 #include <fstream>
00040 
00041 // include local header files
00042 #include "streams.h"
00043 #include "covboxes.h"
00044 
00045 
00046 // --------------------------------------------------
00047 // -------------- write double as hex ---------------
00048 // --------------------------------------------------
00049 
00050 /// Writes a given unsigned byte in the hexadecimal way.
00051 inline void byte2hex (std::ostream &out, unsigned int byte)
00052 {
00053         const char symbols [16] = {'0', '1', '2', '3', '4', '5', '6',
00054                 '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
00055         out << symbols [(byte >> 4) & 0x0F] << symbols [byte & 0x0F];
00056         return;
00057 } /* byte2hex */
00058 
00059 /// Writes a double precision floating-point number
00060 /// in the hexadecimal way as bytes.
00061 inline void double2hex (std::ostream &out, const double &x)
00062 {
00063         // determine the address of the number in the memory
00064         double number = x;
00065         const unsigned char *numBytes =
00066                 reinterpret_cast<const unsigned char *> (&number);
00067 
00068         // prepare a number for testing the endianness
00069         int testByte = 1;
00070 
00071         // in the case of little endian
00072         if (*reinterpret_cast<const char *> (&testByte) == 1)
00073         {
00074                 for (int i = sizeof (double) - 1; i >= 0; -- i)
00075                         byte2hex (out, numBytes [i]);
00076         }
00077 
00078         // in the case of big endian
00079         else
00080         {
00081                 for (unsigned int i = 0; i < sizeof (double); ++ i)
00082                         byte2hex (out, numBytes [i]);
00083         }
00084 
00085         return;
00086 } /* double2hex */
00087 
00088 
00089 // --------------------------------------------------
00090 // ------------------ save a cover ------------------
00091 // --------------------------------------------------
00092 
00093 /// Saves the given cover to a text file.
00094 inline void saveCover (const tCoverBoxes<double> &cover,
00095         const char *filename)
00096 {
00097         // create a file to save the cover to
00098         std::ofstream f (filename);
00099         if (!f)
00100                 throw "Cannot create a file to save the cover to.";
00101 
00102         // determine the dimension of the phase space
00103         int dim = cover. dim ();
00104 
00105         // save the boxes in the cover
00106         double *leftBounds = new double [dim];
00107         double *rightBounds = new double [dim];
00108         for (int n = 0; n < cover. size (); ++ n)
00109         {
00110                 // retrieve the bounds of the box
00111                 cover. get (n, leftBounds, rightBounds);
00112 
00113                 // write the cartesian product as decimal numbers
00114                 for (int i = 0; i < dim; ++ i)
00115                 {
00116                         f << (i ? ") x (" : "(") << leftBounds [i] <<
00117                                 "," << rightBounds [i];
00118                 }
00119                 f << ")\n";
00120 
00121                 // write the cartesian product using hexadecimal code
00122                 for (int i = 0; i < dim; ++ i)
00123                 {
00124                         f << (i ? ") x (" : "(");
00125                         double2hex (f, leftBounds [i]);
00126                         f << ",";
00127                         double2hex (f, rightBounds [i]);
00128                 }
00129                 f << ")\n\n";
00130         }
00131         delete [] rightBounds;
00132         delete [] leftBounds;
00133 
00134         return;
00135 } /* saveCover */
00136 
00137 /// Throws an exception because of an unknown cover is to be saved.
00138 template <class UnknownCover>
00139 inline void saveCover (const UnknownCover &cover, const char *filename)
00140 {
00141         throw "Trying to save a cover of unsupported type to a file.";
00142         return;
00143 } /* saveCover */
00144 
00145 
00146 #endif // _FINRESDYN_SAVECOVER_H_
00147 

Generated on Mon Apr 12 15:09:57 2010 for The Finite Resolution Dynamics Software by  doxygen 1.5.3