shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/data_structures/fast-set.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// FastSet を std::set による愚直な実装とランダムな操作列で比べる
// 段の境目 (64、4096、262144) をまたぐ n も試す
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/data_structures/fast-set.hpp"

#include <iostream>
#include <random>
#include <set>
#include <vector>

bool ok = true;
void check(bool cond, const char* what) {
    if (!cond && ok) std::cerr << "failed: " << what << std::endl;
    if (!cond) ok = false;
}

long long naive_next(const std::set<long long>& s, long long x) {
    auto it = s.lower_bound(x);
    return it == s.end() ? -1 : *it;
}
long long naive_prev(const std::set<long long>& s, long long x) {
    auto it = s.upper_bound(x);
    return it == s.begin() ? -1 : *std::prev(it);
}

int main() {
    std::mt19937_64 rng(314);
    std::vector<long long> sizes = {0, 1, 2, 63, 64, 65, 127, 128, 4095, 4096, 4097, 262143, 262144, 262145};
    for (int rep = 0; rep < 300; rep++) sizes.push_back(rng() % 300 + 1);
    for (long long n : sizes) {
        if (!ok) break;
        // 要素が少ない場合と多い場合を混ぜる
        int density = rng() % 3;
        std::vector<bool> init(n);
        std::set<long long> s;
        for (long long i = 0; i < n; i++) {
            bool in = density == 0 ? rng() % 50 == 0 : density == 1 ? rng() % 2 == 0
                                                                    : rng() % 50 != 0;
            init[i] = in;
            if (in) s.insert(i);
        }
        FastSet fs = rng() % 2 ? FastSet(init) : FastSet(n);
        if (fs.empty() && !s.empty()) {
            for (long long x : s) fs.insert(x);
        }
        check(fs.limit() == n, "limit");
        int q = n > 10000 ? 3000 : 1000;
        for (int t = 0; t < q && ok; t++) {
            // n の外側 (負や n 以上) も next / prev に渡す
            long long x = n == 0 ? 0 : static_cast<long long>(rng() % n);
            long long y = static_cast<long long>(rng() % (n + 4)) - 2;
            int type = rng() % 6;
            if (n > 0 && type == 0) {
                check(fs.insert(x) == s.insert(x).second, "insert");
            } else if (n > 0 && type == 1) {
                check(fs.erase(x) == (s.erase(x) == 1), "erase");
            } else if (n > 0 && type == 2) {
                check(fs.contains(x) == (s.count(x) == 1) && fs.count(x) == static_cast<long long>(s.count(x)), "contains");
            } else if (type == 3) {
                check(fs.next(y) == naive_next(s, y), "next");
            } else if (type == 4) {
                check(fs.prev(y) == naive_prev(s, y), "prev");
            } else {
                check(fs.min() == (s.empty() ? -1 : *s.begin()) && fs.max() == (s.empty() ? -1 : *s.rbegin()), "min / max");
            }
            check(fs.size() == static_cast<long long>(s.size()) && fs.empty() == s.empty(), "size");
        }
        check(fs.to_vector() == std::vector<long long>(s.begin(), s.end()), "to_vector");
        fs.clear();
        check(fs.empty() && fs.min() == -1 && fs.max() == -1, "clear");
    }
    // 大きい n: 10^9 でも数要素だけなら速く動く
    {
        FastSet fs(1000000000);
        fs.insert(0), fs.insert(123456789), fs.insert(999999999);
        check(fs.next(1) == 123456789 && fs.next(123456790) == 999999999 && fs.prev(999999998) == 123456789 && fs.prev(-5) == -1, "10^9");
        fs.erase(123456789);
        check(fs.next(1) == 999999999 && fs.prev(999999998) == 0, "10^9 erase");
    }

    if (ok) std::cout << "Hello World" << std::endl;
}
#line 1 "test/unittest/data_structures/fast-set.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// FastSet を std::set による愚直な実装とランダムな操作列で比べる
// 段の境目 (64、4096、262144) をまたぐ n も試す
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/data_structures/fast-set.hpp"

/**
 * @file fast-set.hpp
 * @brief 64 分木による整数の集合 (FastSet)
 */

#include <cassert>
#include <cstdint>
#include <vector>

/**
 * @brief [0, n) の整数の集合を 64 分木で持つ
 *
 * 各段は 64 bit の配列で、上の段の bit は下の段の 64 bit が 0 でないかを表す
 * 挿入・削除・x 以上の最小・x 以下の最大を O(log_64 n) で行う (n = 10^9 でも 5 段程度)
 * 例:
 * FastSet s(n);
 * s.insert(3); s.insert(10);
 * s.next(4);  // 10
 * s.prev(4);  // 3
 */
