SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
data_collector_3d.h
Go to the documentation of this file.
1 
5 #ifndef DATA_COLLECTOR_H
6 #define DATA_COLLECTOR_H
7 
8 #include <string>
9 #include <set>
10 #include <map>
11 #include <utility>
12 #include <fstream>
13 #include <iomanip>
14 
15 #include <iostream>
16 
17 
18 enum DataExportFormat
19 {
20  GNUPLOT,
21  PREDICTION_FILE
22 };
23 
37 template <typename Tx, typename Ty, typename Tz>
39 {
40 
41 public:
42  DataCollector3D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY, const std::string& _axisNameZ);
43 
44  void addData(Tx x, Ty y, Tz z);
45  void clear();
46  void writeDataToFile(const std::string& filename = "", DataExportFormat format = GNUPLOT, int tabLength=-1);
47 
48 private:
49  std::string dataSetName;
50  std::pair<std::string, std::pair<std::string, std::string> > axisNames;
51  std::multimap< Tx, std::pair<Ty, Tz> > dataSet;
52 
53  void writeGnuPlotFile(const std::string& filename, int tabLength=-1);
54 
55 };
56 
65 template <typename Tx, typename Ty, typename Tz>
66 DataCollector3D<Tx, Ty, Tz>::DataCollector3D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY,const std::string& _axisNameZ)
67 {
68  dataSetName = _dataSetName;
69  axisNames.first = _axisNameX;
70  axisNames.second = std::make_pair(_axisNameY, _axisNameZ);
71 }
72 
80 template <typename Tx, typename Ty, typename Tz>
82 {
83  dataSet.insert(std::make_pair(x, std::make_pair(y,z)));
84 }
85 
89 template <typename Tx, typename Ty, typename Tz>
91 {
92  dataSet.clear();
93 }
94 
102 template <typename Tx, typename Ty, typename Tz>
103 void DataCollector3D<Tx, Ty, Tz>::writeDataToFile(const std::string& filename, DataExportFormat format, int tabLength)
104 {
105  std::string _filename;
106 
107  if(filename.empty())
108  {
109  _filename = dataSetName + ".dat";
110  }
111  else
112  {
113  _filename = filename;
114  }
115 
116  //Call right exporter
117  if(format == GNUPLOT)
118  {
119  writeGnuPlotFile(_filename, tabLength);
120  }
121 }
122 
129 template <typename Tx, typename Ty, typename Tz>
130 void DataCollector3D<Tx, Ty, Tz>::writeGnuPlotFile(const std::string& filename, int tabLength)
131 {
132  std::ofstream file(filename.c_str());
133 
134  if(tabLength<1)
135  tabLength = axisNames.first.length()+10;
136 
137  file<<std::left;
138  file<<std::fixed <<std::setprecision(5);
139  if(file.is_open())
140  {
141  //First add name and axis names as comments
142  file<<"# " <<dataSetName <<"\n";
143  file<<"# " <<std::setw(tabLength) <<axisNames.first <<std::setw(tabLength) <<axisNames.second.first <<std::setw(tabLength) <<axisNames.second.second <<"\n";
144 
145  const Tx *pLastKey = NULL;
146 
147  //Add data in two columns
148  for(typename std::multimap< Tx, std::pair<Ty, Tz> >::iterator it = dataSet.begin(); it != dataSet.end(); ++it)
149  {
150  if( (pLastKey!=NULL) && (*pLastKey==it->first) )
151  {
152  continue;
153  }
154  pLastKey = &(it->first);
155 
156  typename std::pair<typename std::multimap< Tx, std::pair<Ty, Tz> >::iterator,typename std::multimap< Tx, std::pair<Ty, Tz> >::iterator> range = dataSet.equal_range(*pLastKey);
157 
158  for(typename std::multimap< Tx, std::pair<Ty, Tz> >::iterator i = range.first; i != range.second; ++i)
159  {
160  file<<" " <<std::setw(tabLength) <<(*pLastKey)<<std::setw(tabLength)<<i->second.first<<std::setw(tabLength)<<i->second.second<<"\n";
161 
162  }
163  file<<"\n";
164  }
165 
166  file.close();
167  }
168 }
169 
170 
171 #endif
172 
void writeDataToFile(const std::string &filename="", DataExportFormat format=GNUPLOT, int tabLength=-1)
Definition: data_collector_3d.h:103
void addData(Tx x, Ty y, Tz z)
Definition: data_collector_3d.h:81
void clear()
Definition: data_collector_3d.h:90
DataCollector3D(const std::string &_dataSetName, const std::string &_axisNameX, const std::string &_axisNameY, const std::string &_axisNameZ)
Definition: data_collector_3d.h:66
A class that can be used to collect 2D data.
Definition: data_collector_3d.h:38