proconlib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub KodamaD/proconlib

:heavy_check_mark: utility/auto_realloc.cpp

Depends on

Required by

Verified with

Code

#pragma once
#include <utility>
#include <vector>
#include "ceil_log2.cpp"
#include "int_alias.cpp"

template <class F> class AutoReallocation {
    using R = typename decltype(std::declval<F>()(0))::value_type;

    F func;
    mutable std::vector<R> data;

  public:
    explicit AutoReallocation(F&& f) : func(std::forward<F>(f)), data() {}

    void reserve(const int size) const {
        if ((int)data.size() < size) data = func(((int)1 << ceil_log2(size)));
    }
    R operator[](const int i) const {
        assert(i >= 0);
        reserve(i + 1);
        return data[i];
    }
};

template <class F> decltype(auto) auto_realloc(F&& f) { return AutoReallocation<F>(std::forward<F>(f)); }
#line 2 "utility/auto_realloc.cpp"
#include <utility>
#include <vector>
#line 2 "internal/enable_avx2.cpp"

#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"

TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
    return x == 0 ? 64 : __builtin_clzll(x);
#else
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"

TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 5 "utility/ceil_log2.cpp"

TARGET_AVX2 constexpr int ceil_log2(const u64 x) {
#ifdef __GNUC__
    return x == 0 ? 0 : bit_width(x - 1);
#else
    int e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
#endif
}
#line 6 "utility/auto_realloc.cpp"

template <class F> class AutoReallocation {
    using R = typename decltype(std::declval<F>()(0))::value_type;

    F func;
    mutable std::vector<R> data;

  public:
    explicit AutoReallocation(F&& f) : func(std::forward<F>(f)), data() {}

    void reserve(const int size) const {
        if ((int)data.size() < size) data = func(((int)1 << ceil_log2(size)));
    }
    R operator[](const int i) const {
        assert(i >= 0);
        reserve(i + 1);
        return data[i];
    }
};

template <class F> decltype(auto) auto_realloc(F&& f) { return AutoReallocation<F>(std::forward<F>(f)); }
Back to top page