From 2af5a445f3905c69c42be5c758c52a2f21b17446 Mon Sep 17 00:00:00 2001 From: graehl Date: Tue, 31 Aug 2010 01:08:42 +0000 Subject: l2r bugfixes git-svn-id: https://ws10smt.googlecode.com/svn/trunk@634 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/writer.h | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 utils/writer.h (limited to 'utils/writer.h') diff --git a/utils/writer.h b/utils/writer.h new file mode 100644 index 00000000..d21b74d6 --- /dev/null +++ b/utils/writer.h @@ -0,0 +1,155 @@ +#ifndef WRITER_H +#define WRITER_H + +#include + +struct Writer +{ + template + std::basic_ostream& + operator()(std::basic_ostream& o,const value_type &l) const { + return o << l; + } +}; + +struct LineWriter +{ + template + std::basic_ostream& + operator()(std::basic_ostream& o,const Label &l) const { + return o << l << std::endl; + } +}; + +template inline +std::ios_base::iostate write_range_iostate(O& o,T begin, T end,W writer,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')',char space=' ') +{ + static const char *const MULTILINE_SEP="\n"; + if (parens) { + o << open_paren; + if (multiline) + o << MULTILINE_SEP; + } + if (multiline) { + for (;begin!=end;++begin) { + o << space; + writer(o,*begin); + o << MULTILINE_SEP; + } + } else { + for (T i=begin;i!=end;++i) { + if (i!=begin) o< +struct range_formatter { + Ib i; + Ie e; + W w; + bool multiline; + bool parens; + range_formatter(Ib i,Ie e,W w=W(),bool multiline=false,bool parens=true) : + i(i),e(e),w(w),multiline(multiline),parens(parens) {} + + template + std::basic_ostream & + operator()(std::basic_ostream &o) const { + write_range_iostate(o,i,e,w,multiline,parens); + return o; + } + + template + friend inline + std::basic_ostream & + operator<<(std::basic_ostream &o,range_formatter const& w) { + return w(o); + } +}; + +template +range_formatter +wrange(Ib i,Ie e,W const& w,bool multiline=false,bool parens=true) +{ + return range_formatter(i,e,w,multiline,parens); +} + +template +range_formatter +prange(Ib i,Ie e,bool multiline=false,bool parens=true) +{ + return range_formatter(i,e,Writer(),multiline,parens); +} + + +template inline +std::basic_ostream & write_range(std::basic_ostream& o,T begin, T end,W writer,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')') +{ + write_range_iostate(o,begin,end,writer,multiline,parens,open_paren,close_paren); + return o; +} + +template +inline std::ios_base::iostate print_range(O& o,T begin,T end,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')') { + return write_range_iostate(o,begin,end,Writer(),multiline,parens,open_paren,close_paren); +} + +template +inline std::ios_base::iostate print_range_i(O& o,C const&c,unsigned from,unsigned to,bool multiline=false,bool parens=true,char open_paren='(',char close_paren=')') { + return write_range_iostate(o,c.begin()+from,c.begin()+to,Writer(),multiline,parens,open_paren,close_paren); +} + + +template +struct bound_printer +{ + O *po; + template + void operator()(T const& t) const + { + *po << t; + } +}; + +template +bound_printer +make_bound_printer(O &o) +{ + bound_printer ret; + ret.po=&o; + return ret; +} + +template +struct bound_writer +{ + W const& w; + bound_writer(W const& w) : w(w) {} + bound_writer(bound_writer const& o) :w(o.w) {} + template + void operator()(O &o,V const& v) const + { + v.print(o,w); + } +}; + + +template +bound_writer +make_bound_writer(W const& w) +{ + return bound_writer(w); +} + + + +#endif -- cgit v1.2.3