00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _FINRESDYN_SAVECOVER_H_
00034 #define _FINRESDYN_SAVECOVER_H_
00035
00036
00037
00038 #include <iostream>
00039 #include <fstream>
00040
00041
00042 #include "streams.h"
00043 #include "covboxes.h"
00044
00045
00046
00047
00048
00049
00050
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 }
00058
00059
00060
00061 inline void double2hex (std::ostream &out, const double &x)
00062 {
00063
00064 double number = x;
00065 const unsigned char *numBytes =
00066 reinterpret_cast<const unsigned char *> (&number);
00067
00068
00069 int testByte = 1;
00070
00071
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
00079 else
00080 {
00081 for (unsigned int i = 0; i < sizeof (double); ++ i)
00082 byte2hex (out, numBytes [i]);
00083 }
00084
00085 return;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 inline void saveCover (const tCoverBoxes<double> &cover,
00095 const char *filename)
00096 {
00097
00098 std::ofstream f (filename);
00099 if (!f)
00100 throw "Cannot create a file to save the cover to.";
00101
00102
00103 int dim = cover. dim ();
00104
00105
00106 double *leftBounds = new double [dim];
00107 double *rightBounds = new double [dim];
00108 for (int n = 0; n < cover. size (); ++ n)
00109 {
00110
00111 cover. get (n, leftBounds, rightBounds);
00112
00113
00114 for (int i = 0; i < dim; ++ i)
00115 {
00116 f << (i ? ") x (" : "(") << leftBounds [i] <<
00117 "," << rightBounds [i];
00118 }
00119 f << ")\n";
00120
00121
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 }
00136
00137
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 }
00144
00145
00146 #endif // _FINRESDYN_SAVECOVER_H_
00147