The Finite Resolution Dynamics Software
savecover.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// @file savecover.h
4 ///
5 /// Saving a cover to a file.
6 /// This file contains the definition of a function that saves a cover
7 /// to a text file.
8 ///
9 /// @author Pawel Pilarczyk
10 ///
11 /////////////////////////////////////////////////////////////////////////////
12 
13 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
14 //
15 // This file is part of my research software package. This is free software;
16 // you can redistribute it and/or modify it under the terms of the GNU
17 // General Public License as published by the Free Software Foundation;
18 // either version 2 of the License, or (at your option) any later version.
19 //
20 // This software is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU General Public License for more details.
24 //
25 // You should have received a copy of the GNU General Public License along
26 // with this software; see the file "license.txt". If not, write to the
27 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 // MA 02111-1307, USA.
29 
30 // Started on March 19, 2009. Last revision: March 19, 2009.
31 
32 
33 #ifndef _FINRESDYN_SAVECOVER_H_
34 #define _FINRESDYN_SAVECOVER_H_
35 
36 
37 // include some standard C++ header files
38 #include <iostream>
39 #include <fstream>
40 
41 // include local header files
42 #include "streams.h"
43 #include "covboxes.h"
44 
45 
46 // --------------------------------------------------
47 // -------------- write double as hex ---------------
48 // --------------------------------------------------
49 
50 /// Writes a given unsigned byte in the hexadecimal way.
51 inline void byte2hex (std::ostream &out, unsigned int byte)
52 {
53  const char symbols [16] = {'0', '1', '2', '3', '4', '5', '6',
54  '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
55  out << symbols [(byte >> 4) & 0x0F] << symbols [byte & 0x0F];
56  return;
57 } /* byte2hex */
58 
59 /// Writes a double precision floating-point number
60 /// in the hexadecimal way as bytes.
61 inline void double2hex (std::ostream &out, const double &x)
62 {
63  // determine the address of the number in the memory
64  double number = x;
65  const unsigned char *numBytes =
66  reinterpret_cast<const unsigned char *> (&number);
67 
68  // prepare a number for testing the endianness
69  int testByte = 1;
70 
71  // in the case of little endian
72  if (*reinterpret_cast<const char *> (&testByte) == 1)
73  {
74  for (int i = sizeof (double) - 1; i >= 0; -- i)
75  byte2hex (out, numBytes [i]);
76  }
77 
78  // in the case of big endian
79  else
80  {
81  for (unsigned int i = 0; i < sizeof (double); ++ i)
82  byte2hex (out, numBytes [i]);
83  }
84 
85  return;
86 } /* double2hex */
87 
88 
89 // --------------------------------------------------
90 // ------------------ save a cover ------------------
91 // --------------------------------------------------
92 
93 /// Saves the given cover to a text file.
94 inline void saveCover (const tCoverBoxes<double> &cover,
95  const char *filename)
96 {
97  // create a file to save the cover to
98  std::ofstream f (filename);
99  if (!f)
100  throw "Cannot create a file to save the cover to.";
101 
102  // determine the dimension of the phase space
103  int dim = cover. dim ();
104 
105  // save the boxes in the cover
106  double *leftBounds = new double [dim];
107  double *rightBounds = new double [dim];
108  for (int n = 0; n < cover. size (); ++ n)
109  {
110  // retrieve the bounds of the box
111  cover. get (n, leftBounds, rightBounds);
112 
113  // write the cartesian product as decimal numbers
114  for (int i = 0; i < dim; ++ i)
115  {
116  f << (i ? ") x (" : "(") << leftBounds [i] <<
117  "," << rightBounds [i];
118  }
119  f << ")\n";
120 
121  // write the cartesian product using hexadecimal code
122  for (int i = 0; i < dim; ++ i)
123  {
124  f << (i ? ") x (" : "(");
125  double2hex (f, leftBounds [i]);
126  f << ",";
127  double2hex (f, rightBounds [i]);
128  }
129  f << ")\n\n";
130  }
131  delete [] rightBounds;
132  delete [] leftBounds;
133 
134  return;
135 } /* saveCover */
136 
137 /// Throws an exception because of an unknown cover is to be saved.
138 template <class UnknownCover>
139 inline void saveCover (const UnknownCover &cover, const char *filename)
140 {
141  throw "Trying to save a cover of unsupported type to a file.";
142  return;
143 } /* saveCover */
144 
145 
146 #endif // _FINRESDYN_SAVECOVER_H_
147 
Various output streams for the program.
void double2hex(std::ostream &out, const double &x)
Writes a double precision floating-point number in the hexadecimal way as bytes.
Definition: savecover.h:61
A cover of a subset in R^n consisting of open boxes.
Definition: covboxes.h:73
A box cover type.
void byte2hex(std::ostream &out, unsigned int byte)
Writes a given unsigned byte in the hexadecimal way.
Definition: savecover.h:51
void saveCover(const tCoverBoxes< double > &cover, const char *filename)
Saves the given cover to a text file.
Definition: savecover.h:94