New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stan refactor #2004
Stan refactor #2004
Changes from 131 commits
630a7ec
8761a55
a241c7a
c226c91
d78a6e0
b993b41
24b1902
bd90426
74f7fb6
9091931
5f356be
86e7c40
cfe30cd
2bf8e14
4e5ad0c
5a27f99
531160b
408d718
7f7c695
bb68a7e
2058c4d
700d4df
56549bf
bfb59f5
908a014
ffb0cf2
29f6c4c
d380706
2d56c41
05c63db
ccf8aa9
e205551
387e8f7
419ab42
57c5c93
16d9a72
5bb15ca
524986d
ec144ac
89aadd6
00ef3bc
d0e0b82
f589d77
ebee819
462b6cf
d052b49
feda4a2
bece850
dc2f43e
8d28dad
3fc35ad
f614952
4fc003f
c3dba30
26d4865
ea9bf6c
737520b
d8a61fc
7f4a13a
6349c41
13c25e4
c6121a0
21b7bc4
fe25f74
e089a40
83acaae
f9588b3
a307555
c41cd0b
74303af
d4e7a06
6ddb1bc
777dfb7
6a77da2
e23aa24
72df03d
ddb4fb7
c07c74d
c255ba7
0456dfe
e7b3ea9
5f60d20
ec18460
79daf7b
3bfa61d
3df59d6
7ba89de
cd13dc4
a5b7513
8a4e997
e006b74
2100505
ce6267f
4dfb24c
ae22860
cdbf25c
03207db
c7523c5
8c3343d
56964cb
0441cca
cce23ca
4fee83c
a1d9711
ab9c34b
5c49027
a2de420
8f3718a
4ad038f
f15d78f
0327d32
1319011
1102cd2
8bc4520
c536c68
fa75ca1
0f9ce3e
cf6218d
800856e
2ac6f69
f05f8a6
2281219
e63e674
354bd0f
af7092b
5fd895f
fe43b92
5f87f9d
23a9458
2a3c0aa
ffa5aac
48eefc6
54833b6
c5e43b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef STAN_CALLBACKS_INTERRUPT_HPP | ||
#define STAN_CALLBACKS_INTERRUPT_HPP | ||
|
||
namespace stan { | ||
namespace callbacks { | ||
|
||
/** | ||
* <code>interrupt</code> is a base class defining the interface | ||
* for Stan interrupt callbacks. | ||
* | ||
* The interrupt is called from within Stan algorithms to allow | ||
* for the interfaces to handle interrupt signals (ctrl-c). | ||
*/ | ||
class interrupt { | ||
public: | ||
/** | ||
* Callback function. | ||
* | ||
* This function is called by the algorithms allowing the interfaces | ||
* to break when necessary. | ||
*/ | ||
virtual void operator()() { | ||
} | ||
|
||
/** | ||
* Virtual destructor. | ||
*/ | ||
virtual ~interrupt() {} | ||
}; | ||
|
||
} | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#ifndef STAN_CALLBACKS_STREAM_WRITER_HPP | ||
#define STAN_CALLBACKS_STREAM_WRITER_HPP | ||
|
||
#include <stan/callbacks/writer.hpp> | ||
#include <ostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace stan { | ||
namespace callbacks { | ||
|
||
/** | ||
* <code>stream_writer</code> is an implementation | ||
* of <code>writer</code> that writes to a stream. | ||
*/ | ||
class stream_writer : public writer { | ||
public: | ||
/** | ||
* Constructs a stream writer with an output stream | ||
* and an optional prefix for comments. | ||
* | ||
* @param[in, out] output stream to write | ||
* @param[in] comment_prefix string to stream before | ||
* each comment line. Default is "". | ||
*/ | ||
stream_writer(std::ostream& output, | ||
const std::string& comment_prefix = ""): | ||
output_(output), comment_prefix_(comment_prefix) {} | ||
|
||
/** | ||
* Writes a set of names on a single line in csv format followed | ||
* by a newline. | ||
* | ||
* Note: the names are not escaped. | ||
* | ||
* @param[in] names Names in a std::vector | ||
*/ | ||
void operator()(const std::vector<std::string>& names) { | ||
if (names.empty()) return; | ||
|
||
std::vector<std::string>::const_iterator last = names.end(); | ||
--last; | ||
|
||
for (std::vector<std::string>::const_iterator it = names.begin(); | ||
it != last; ++it) | ||
output_ << *it << ","; | ||
output_ << names.back() << std::endl; | ||
} | ||
|
||
/** | ||
* Writes a set of values in csv format followed by a newline. | ||
* | ||
* Note: the precision of the output is determined by the settings | ||
* of the stream on construction. | ||
* | ||
* @param[in] state Values in a std::vector | ||
*/ | ||
void operator()(const std::vector<double>& state) { | ||
if (state.empty()) return; | ||
|
||
std::vector<double>::const_iterator last = state.end(); | ||
--last; | ||
|
||
for (std::vector<double>::const_iterator it = state.begin(); | ||
it != last; ++it) | ||
output_ << *it << ","; | ||
output_ << state.back() << std::endl; | ||
} | ||
|
||
/** | ||
* Writes the comment_prefix to the stream followed by a newline. | ||
*/ | ||
void operator()() { | ||
output_ << comment_prefix_ << std::endl; | ||
} | ||
|
||
/** | ||
* Writes the comment_prefix then the message followed by a newline. | ||
* | ||
* @param[in] message A string | ||
*/ | ||
void operator()(const std::string& message) { | ||
output_ << comment_prefix_ << message << std::endl; | ||
} | ||
|
||
/** | ||
* Virtual destructor | ||
*/ | ||
virtual ~stream_writer() {} | ||
|
||
private: | ||
/** | ||
* Output stream | ||
*/ | ||
std::ostream& output_; | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space between declarations |
||
* Comment prefix to use when printing comments: strings and blank lines | ||
*/ | ||
std::string comment_prefix_; | ||
}; | ||
|
||
} | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#ifndef STAN_CALLBACKS_TEE_WRITER_HPP | ||
#define STAN_CALLBACKS_TEE_WRITER_HPP | ||
|
||
#include <stan/callbacks/writer.hpp> | ||
#include <ostream> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace stan { | ||
namespace callbacks { | ||
|
||
/** | ||
* <code>tee_writer</code> is an implementation that writes to | ||
* two writers. | ||
* | ||
* For any call to this writer, it will tee the call to both writers | ||
* provided in the constructor. | ||
*/ | ||
class tee_writer : public writer { | ||
public: | ||
/** | ||
* Constructor accepting two writers. | ||
* | ||
* @param[in, out] writer1 first writer | ||
* @param[in, out] writer2 second writer | ||
*/ | ||
tee_writer(writer& writer1, | ||
writer& writer2) | ||
: writer1_(writer1), writer2_(writer2) { | ||
} | ||
|
||
void operator()(const std::vector<std::string>& names) { | ||
writer1_(names); | ||
writer2_(names); | ||
} | ||
|
||
void operator()(const std::vector<double>& state) { | ||
writer1_(state); | ||
writer2_(state); | ||
} | ||
|
||
void operator()() { | ||
writer1_(); | ||
writer2_(); | ||
} | ||
|
||
void operator()(const std::string& message) { | ||
writer1_(message); | ||
writer2_(message); | ||
} | ||
|
||
virtual ~tee_writer() {} | ||
|
||
private: | ||
/** | ||
* The first writer | ||
*/ | ||
writer& writer1_; | ||
/** | ||
* The second writer | ||
*/ | ||
writer& writer2_; | ||
}; | ||
|
||
} | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef STAN_CALLBACKS_WRITER_HPP | ||
#define STAN_CALLBACKS_WRITER_HPP | ||
|
||
#include <boost/lexical_cast.hpp> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace stan { | ||
namespace callbacks { | ||
|
||
/** | ||
* <code>writer</code> is a base class defining the interface | ||
* for Stan writer callbacks. The base class can be used as a | ||
* no-op implementation. | ||
*/ | ||
class writer { | ||
public: | ||
/** | ||
* Writes a set of names. | ||
* | ||
* @param[in] names Names in a std::vector | ||
*/ | ||
virtual void operator()(const std::vector<std::string>& names) { | ||
} | ||
|
||
/** | ||
* Writes a set of values. | ||
* | ||
* @param[in] state Values in a std::vector | ||
*/ | ||
virtual void operator()(const std::vector<double>& state) { | ||
} | ||
|
||
/** | ||
* Writes blank input. | ||
*/ | ||
virtual void operator()() { | ||
} | ||
|
||
/** | ||
* Writes a string. | ||
* | ||
* @param[in] message A string | ||
*/ | ||
virtual void operator()(const std::string& message) { | ||
} | ||
|
||
/** | ||
* Virtual destructor. | ||
*/ | ||
virtual ~writer() {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move dtor up before other methods |
||
}; | ||
|
||
} | ||
} | ||
#endif |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is 100% code dup with previous---I would ratha single templated impl called twice