13 #pragma GCC diagnostic push 14 #pragma GCC diagnostic ignored "-Wshadow" 16 #pragma GCC diagnostic pop 21 #define ENDL <<std::endl; 22 #define ENDLL <<"\n"<<std::endl; 23 #define CERR std::cerr<< 24 #define COUT std::cout<< 25 #define COUTT std::cout<<"\t"<< 26 #define COUTTT std::cout<<"\t\t"<< 39 template<
typename FIRST,
typename... ARGS>
40 void OUTPUTN(std::ostream& o, FIRST f, ARGS... args) {
42 if constexpr(
sizeof...(ARGS) > 0) {
52 template<
typename FIRST,
typename... ARGS>
53 void print(FIRST f, ARGS... args) {
54 std::lock_guard guard(output_lock);
62 template<
typename FIRST,
typename... ARGS>
63 void ERRN(FIRST f, ARGS... args) {
64 std::lock_guard guard(err_lock);
72 template<
typename FIRST,
typename... ARGS>
73 void DEBUG(FIRST f, ARGS... args) {
74 std::lock_guard guard(output_lock);
75 std::stringstream ss; ss << std::this_thread::get_id();
76 OUTPUTN(std::cout,
"DEBUG [thread "+ss.str()+
"]", f, args...);
85 void save(std::string filename, T& hypotheses) {
87 std::ofstream out(filename);
89 for(
auto& v: hypotheses) {
90 out << v.serialize()
ENDL;
102 template<
typename HYP>
103 std::vector<HYP>
load(std::string filename) {
105 std::vector<HYP> out;
107 std::ifstream fs(filename);
110 while(std::getline(fs, line)) {
112 out.push_back(HYP::deserialize(line));
124 std::string
gulp(std::string filename) {
126 std::ifstream fs(filename);
129 assert(
false &&
"*** Error file could not be opened.");
133 while(std::getline(fs, line)) {
134 if(line.length() > 0 and line[0] !=
'#'){
139 ret.erase(ret.size()-1);
156 std::vector<std::array<std::string,N>>
read_csv(
const std::string path,
bool skipheader,
const char delimiter=
'\t') {
158 std::ifstream file(path);
159 if(file.fail()){
print(
"*** File does not exist", path); assert(
false); }
162 std::vector<std::array<std::string,N>> out;
163 while( std::getline(file,s) ) {
164 if(s.size() == 0 or s[0] ==
'#')
continue;
171 out.push_back(split<N>(s,delimiter));
177 std::vector<std::vector<std::string>>
read_csv(
const std::string path,
bool skipheader,
const char delimiter=
'\t') {
179 std::ifstream file(path);
180 if(file.fail()){
print(
"*** File does not exist", path); assert(
false); }
183 std::vector<std::vector<std::string>> out;
184 while( std::getline(file,s) ) {
185 if(s.size() == 0 or s[0] ==
'#')
continue;
192 auto q =
split(s,delimiter);
193 std::vector<std::string> v;
195 v.push_back(q.front()); q.pop_front();
Definition: OrderedLock.h:16
std::vector< std::array< std::string, N > > read_csv(const std::string path, bool skipheader, const char delimiter='\t')
Load data – divides in columns at the delimiter. NOTE: we are required to say whether we skip a head...
Definition: IO.h:156
OrderedLock output_lock
Definition: IO.h:31
void ERRN(FIRST f, ARGS... args)
Lock err_lock and print to std:cerr.
Definition: IO.h:63
void save(std::string filename, T &hypotheses)
Simple saving of a vector of hypotheses.
Definition: IO.h:85
std::deque< std::string > split(const std::string &s, const char delimiter)
Split is returns a deque of s split up at the character delimiter. It handles these special cases: sp...
Definition: str.h:50
std::vector< HYP > load(std::string filename)
Simple loading for a vector of hypotheses.
Definition: IO.h:103
void OUTPUTN(std::ostream &o, FIRST f, ARGS... args)
This assumes o has been appropriately locked.
Definition: IO.h:40
void print(FIRST f, ARGS... args)
Lock output_lock and print to std:cout.
Definition: IO.h:53
A FIFO mutex (from stackoverflow) https://stackoverflow.com/questions/14792016/creating-a-lock-that-p...
#define ENDL
Definition: IO.h:21
void DEBUG(FIRST f, ARGS... args)
Print to std:ccout with debugging info.
Definition: IO.h:73
std::string gulp(std::string filename)
Read this entire file as a string Skipping comment # lines.
Definition: IO.h:124
const std::string OUTPUT_SEP
Definition: IO.h:28
OrderedLock err_lock
Definition: IO.h:32