class FastSet {
    long long n, cnt = 0;
    std::vector<std::vector<std::uint64_t>> seg;  // seg[0] が要素の bit、seg[h + 1] は seg[h] の各 64 bit が 0 でないか

    void build_levels() {
        // 1 つの 64 bit に収まるまで段を重ねる
        do {
            long long m = seg.empty() ? n : static_cast<long long>(seg.back().size());
            seg.emplace_back((m + 63) / 64, 0);
        } while (seg.back().size() > 1);
    }

   public:
    /**
     * @brief [0, n) の空の集合
     */
    explicit FastSet(long long n = 0) : n(n) {
        assert(n >= 0);
        build_levels();
    }
    /**
     * @brief v[i] が true の i からなる集合 O(n)
     */
    explicit FastSet(const std::vector<bool>& v) : FastSet(static_cast<long long>(v.size())) {
        for (long long i = 0; i < n; i++) {
            if (v[i]) seg[0][i >> 6] |= std::uint64_t(1) << (i & 63), cnt++;
        }
        for (int h = 0; h + 1 < static_cast<int>(seg.size()); h++) {
            for (long long i = 0; i < static_cast<long long>(seg[h].size()); i++) {
                if (seg[h][i]) seg[h + 1][i >> 6] |= std::uint64_t(1) << (i & 63);
            }
        }
    }

    /**
     * @brief 扱える整数の範囲 [0, n) の n
     */
    long long limit() const { return n; }
    /**
     * @brief 要素数
     */
    long long size() const { return cnt; }
    /**
     * @brief 空か
     */
    bool empty() const { return cnt == 0; }

    /**
     * @brief x が含まれるか O(1)
     */
    bool contains(long long x) const {
        assert(0 <= x && x < n);
        return seg[0][x >> 6] >> (x & 63) & 1;
    }
    /**
     * @brief x の個数 (0 か 1) O(1)
     */
    long long count(long long x) const { return contains(x); }
    /**
     * @brief x を挿入する。新しく入ったら true O(log_64 n)
     */
    bool insert(long long x) {
        assert(0 <= x && x < n);
        if (contains(x)) return false;
        cnt++;
        for (auto& level : seg) {
            level[x >> 6] |= std::uint64_t(1) << (x & 63);
            x >>= 6;
        }
        return true;
    }
    /**
     * @brief x を削除する。入っていたら true O(log_64 n)
     */
    bool erase(long long x) {
        assert(0 <= x && x < n);
        if (!contains(x)) return false;
        cnt--;
        for (auto& level : seg) {
            level[x >> 6] &= ~(std::uint64_t(1) << (x & 63));
            // 同じ 64 bit に他の要素が残っていれば、上の段は変わらない
            if (level[x >> 6]) break;
            x >>= 6;
        }
        return true;
    }

    /**
     * @brief x 以上の最小の要素 (なければ -1) O(log_64 n)
     */
    long long next(long long x) const {
        if (x < 0) x = 0;
        if (x >= n) return -1;
        for (int h = 0; h < static_cast<int>(seg.size()); h++) {
            if ((x >> 6) == static_cast<long long>(seg[h].size())) break;
            std::uint64_t d = seg[h][x >> 6] >> (x & 63);
            if (!d) {
                x = (x >> 6) + 1;
                continue;
            }
            // この段で見つかったので、下の段では最も小さい bit をたどる
            x += __builtin_ctzll(d);
            for (int g = h - 1; g >= 0; g--) {
                x <<= 6;
                x += __builtin_ctzll(seg[g][x >> 6]);
            }
            return x;
        }
        return -1;
    }
    /**
     * @brief x 以下の最大の要素 (なければ -1) O(log_64 n)
     */
    long long prev(long long x) const {
        if (x >= n) x = n - 1;
        if (x < 0) return -1;
        for (int h = 0; h < static_cast<int>(seg.size()); h++) {
            if (x == -1) break;
            std::uint64_t d = seg[h][x >> 6] << (63 - (x & 63));
            if (!d) {
                x = (x >> 6) - 1;
                continue;
            }
            // この段で見つかったので、下の段では最も大きい bit をたどる
            x -= __builtin_clzll(d);
            for (int g = h - 1; g >= 0; g--) {
                x <<= 6;
                x += 63 - __builtin_clzll(seg[g][x >> 6]);
            }
            return x;
        }
        return -1;
    }
    /**
     * @brief 最小の要素 (空なら -1) O(log_64 n)
     */
    long long min() const { return next(0); }
    /**
     * @brief 最大の要素 (空なら -1) O(log_64 n)
     */
    long long max() const { return prev(n - 1); }

