Fix performance bug in format, add paren combinator

This commit is contained in:
Soonho Kong 2013-08-01 18:03:51 -07:00
parent 3c1a65235c
commit e898bb996c
3 changed files with 32 additions and 13 deletions

View file

@ -34,6 +34,10 @@ static void tst1() {
format f10;
f10 += f6 + f5 + f3;
format f11 = above(f1, above(above(f2, f3), f4));
format f12 = paren(format{format("a"),
format("b"),
format("c"),
format("d")});
std::vector<std::pair<std::string, format> > v =
{{"f1", f1},
@ -47,6 +51,7 @@ static void tst1() {
{"f9", f9},
{"f10", f10},
{"f11", f11},
{"f12", f12},
};
std::for_each(v.begin(), v.end(),
@ -56,11 +61,17 @@ static void tst1() {
<< "--------------------" << endl << endl;
});
// std::vector<std::string> ss = {"Last", "weekend's", "revelation", "that", "J.K.", "Rowling", "is", "the", "author", "of", "the", "critically", "acclaimed", "and", "--", "until", "now", "--", "commercially", "unsuccessful", "crime", "novel", "The", "Cuckoo's", "Calling", "has", "electrified", "the", "book", "world", "and", "solidified", "Rowling's", "reputation", "as", "a", "genuine", "writing", "talent:", "After", "all,", "if", "she", "can", "impress", "the", "critics", "without", "the", "benefit", "of", "her", "towering", "reputation,", "then", "surely", "her", "success", "is", "deserved."};
std::vector<std::string> ss = {"Last", "weekend's", "revelation", "that", "J.K.", "Rowling", "is", "the", "author", "of", "the", "critically", "acclaimed"};
std::vector<std::string> ss = {"Last", "weekend's", "revelation", "that", "J.K.", "Rowling", "is", "the", "author", "of", "the", "critically", "acclaimed", "and", "--", "until", "now", "--", "commercially", "unsuccessful", "crime", "novel", "The", "Cuckoo's", "Calling", "has", "electrified", "the", "book", "world", "and", "solidified", "Rowling's", "reputation", "as", "a", "genuine", "writing", "talent:", "After", "all,", "if", "she", "can", "impress", "the", "critics", "without", "the", "benefit", "of", "her", "towering", "reputation,", "then", "surely", "her", "success", "is", "deserved."};
std::vector<format> sl = {f1, f2, f3, f4};
cout << accumulate(++ss.begin(),
ss.end(),
format(*ss.begin()),
[](format const & result, std::string const & s) {
return wrap(result, format(s));
});
std::vector<format> sl = {f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12};
cout << "fill" << endl;
cout << std::string(40, '=') << endl;

View file

@ -115,6 +115,9 @@ format bracket(std::string const l, format const & x, std::string const r) {
line(),
format(r)});
}
format paren(format const & x) {
return bracket("(", x, ")");
}
// wrap = <+/>
// wrap x y = x <> (text " " :<|> line) <> y
@ -127,8 +130,9 @@ bool format::fits(int w, sexpr const & s) {
lean_assert(is_list(s));
if (is_nil(s))
return true;
if (w < 0)
if (w < 0) {
return false;
}
sexpr const & x = car(s);
switch (sexpr_kind(x)) {
@ -139,10 +143,12 @@ bool format::fits(int w, sexpr const & s) {
case format_kind::TEXT:
{
size_t l = sexpr_text_length(x);
if(l - w > 0)
if(w - static_cast<int>(l) < 0) {
return false;
else
return fits(w - l, cdr(s));
}
else {
return fits(w - static_cast<int>(l), cdr(s));
}
}
case format_kind::LINE:
return true;
@ -204,13 +210,14 @@ sexpr format::be(unsigned w, unsigned k, sexpr const & s, sexpr const & r) {
{
sexpr const & x = sexpr_choice_1(v);
sexpr const & y = sexpr_choice_2(v);;
sexpr const & s1 = be(w, k, sexpr(sexpr(i, x), z), r);
if (fits(w - k, s1)) {
return s1;
} else {
sexpr const & s2 = be(w, k, sexpr(sexpr(i, y), z), r);
return s2;
int d = static_cast<int>(w) - static_cast<int>(k);
if(d >= 0) {
sexpr const & s1 = be(w, k, sexpr(sexpr(i, x), z), r);
if (fits(d, s1))
return s1;
}
sexpr const & s2 = be(w, k, sexpr(sexpr(i, y), z), r);
return s2;
}
}
lean_unreachable();

View file

@ -196,6 +196,7 @@ format line();
format group(format const & f);
format above(format const & f1, format const & f2);
format bracket(std::string const l, format const & x, std::string const r);
format paren(format const & x);
format wrap(format const & f1, format const & f2);
template <class InputIterator, typename F>