@anonymous/TidyExtrasmallProlog
C++11

No description

repl talk
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <vector>
#include <tuple>
#include <type_traits>
#include <cstddef>
#include <exception>
#include <string>
#include <algorithm>

/**************************************
 * Defining the output types 
 **************************************/

//Helper for output types with fixed headers
struct no_header {
	static no_header nh;
};
no_header no_header::nh;


struct hmc_iter_stats {
	double energy;
	int treedepth;
	bool divergent;

	typedef no_header header_def;
};

struct constrained_params {
	std::vector<double> values;

	typedef std::vector<std::string> header_def;
};


/**************************************
 * data_stream - ancestor of all streams of data
 **************************************/

//I require all template parameters to be class so that
//a different name -> different type
template <class T> class data_stream {
public:
	typedef T output;
	// Couldn't figure out the syntax with typedef
	using header_def = typename T::header_def;

	virtual void send_headers(const header_def &header) = 0;
	virtual void send(const T &data) = 0;
};

/**************************************
 * Some utility templates to be able define the relay 
 **************************************/

template<class T> struct always_false : std::false_type {};

//Resolves to the first index of What in the paremter pack Where
template <typename What, typename... Where>
struct index_of;

//Case 0: type not found, providing at least a little meaningful error message
template <typename What> 
struct index_of<What> {
  static_assert(always_false<What>::value, "The required type is not part of the type list");
};

//Case 1: Found the type
template <typename What, typename... Where>
struct index_of<What, What, Where...> : std::integral_constant<std::size_t, 0> {};

//Case 2: Recursion
template <typename What, typename Where_head, typename... Where_tail>
struct index_of<What, Where_head, Where_tail...> : std::integral_constant<std::size_t, 1 + index_of<What, Where_tail...>::value> {};


/**************************************
 * The relay and its instances
 **************************************/

template <class... Outputs>
class relay {
  std::tuple<data_stream<Outputs>*...> streams;

  template<class T> data_stream<T>*& stream_ref() {
    std::get<index_of<T,Outputs...>::value>(streams);
  }
  
public:
  template<class T> data_stream<T>* get_stream() {
    stream_ref<T>();
  }

  template<class T> void set_stream(data_stream<T>* stream) {
    if(stream_ref<T>() != 0) {
      throw std::invalid_argument("Stream already set."); 
    }
    stream_ref<T>() = stream;
  }

  template<class T> void send_headers(const typename T::header_def& header) {
    if(stream_ref<T>() != 0) {
      stream_ref<T>()->send_headers(header);
    }
  }

  template<class T> void send(const T& data) {
    if(stream_ref<T>() != 0) {
      stream_ref<T>()->send(data);
    }
  }  
};

typedef relay<hmc_iter_stats, constrained_params> hmc_relay;

/**************************************
 * Some data_stream implementations
 **************************************/

//Helper for multiple types of possible outputs
template<class T>
  class to_string_columns;

template<>
  class to_string_columns<hmc_iter_stats> {
    public:
      static void get_headers(const hmc_iter_stats::header_def&, std::vector<std::string>& out) {
        out.push_back("energy");
        out.push_back("treedepth");
        out.push_back("divergent");
      }

      static void get_values(const hmc_iter_stats& data, std::vector<std::string>& out) {
        out.push_back(std::to_string(data.energy));
        out.push_back(std::to_string(data.treedepth));
        out.push_back(std::to_string(data.divergent));
      }    
    };

template<>
  class to_string_columns<constrained_params> {
    public:
      static void get_headers(const constrained_params::header_def& header, std::vector<std::string>& out) {
        out.insert(out.end(), header.begin(), header.end());
      }

      static void get_values(const constrained_params& data, std::vector<std::string>& out) {
        std::for_each(data.values.begin(), data.values.end(), [&out](double val) {
          out.push_back(std::to_string(val));
        });
      }    
    };


template<class T> 
  class csv_stream : public data_stream<T> {
    private:
      std::ostream& out;
      std::vector<std::string> buffer;
    public:
      csv_stream(std::ostream& out) : out(out), buffer() {}

      virtual void send_headers(const typename T::header_def &header) override {
        buffer.clear();
        to_string_columns<T>::get_headers(header, buffer);
        std::for_each(buffer.begin(), buffer.end(), [this](std::string& s) {
          out << s << ',';
        });
        out << std::endl;
      }

      virtual void send(const T &data) override {
        buffer.clear();
        to_string_columns<T>::get_values(data, buffer);
        std::for_each(buffer.begin(), buffer.end(), [this](std::string& s) {
          out << s << ',';
        });
        out << std::endl;
      }  
  };

int main() {
	hmc_relay r;
  csv_stream<constrained_params> params_stream(std::cout);
  r.set_stream<constrained_params>(&params_stream);

  csv_stream<hmc_iter_stats> stats_stream(std::cout);
  r.set_stream<hmc_iter_stats>(&stats_stream);

  std::vector<std::string> param_names;
  param_names.push_back("alpha");
  param_names.push_back("beta");
  r.send_headers<constrained_params>(param_names);
  r.send_headers<hmc_iter_stats>(no_header::nh);

  std::vector<double> param_values;
  param_values.push_back(0.1);
  param_values.push_back(0.2);
  r.send<constrained_params>({param_values});
  r.send<hmc_iter_stats>({0.5, 1, true});

  /* Uncomment to see error when stream is not defined
  struct undefined_output {
 	  typedef no_header header_def;   
  };

  r.send_headers<undefined_output>(no_header::nh);
  */
}
gcc version 4.6.3