From 7a922cf6fc521b9826e83bcea315ea808c9c7d5b Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Tue, 21 Mar 2017 23:45:03 +0300 Subject: [PATCH] Fix patterns of kind [aab]; rnd.next(char*, ...) --- common.h | 4 ++-- jngen.h | 22 ++++++++++++++-------- pattern.h | 12 +++++++----- random.h | 6 +++++- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/common.h b/common.h index cedb472..dc98c11 100644 --- a/common.h +++ b/common.h @@ -24,7 +24,7 @@ while (false) namespace jngen { template -std::string format(const std::string& format, Args... args) { +std::string format(const std::string& fmt, Args... args) { constexpr static char BUF_SIZE = 64; static char BUFFER[BUF_SIZE]; @@ -32,7 +32,7 @@ std::string format(const std::string& format, Args... args) { char *buf = BUFFER; while (true) { - int ret = snprintf(buf, bufSize, format.c_str(), args...); + int ret = snprintf(buf, bufSize, fmt.c_str(), args...); if (ret < bufSize) { break; } diff --git a/jngen.h b/jngen.h index 78acc38..21fb0b6 100644 --- a/jngen.h +++ b/jngen.h @@ -23,7 +23,7 @@ while (false) namespace jngen { template -std::string format(const std::string& format, Args... args) { +std::string format(const std::string& fmt, Args... args) { constexpr static char BUF_SIZE = 64; static char BUFFER[BUF_SIZE]; @@ -31,7 +31,7 @@ std::string format(const std::string& format, Args... args) { char *buf = BUFFER; while (true) { - int ret = snprintf(buf, bufSize, format.c_str(), args...); + int ret = snprintf(buf, bufSize, fmt.c_str(), args...); if (ret < bufSize) { break; } @@ -165,6 +165,7 @@ void getopts(int argc, char *argv[], Args& ...args) { using jngen::getopts; +#include #include #include #include @@ -298,7 +299,7 @@ class Parser { } std::vector parseBlock() { - std::set allowed; + std::vector allowed; char last = -1; bool inRange = false; while (control(peek()) != ']') { @@ -311,13 +312,13 @@ class Parser { } else if (inRange) { ensure(c >= last); for (char i = last; i <= c; ++i) { - allowed.insert(i); + allowed.push_back(i); } inRange = false; last = -1; } else { if (last != -1) { - allowed.insert(last); + allowed.push_back(last); } last = c; } @@ -327,10 +328,11 @@ class Parser { ensure(!inRange); if (last != -1) { - allowed.insert(last); + allowed.push_back(last); } - return std::vector(allowed.begin(), allowed.end()); + std::sort(allowed.begin(), allowed.end()); + return allowed; } Pattern parsePattern() { @@ -546,6 +548,11 @@ class Random { return Pattern(pattern).next([this](int n) { return next(n); }); } + template + std::string next(const std::string& pattern, Args... args) { + return next(format(pattern, args...)); + } + template T tnext(Args... args) { return TypedRandom{*this}.next(args...); @@ -653,7 +660,6 @@ struct TypedRandom : public BaseTypedRandom { T next(Args... args) { return random.next(args...); } }; - struct OrderedPairTag {} opair; template<> diff --git a/pattern.h b/pattern.h index b19ac43..23b69c2 100644 --- a/pattern.h +++ b/pattern.h @@ -2,6 +2,7 @@ #include "common.h" +#include #include #include #include @@ -135,7 +136,7 @@ class Parser { } std::vector parseBlock() { - std::set allowed; + std::vector allowed; char last = -1; bool inRange = false; while (control(peek()) != ']') { @@ -148,13 +149,13 @@ class Parser { } else if (inRange) { ensure(c >= last); for (char i = last; i <= c; ++i) { - allowed.insert(i); + allowed.push_back(i); } inRange = false; last = -1; } else { if (last != -1) { - allowed.insert(last); + allowed.push_back(last); } last = c; } @@ -164,10 +165,11 @@ class Parser { ensure(!inRange); if (last != -1) { - allowed.insert(last); + allowed.push_back(last); } - return std::vector(allowed.begin(), allowed.end()); + std::sort(allowed.begin(), allowed.end()); + return allowed; } Pattern parsePattern() { diff --git a/random.h b/random.h index 390664c..b2f72d8 100644 --- a/random.h +++ b/random.h @@ -134,6 +134,11 @@ class Random { return Pattern(pattern).next([this](int n) { return next(n); }); } + template + std::string next(const std::string& pattern, Args... args) { + return next(format(pattern, args...)); + } + template T tnext(Args... args) { return TypedRandom{*this}.next(args...); @@ -241,7 +246,6 @@ struct TypedRandom : public BaseTypedRandom { T next(Args... args) { return random.next(args...); } }; - struct OrderedPairTag {} opair; template<>