競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// vector_func.hpp の演算子と関数を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#include "../../../src/utility/vector_func.hpp"
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
bool ok = true;
void check(bool cond, const char* what) {
if (!cond) {
if (ok) std::cerr << "failed: " << what << std::endl;
ok = false;
}
}
int main() {
std::mt19937_64 rng(314);
auto rand_vec = [&](int n) {
std::vector<long long> v(n);
for (auto& x : v) x = static_cast<long long>(rng() % 21) - 10;
return v;
};
for (int rep = 0; rep < 2000 && ok; rep++) {
int n = rng() % 8, m = rng() % 8;
std::vector<long long> a = rand_vec(n), b = rand_vec(m);
long long c = static_cast<long long>(rng() % 11) - 5;
// ++ / -- (前置は変更後、後置は変更前を返す)
{
std::vector<long long> x = a, inc = a, dec = a;
for (auto& e : inc) e++;
for (auto& e : dec) e--;
check(++x == inc && x == inc, "前置 ++");
check(x-- == inc && x == a, "後置 --");
check(--x == dec && x == dec, "前置 --");
check(x++ == dec && x == a, "後置 ++");
}
// 要素ごとの和 (短い方は 0 で埋める)
{
std::vector<long long> expect(std::max(n, m));
for (int i = 0; i < static_cast<int>(expect.size()); i++) expect[i] = (i < n ? a[i] : 0) + (i < m ? b[i] : 0);
check(a + b == expect, "v + w");
std::vector<long long> x = a;
x += b;
check(x == expect, "v += w");
}
// 要素ごとの積 (どちらかにない位置は 0)
{
std::vector<long long> expect(std::max(n, m));
for (int i = 0; i < static_cast<int>(expect.size()); i++) expect[i] = (i < n && i < m) ? a[i] * b[i] : 0;
check(a * b == expect, "v * w");
std::vector<long long> x = a;
x *= b;
check(x == expect, "v *= w");
}
// スカラーとの和・積
{
std::vector<long long> plus = a, times = a;
for (auto& e : plus) e += c;
for (auto& e : times) e *= c;
check(a + c == plus && c + a == plus, "v + c, c + v");
check(a * c == times && c * a == times, "v * c, c * v");
std::vector<long long> x = a;
x += c;
check(x == plus, "v += c");
x = a;
x *= c;
check(x == times, "v *= c");
}
// 連結と繰り返し
{
std::vector<long long> cat = a;
cat.insert(cat.end(), b.begin(), b.end());
std::vector<long long> x = a;
check(&extend(x, b) == &x && x == cat, "extend");
x = a;
x |= b;
check(x == cat, "v |= w");
int k = rng() % 4;
std::vector<long long> rep_k;
for (int i = 0; i < k; i++) rep_k.insert(rep_k.end(), a.begin(), a.end());
check((a | k) == rep_k && (k | a) == rep_k, "v | k, k | v");
x = a;
x |= k;
check(x == rep_k, "v |= k");
}
// assign (型の変換つき)
{
std::vector<int> small(b.begin(), b.end());
std::vector<long long> x = a;
check(&assign(x, small) == &x && x == b, "assign");
}
// abs
{
std::vector<long long> expect = a;
for (auto& e : expect) e = std::llabs(e);
check(abs(a) == expect, "abs");
}
// argsort: 値の昇順 (降順) に安定ソートした添字の並び
{
for (bool rev : {false, true}) {
auto idx = rev ? argsort(a, std::greater<>()) : argsort(a);
std::vector<long long> perm = idx;
std::sort(perm.begin(), perm.end());
bool is_perm = static_cast<int>(perm.size()) == n;
for (int i = 0; i < static_cast<int>(perm.size()) && is_perm; i++) is_perm = perm[i] == i;
bool is_sorted = true;
for (int i = 0; i + 1 < static_cast<int>(idx.size()); i++) {
long long x = a[idx[i]], y = a[idx[i + 1]];
if (rev ? x < y : x > y) is_sorted = false;
if (x == y && idx[i] > idx[i + 1]) is_sorted = false; // 安定
}
check(is_perm && is_sorted, "argsort");
}
}
// rank: v[i] より小さい (降順なら大きい) 要素の個数
{
for (bool rev : {false, true}) {
auto r = rev ? rank(a, std::greater<>()) : rank(a);
bool same = static_cast<int>(r.size()) == n;
for (int i = 0; i < n && same; i++) {
long long cnt = 0;
for (int j = 0; j < n; j++) cnt += rev ? a[j] > a[i] : a[j] < a[i];
same = r[i] == cnt;
}
check(same, "rank");
}
}
// inverse_permutation: argsort(argsort(v)) と一致し、元の置換と合成すると恒等置換
{
auto p = argsort(a);
auto q = inverse_permutation(p);
check(q == argsort(p), "inverse_permutation == argsort(argsort(v))");
bool is_inv = static_cast<int>(q.size()) == n;
for (int i = 0; i < n && is_inv; i++) is_inv = p[q[i]] == i && q[p[i]] == i;
check(is_inv, "inverse_permutation");
std::vector<int> pi(p.begin(), p.end());
check(inverse_permutation(pi) == q, "inverse_permutation (int)");
}
// argsort / rank (射影つき): 写した値の列に対する結果と一致する
{
auto key = [](long long x) { return x * x; };
std::vector<long long> b(n);
for (int i = 0; i < n; i++) b[i] = key(a[i]);
check(argsort(a, {}, key) == argsort(b), "argsort(v, {}, proj)");
check(argsort(a, std::greater<>(), key) == argsort(b, std::greater<>()), "argsort(v, comp, proj)");
check(rank(a, {}, key) == rank(b), "rank(v, {}, proj)");
check(rank(a, std::greater<>(), key) == rank(b, std::greater<>()), "rank(v, comp, proj)");
std::vector<std::pair<long long, long long>> ps(n);
for (int i = 0; i < n; i++) ps[i] = {a[i], -i};
check(argsort(ps, {}, &std::pair<long long, long long>::first) == argsort(a), "argsort(v, {}, &T::member)");
}
// select: 添字の列で取り出す
{
std::vector<int> idx(n);
for (auto& i : idx) i = static_cast<int>(rng() % std::max(n, 1));
auto s = select(a, idx);
bool ok = static_cast<int>(s.size()) == n;
for (int i = 0; i < n && ok; i++) ok = s[i] == a[idx[i]];
check(ok, "select");
std::vector<long long> expect = a;
std::sort(expect.begin(), expect.end());
check(select(a, argsort(a)) == expect, "select(v, argsort(v))");
}
// iota
{
std::vector<int> expect(n);
for (int i = 0; i < n; i++) expect[i] = i;
check(iota(n) == expect, "iota");
}
// radix_convert: 整数 -> 桁 (上位から) -> 整数
{
int base = rng() % 15 + 2;
long long x = rng() % 1000000000000LL;
std::vector<long long> digits;
for (long long y = x; y > 0; y /= base) digits.push_back(y % base);
std::reverse(digits.begin(), digits.end());
check(radix_convert(x, base) == digits, "radix_convert(x, base)");
check(radix_convert(digits, base) == x, "radix_convert(digits, base)");
}
}
// 決まった値
check(radix_convert(123, 10) == std::vector<long long>{1, 2, 3}, "radix_convert(123)");
check(radix_convert(std::vector<long long>{1, 0, 1, 1}, 2) == 11, "radix_convert({1,0,1,1}, 2)");
check(radix_convert(0, 10).empty() && radix_convert(std::vector<long long>{}, 10) == 0, "radix_convert 0");
check(iota(0).empty(), "iota(0)");
check((std::vector<int>{1, 2} | 0).empty(), "v | 0");
{
const std::vector<__int128_t> indices = {2, 0, 1};
check(inverse_permutation(indices) == std::vector<long long>{1, 2, 0}, "integral constraint accepts __int128");
check(select(std::vector<int>{10, 20, 30}, indices) == std::vector<int>{30, 10, 20}, "select with __int128 indices");
}
if (ok) std::cout << "Hello World" << std::endl;
}#line 1 "test/unittest/utility/vector_func.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
// vector_func.hpp の演算子と関数を愚直な実装とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する
#line 2 "src/utility/vector_func.hpp"
/**
* @file vector_func.hpp
* @brief vector に対する演算子と関数
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <functional>
#include <numeric>
#include <type_traits>
#include <vector>
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#line 10 "src/utility/traits.hpp"
#include <iterator>
#include <set>
#line 13 "src/utility/traits.hpp"
#include <utility>
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 18 "src/utility/vector_func.hpp"
/**
* @brief 値を proj で写して comp の順に安定ソートしたときの添字の並び (APL の grade up)
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> argsort(const std::vector<T>& v, F comp = F(), P proj = P()) {
std::vector<long long> ret(v.size());
std::iota(ret.begin(), ret.end(), 0LL);
std::stable_sort(ret.begin(), ret.end(), [&](long long i, long long j) {
return std::invoke(comp, std::invoke(proj, v[i]), std::invoke(proj, v[j]));
});
return ret;
}
/**
* @brief 各要素の順位 (proj で写して comp の順で v[i] より前に来る要素の個数)。同率は同じ順位で、次の順位は飛ばす
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> rank(const std::vector<T>& v, F comp = F(), P proj = P()) {
const long long n = v.size();
std::vector<long long> p = argsort(v, comp, proj), ret(n);
for (long long i = 0; i < n; i++) {
ret[p[i]] = i > 0 && !std::invoke(comp, std::invoke(proj, v[p[i - 1]]), std::invoke(proj, v[p[i]])) ? ret[p[i - 1]] : i;
}
return ret;
}
/**
* @brief 逆置換 (ret[p[i]] = i)。p は 0, ..., n-1 の置換であること
*/
#if __cplusplus >= 202002L
template <typename I>
requires(std::integral<I>)
#else
template <typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<long long> inverse_permutation(const std::vector<I>& p) {
const long long n = p.size();
std::vector<long long> ret(n, -1);
for (long long i = 0; i < n; i++) {
const long long x = static_cast<long long>(p[i]);
assert(0 <= x && x < n && ret[x] == -1);
ret[x] = i;
}
return ret;
}
/**
* @brief {v[idx[0]], v[idx[1]], ...} (Uiua の select)
*/
#if __cplusplus >= 202002L
template <typename T, typename I>
requires(std::integral<I>)
#else
template <typename T, typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<T> select(const std::vector<T>& v, const std::vector<I>& idx) {
std::vector<T> ret;
ret.reserve(idx.size());
for (I i : idx) {
assert(0 <= static_cast<long long>(i) && static_cast<long long>(i) < static_cast<long long>(v.size()));
ret.push_back(v[i]);
}
return ret;
}
template <typename T>
inline std::vector<T>& operator++(std::vector<T>& v) {
for (auto& e : v) e++;
return v;
}
template <typename T>
inline std::vector<T> operator++(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e++;
return res;
}
template <typename T>
inline std::vector<T>& operator--(std::vector<T>& v) {
for (auto& e : v) e--;
return v;
}
template <typename T>
inline std::vector<T> operator--(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e--;
return res;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] += v2[i];
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res += v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e += u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] *= v2[i];
}
for (std::size_t i = v2.size(); i < v1.size(); i++) {
v1[i] *= U(0);
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res *= v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e *= u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res *= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res *= u;
}
template <typename T, typename U>
inline std::vector<T>& assign(std::vector<T>& v1, const std::vector<U>& v2) {
v1.assign(v2.begin(), v2.end());
return v1;
}
template <typename T, typename U>
inline std::vector<T>& extend(std::vector<T>& v1, const std::vector<U>& v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v1, const std::vector<U>& v2) {
return extend(v1, v2);
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v, const U& u) {
if constexpr (std::is_signed_v<U>) assert(u >= 0);
std::vector<T> w(v);
v.clear();
for (long long i = 0; i < static_cast<long long>(u); i++) {
extend(v, w);
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res |= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res |= u;
}
template <typename T>
inline std::vector<T> abs(const std::vector<T>& v) {
std::vector<T> ret;
ret.reserve(v.size());
for (const T& e : v) ret.push_back(std::abs(e));
return ret;
}
template <typename T>
std::vector<T> cumulative_sum(std::vector<T> v) {
v.insert(v.begin(), T{});
std::vector<T> ret(v.size());
std::partial_sum(v.begin(), v.end(), ret.begin());
return ret;
}
#if __cplusplus >= 202002L
template <typename T>
requires(std::integral<T>)
#else
template <typename T, ENABLE_IF_T(std::is_integral_v<T>)>
#endif
std::vector<T> iota(T n) {
assert(n >= 0);
std::vector<T> ret(n);
std::iota(ret.begin(), ret.end(), 0);
return ret;
}
template <typename T>
std::vector<T> unique(const std::vector<T>& v) {
std::vector<T> res(v);
std::sort(res.begin(), res.end());
res.erase(std::unique(res.begin(), res.end()), res.end());
return res;
}
inline long long radix_convert(const std::vector<long long>& v, int base = 10) {
assert(base >= 2);
long long res = 0;
for (long long d : v) {
assert(0 <= d && d < base);
res = res * base + d;
}
return res;
}
inline std::vector<long long> radix_convert(long long v, int base = 10) {
assert(v >= 0 && base >= 2);
std::vector<long long> res;
while (v > 0) {
res.push_back(v % base);
v /= base;
}
std::reverse(res.begin(), res.end());
return res;
}
#line 7 "test/unittest/utility/vector_func.test.cpp"
#line 9 "test/unittest/utility/vector_func.test.cpp"
#include <cstdlib>
#line 11 "test/unittest/utility/vector_func.test.cpp"
#include <iostream>
#include <random>
#line 14 "test/unittest/utility/vector_func.test.cpp"
bool ok = true;
void check(bool cond, const char* what) {
if (!cond) {
if (ok) std::cerr << "failed: " << what << std::endl;
ok = false;
}
}
int main() {
std::mt19937_64 rng(314);
auto rand_vec = [&](int n) {
std::vector<long long> v(n);
for (auto& x : v) x = static_cast<long long>(rng() % 21) - 10;
return v;
};
for (int rep = 0; rep < 2000 && ok; rep++) {
int n = rng() % 8, m = rng() % 8;
std::vector<long long> a = rand_vec(n), b = rand_vec(m);
long long c = static_cast<long long>(rng() % 11) - 5;
// ++ / -- (前置は変更後、後置は変更前を返す)
{
std::vector<long long> x = a, inc = a, dec = a;
for (auto& e : inc) e++;
for (auto& e : dec) e--;
check(++x == inc && x == inc, "前置 ++");
check(x-- == inc && x == a, "後置 --");
check(--x == dec && x == dec, "前置 --");
check(x++ == dec && x == a, "後置 ++");
}
// 要素ごとの和 (短い方は 0 で埋める)
{
std::vector<long long> expect(std::max(n, m));
for (int i = 0; i < static_cast<int>(expect.size()); i++) expect[i] = (i < n ? a[i] : 0) + (i < m ? b[i] : 0);
check(a + b == expect, "v + w");
std::vector<long long> x = a;
x += b;
check(x == expect, "v += w");
}
// 要素ごとの積 (どちらかにない位置は 0)
{
std::vector<long long> expect(std::max(n, m));
for (int i = 0; i < static_cast<int>(expect.size()); i++) expect[i] = (i < n && i < m) ? a[i] * b[i] : 0;
check(a * b == expect, "v * w");
std::vector<long long> x = a;
x *= b;
check(x == expect, "v *= w");
}
// スカラーとの和・積
{
std::vector<long long> plus = a, times = a;
for (auto& e : plus) e += c;
for (auto& e : times) e *= c;
check(a + c == plus && c + a == plus, "v + c, c + v");
check(a * c == times && c * a == times, "v * c, c * v");
std::vector<long long> x = a;
x += c;
check(x == plus, "v += c");
x = a;
x *= c;
check(x == times, "v *= c");
}
// 連結と繰り返し
{
std::vector<long long> cat = a;
cat.insert(cat.end(), b.begin(), b.end());
std::vector<long long> x = a;
check(&extend(x, b) == &x && x == cat, "extend");
x = a;
x |= b;
check(x == cat, "v |= w");
int k = rng() % 4;
std::vector<long long> rep_k;
for (int i = 0; i < k; i++) rep_k.insert(rep_k.end(), a.begin(), a.end());
check((a | k) == rep_k && (k | a) == rep_k, "v | k, k | v");
x = a;
x |= k;
check(x == rep_k, "v |= k");
}
// assign (型の変換つき)
{
std::vector<int> small(b.begin(), b.end());
std::vector<long long> x = a;
check(&assign(x, small) == &x && x == b, "assign");
}
// abs
{
std::vector<long long> expect = a;
for (auto& e : expect) e = std::llabs(e);
check(abs(a) == expect, "abs");
}
// argsort: 値の昇順 (降順) に安定ソートした添字の並び
{
for (bool rev : {false, true}) {
auto idx = rev ? argsort(a, std::greater<>()) : argsort(a);
std::vector<long long> perm = idx;
std::sort(perm.begin(), perm.end());
bool is_perm = static_cast<int>(perm.size()) == n;
for (int i = 0; i < static_cast<int>(perm.size()) && is_perm; i++) is_perm = perm[i] == i;
bool is_sorted = true;
for (int i = 0; i + 1 < static_cast<int>(idx.size()); i++) {
long long x = a[idx[i]], y = a[idx[i + 1]];
if (rev ? x < y : x > y) is_sorted = false;
if (x == y && idx[i] > idx[i + 1]) is_sorted = false; // 安定
}
check(is_perm && is_sorted, "argsort");
}
}
// rank: v[i] より小さい (降順なら大きい) 要素の個数
{
for (bool rev : {false, true}) {
auto r = rev ? rank(a, std::greater<>()) : rank(a);
bool same = static_cast<int>(r.size()) == n;
for (int i = 0; i < n && same; i++) {
long long cnt = 0;
for (int j = 0; j < n; j++) cnt += rev ? a[j] > a[i] : a[j] < a[i];
same = r[i] == cnt;
}
check(same, "rank");
}
}
// inverse_permutation: argsort(argsort(v)) と一致し、元の置換と合成すると恒等置換
{
auto p = argsort(a);
auto q = inverse_permutation(p);
check(q == argsort(p), "inverse_permutation == argsort(argsort(v))");
bool is_inv = static_cast<int>(q.size()) == n;
for (int i = 0; i < n && is_inv; i++) is_inv = p[q[i]] == i && q[p[i]] == i;
check(is_inv, "inverse_permutation");
std::vector<int> pi(p.begin(), p.end());
check(inverse_permutation(pi) == q, "inverse_permutation (int)");
}
// argsort / rank (射影つき): 写した値の列に対する結果と一致する
{
auto key = [](long long x) { return x * x; };
std::vector<long long> b(n);
for (int i = 0; i < n; i++) b[i] = key(a[i]);
check(argsort(a, {}, key) == argsort(b), "argsort(v, {}, proj)");
check(argsort(a, std::greater<>(), key) == argsort(b, std::greater<>()), "argsort(v, comp, proj)");
check(rank(a, {}, key) == rank(b), "rank(v, {}, proj)");
check(rank(a, std::greater<>(), key) == rank(b, std::greater<>()), "rank(v, comp, proj)");
std::vector<std::pair<long long, long long>> ps(n);
for (int i = 0; i < n; i++) ps[i] = {a[i], -i};
check(argsort(ps, {}, &std::pair<long long, long long>::first) == argsort(a), "argsort(v, {}, &T::member)");
}
// select: 添字の列で取り出す
{
std::vector<int> idx(n);
for (auto& i : idx) i = static_cast<int>(rng() % std::max(n, 1));
auto s = select(a, idx);
bool ok = static_cast<int>(s.size()) == n;
for (int i = 0; i < n && ok; i++) ok = s[i] == a[idx[i]];
check(ok, "select");
std::vector<long long> expect = a;
std::sort(expect.begin(), expect.end());
check(select(a, argsort(a)) == expect, "select(v, argsort(v))");
}
// iota
{
std::vector<int> expect(n);
for (int i = 0; i < n; i++) expect[i] = i;
check(iota(n) == expect, "iota");
}
// radix_convert: 整数 -> 桁 (上位から) -> 整数
{
int base = rng() % 15 + 2;
long long x = rng() % 1000000000000LL;
std::vector<long long> digits;
for (long long y = x; y > 0; y /= base) digits.push_back(y % base);
std::reverse(digits.begin(), digits.end());
check(radix_convert(x, base) == digits, "radix_convert(x, base)");
check(radix_convert(digits, base) == x, "radix_convert(digits, base)");
}
}
// 決まった値
check(radix_convert(123, 10) == std::vector<long long>{1, 2, 3}, "radix_convert(123)");
check(radix_convert(std::vector<long long>{1, 0, 1, 1}, 2) == 11, "radix_convert({1,0,1,1}, 2)");
check(radix_convert(0, 10).empty() && radix_convert(std::vector<long long>{}, 10) == 0, "radix_convert 0");
check(iota(0).empty(), "iota(0)");
check((std::vector<int>{1, 2} | 0).empty(), "v | 0");
{
const std::vector<__int128_t> indices = {2, 0, 1};
check(inverse_permutation(indices) == std::vector<long long>{1, 2, 0}, "integral constraint accepts __int128");
check(select(std::vector<int>{10, 20, 30}, indices) == std::vector<int>{30, 10, 20}, "select with __int128 indices");
}
if (ok) std::cout << "Hello World" << std::endl;
}