    /**
     * @brief 要素を昇順に並べたもの O(size() log_64 n)
     */
    std::vector<long long> to_vector() const {
        std::vector<long long> res;
        res.reserve(cnt);
        for (long long x = next(0); x != -1; x = next(x + 1)) res.push_back(x);
        return res;
    }
    /**
     * @brief すべて削除する O(n / 64)
     */
    void clear() {
        for (auto& level : seg) level.assign(level.size(), 0);
        cnt = 0;
    }
};
#line 8 "test/unittest/data_structures/fast-set.test.cpp"

#include <iostream>
#include <random>
#include <set>
#line 13 "test/unittest/data_structures/fast-set.test.cpp"

bool ok = true;
void check(bool cond, const char* what) {
    if (!cond && ok) std::cerr << "failed: " << what << std::endl;
    if (!cond) ok = false;
}

long long naive_next(const std::set<long long>& s, long long x) {
    auto it = s.lower_bound(x);
    return it == s.end() ? -1 : *it;
}
long long naive_prev(const std::set<long long>& s, long long x) {
    auto it = s.upper_bound(x);
    return it == s.begin() ? -1 : *std::prev(it);
}

int main() {
    std::mt19937_64 rng(314);
    std::vector<long long> sizes = {0, 1, 2, 63, 64, 65, 127, 128, 4095, 4096, 4097, 262143, 262144, 262145};
    for (int rep = 0; rep < 300; rep++) sizes.push_back(rng() % 300 + 1);
    for (long long n : sizes) {
        if (!ok) break;
        // 要素が少ない場合と多い場合を混ぜる
        int density = rng() % 3;
        std::vector<bool> init(n);
        std::set<long long> s;
        for (long long i = 0; i < n; i++) {
            bool in = density == 0 ? rng() % 50 == 0 : density == 1 ? rng() % 2 == 0
                                                                    : rng() % 50 != 0;
            init[i] = in;
            if (in) s.insert(i);
        }
        FastSet fs = rng() % 2 ? FastSet(init) : FastSet(n);
        if (fs.empty() && !s.empty()) {
            for (long long x : s) fs.insert(x);
        }
        check(fs.limit() == n, "limit");
        int q = n > 10000 ? 3000 : 1000;
        for (int t = 0; t < q && ok; t++) {
            // n の外側 (負や n 以上) も next / prev に渡す
            long long x = n == 0 ? 0 : static_cast<long long>(rng() % n);
            long long y = static_cast<long long>(rng() % (n + 4)) - 2;
            int type = rng() % 6;
            if (n > 0 && type == 0) {
                check(fs.insert(x) == s.insert(x).second, "insert");
            } else if (n > 0 && type == 1) {
                check(fs.erase(x) == (s.erase(x) == 1), "erase");
            } else if (n > 0 && type == 2) {
                check(fs.contains(x) == (s.count(x) == 1) && fs.count(x) == static_cast<long long>(s.count(x)), "contains");
            } else if (type == 3) {
                check(fs.next(y) == naive_next(s, y), "next");
            } else if (type == 4) {
                check(fs.prev(y) == naive_prev(s, y), "prev");
            } else {
                check(fs.min() == (s.empty() ? -1 : *s.begin()) && fs.max() == (s.empty() ? -1 : *s.rbegin()), "min / max");
            }
            check(fs.size() == static_cast<long long>(s.size()) && fs.empty() == s.empty(), "size");
        }
        check(fs.to_vector() == std::vector<long long>(s.begin(), s.end()), "to_vector");
        fs.clear();
        check(fs.empty() && fs.min() == -1 && fs.max() == -1, "clear");
    }
    // 大きい n: 10^9 でも数要素だけなら速く動く
    {
        FastSet fs(1000000000);
        fs.insert(0), fs.insert(123456789), fs.insert(999999999);
        check(fs.next(1) == 123456789 && fs.next(123456790) == 999999999 && fs.prev(999999998) == 123456789 && fs.prev(-5) == -1, "10^9");
        fs.erase(123456789);
        check(fs.next(1) == 999999999 && fs.prev(999999998) == 0, "10^9 erase");
    }

    if (ok) std::cout << "Hello World" << std::endl;
}
Back to top page