跳到正文

科技·工程

Testlib 用于 generator 的函数整理

y_kx_b约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年7月1日
收录
2026年3月25日
更新
2026年11月13日
篇幅
约 8 分钟

版本

  1. v1当前

pattern:

C++
/*
 * Very simple regex-like pattern.
 * It used for two purposes: validation and generation.
 *
 * For example, pattern("[a-z]{1,5}").next(rnd) will return
 * random string from lowercase latin letters with length
 * from 1 to 5. It is easier to call rnd.next("[a-z]{1,5}")
 * for the same effect.
 *
 * Another samples:
 * "mike|john" will generate (match) "mike" or "john";
 * "-?[1-9][0-9]{0,3}" will generate (match) non-zero integers from -9999 to 9999;
 * "id-([ac]|b{2})" will generate (match) "id-a", "id-bb", "id-c";
 * "[^0-9]*" will match sequences (empty or non-empty) without digits, you can't
 * use it for generations.
 *
 * You can't use pattern for generation if it contains meta-symbol '*'. Also it
 * is not recommended to use it for char-sets with meta-symbol '^' like [^a-z].
 *
 * For matching very simple greedy algorithm is used. For example, pattern
 * "[0-9]?1" will not match "1", because of greedy nature of matching.
 * Alternations (meta-symbols "|") are processed with brute-force algorithm, so
 * do not use many alternations in one expression.
 *
 * If you want to use one expression many times it is better to compile it into
 * a single pattern like "pattern p("[a-z]+")". Later you can use
 * "p.matches(std::string s)" or "p.next(random_t& rd)" to check matching or generate
 * new string by pattern.
 *
 * Simpler way to read token and check it for pattern matching is "inf.readToken("[a-z]+")".
 *
 * All spaces are ignored in regex, unless escaped with \. For example, ouf.readLine("NO SOLUTION")
 * will expect "NOSOLUTION", the correct call should be ouf.readLine("NO\\ SOLUTION") or
 * ouf.readLine(R"(NO\ SOLUTION)") if you prefer raw string literals from C++11.
 */

  • std::string next(std::string ptrn) / std::string next(const char *format, ...): Random string value by given pattern (see pattern documentation).
  • int next(int n): Random value in range [0, n-1].(int 外的整数型使用方法相同,下同。)
  • int next(int from, int to): Returns random value in range [from,to].
  • double next(): Random double value in range [0, 1).
  • double next(double n): Random double value in range [0, n).
  • double next(double from, double to): Random double value in range [from, to).
  • template<typename Container> typename Container::value_type any(const Container &c): Returns random element from container.
  • template<typename Iter> typename Iter::value_type any(const Iter &begin, const Iter &end): Returns random element from iterator range.
  • int wnext(int n, int type): 见 OI Wiki - Generator。其他的 wnext 就是在 next 最后加了一个参数(参数类型始终是 int)。
  • template<typename Container> typename Container::value_type wany(const Container &c, int type): Returns weighted random element from container.
    • 源码:typename Container::const_iterator it = c.begin(); std::advance(it, wnext(size, type)); return *it;(其实就是对 index 用 wnext。)
  • template<typename Iter> typename Iter::value_type wany(const Iter &begin, const Iter &end, int type): Returns weighted random element from iterator range.
  • template<typename T, typename E> std::vector<E> perm(T size, E first): Returns random permutation of the given size (values are between first and first+size-1)
  • template<typename T> std::vector<T> distinct(int size, T from, T to): Returns size unordered (unsorted) distinct numbers between from and to.
  • template<typename T> std::vector<T> partition(int size, T sum, T min_part): Returns random (unsorted) partition which is a representation of sum as a sum of integers not less than min_part.
  • 新特性:解析命令行参数(opt<T>):见 OI Wiki - Generator。
    • Added opt defaults via opt<T>(key/index, default_val);
    • check unused opts when using has_opt or default opt (turn off this check with suppressEnsureNoUnusedOpt()). (事实上还可以空访问,比如孤零零写个 opt<int>("rand");())
    • opt<bool>("some_missing_key") returns false now
    • has_opt(key)
    • Opts supported: use them like n = opt<int>("n"), in a command line you can use an exponential notation(比如 -n=1e5,但是不能识别 -n=1e5-1)
原文