sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

Sliding Window Aggregation

DataStructure/Sliding_Window_Aggregation.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

SlidingWindowAggregation<T,Op>は、末尾へ追加・先頭から削除するキューと、その全体の集約値を管理します。スライドする区間の和・最小値・写像合成などに使えます。Opはconst呼出可能な結合演算、identityは左右の単位元です。逆演算や左右の交換は要求しません。使用例を参照してください。

以下、Nは要素数です。値の演算・コピーをO(1)として計算量を記します。

コンストラクタ

explicit SlidingWindowAggregation(const T& identity, Op op = Op{});
SlidingWindowAggregation(const SlidingWindowAggregation&) = default;
SlidingWindowAggregation(SlidingWindowAggregation&& other);

指定した単位元identity・結合演算opで、空のキューを構築します。

制約: 冒頭の値型・演算の前提を満たすこと。指定する列長は内部容量の上限内です。

計算量: 空の構築・ムーブ構築はO(1)、コピーは保持要素数に比例。

push_back

void push_back(const T& value);

列の末尾へ値を追加します。

制約: 共通の前提を満たすこと。

計算量: 償却O(1)、1回の最悪O(N)。値のコピー・演算をO(1)とします。

pop_front

void pop_front();

列の先頭を削除します。

制約: 空でないこと。

計算量: 償却O(1)、1回の最悪O(N)。値のコピー・演算をO(1)とします。

front

const T& front() const;

先頭要素のconst参照を返します。

制約: 空でないこと。

計算量: O(1)。

back

const T& back() const;

末尾要素のconst参照を返します。

制約: 空でないこと。

計算量: O(1)。

all_prod

T all_prod() const;

先頭から順にOpで結合した値を返します。空ならidentityです。

制約: 共通の前提を満たすこと。

計算量: O(1)。

size

std::size_t size() const noexcept;

現在のキューの要素数を返します。

制約: 共通の前提を満たすこと。

計算量: O(1)。

empty

bool empty() const noexcept;

空ならtrueを返します。

制約: 共通の前提を満たすこと。

計算量: O(1)。

clear

void clear() noexcept;

全要素を削除します。確保容量は保持する場合があります。

制約: 共通の前提を満たすこと。

計算量: O(N)。

swap

void swap(SlidingWindowAggregation& other);

内容と設定を交換します。

制約: 共通の前提を満たすこと。

計算量: O(1)。値型・比較器のswapの費用は別です。

SlidingWindowAggregation<std::int64_t,std::plus<std::int64_t>> queue(0);
queue.push_back(2); queue.push_back(5);
auto sum = queue.all_prod(); // 7。
queue.pop_front();
auto first = queue.front(); // 5。

前提と所有権

挿入側と削除側の2つのスタックは、それぞれの順序で求めた集約値を保持します。この集約順によって、非可換の演算でも元の列の順序を保ちます。計算量は演算と値のコピーをO(1)としたものです。文字列連結などの費用は別途かかります。保持領域は過去の最大要素数に比例し、削除やclearの後も容量を保持する場合があります。

front・back・pop_frontは非空が前提です。既定で空を検査してout_of_rangeを投げ、SKIP_RUNTIME_CHECKSでこの検査を除きます。front・backの参照は更新・交換・移動で無効になり得ます。T=boolも使えます。

T・Opはコピー可能で、破棄が例外を投げない型を使います。Opに外部副作用を持たせず、コールバックから同じキューを変更しないでください。push・popでコピー・演算・確保が失敗しても列を保ちます。演算の外部状態は巻き戻しません。

コピーは独立、移動元は空です。代入・swapにはT・Opが交換可能であることが必要です。代入の強い例外保証は、その交換がnoexceptの場合に成立します。交換自体が例外を投げる型では途中状態を保証しません。

構成と問題選定はNyaanのSWAGを参考にし、実装は独立に組み立てています。

operator=

SlidingWindowAggregation& operator=(const SlidingWindowAggregation& other);
SlidingWindowAggregation& operator=(SlidingWindowAggregation&& other);

コピーまたはムーブで内容を置き換え、自身への参照を返します。コピーは独立した内容を持ちます。

制約: 型・所有・例外保証は共通の説明に従います。

計算量: 保持する配列・ノードのコピーと旧内容の破棄に比例。ムーブ構築とは異なり、代入では旧内容の解放費用も含みます。

ソースのAPI注記
[SlidingWindowAggregation<T, Op>]
SlidingWindowAggregation(identity, op=Op{}): 空のキュー。identityは左右の単位元。O(1)。
Op(a,b)はconst呼出可能な結合演算。非可換でも可。T/Opはコピー可能、演算に外部副作用がないこと。
push_back(value): 末尾にコピーを追加。pop_front(): 先頭を削除。演算/コピーをO(1)として償却O(1)、最悪O(n)。
front()/back(): 先頭/末尾のconst T&。all_prod(): 先頭から順に合成した値、空ならidentity。O(1)。
front/back/pop_frontは非空が前提。既定で空を検査しout_of_range。SKIP_RUNTIME_CHECKSで検査を除く。
size()/empty(): 要素数/空か。O(1)。clear(): 全削除、O(n)。swap(other): 演算/単位元/列を交換、O(1)。
代入/swapではT/Opをswapで交換可能とする。コピーは独立、移動元は空。保持領域O(過去最大n+1)。
front/backの参照は更新/clear/交換/移動で無効になり得る。
push/pop中のコピー・演算・確保の例外では列を保つ。代入の強い保証はT/Opのswapがnoexceptの場合。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Queue Operate All Composite sliding_window_aggregation.cpp 未提出 —

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 未測定
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(構成・問題選定): https://nyaannyaan.github.io/library/data-structure/slide-window-aggregation.hpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/sliding_window_aggregation.md
//
// API一覧
// [SlidingWindowAggregation<T, Op>]
// SlidingWindowAggregation(identity, op=Op{}): 空のキュー。identityは左右の単位元。O(1)。
// Op(a,b)はconst呼出可能な結合演算。非可換でも可。T/Opはコピー可能、演算に外部副作用がないこと。
// push_back(value): 末尾にコピーを追加。pop_front(): 先頭を削除。演算/コピーをO(1)として償却O(1)、最悪O(n)。
// front()/back(): 先頭/末尾のconst T&。all_prod(): 先頭から順に合成した値、空ならidentity。O(1)。
// front/back/pop_frontは非空が前提。既定で空を検査しout_of_range。SKIP_RUNTIME_CHECKSで検査を除く。
// size()/empty(): 要素数/空か。O(1)。clear(): 全削除、O(n)。swap(other): 演算/単位元/列を交換、O(1)。
// 代入/swapではT/Opをswapで交換可能とする。コピーは独立、移動元は空。保持領域O(過去最大n+1)。
// front/backの参照は更新/clear/交換/移動で無効になり得る。
// push/pop中のコピー・演算・確保の例外では列を保つ。代入の強い保証はT/Opのswapがnoexceptの場合。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_SLIDING_WINDOW_AGGREGATION_DEFINED
#define SIRKIT_SLIDING_WINDOW_AGGREGATION_DEFINED
template<class T, class Op>
class SlidingWindowAggregation {
    struct Entry { T value, product; };
    [[no_unique_address]] Op op_;
    T identity_;
    std::vector<Entry> incoming_, outgoing_;

    void check_nonempty() const {
#ifndef SKIP_RUNTIME_CHECKS
        if (empty()) throw std::out_of_range("SlidingWindowAggregation: empty");
#endif
    }
    void transfer() {
        // 構築が完了するまで元のキューを変更しない。
        std::vector<Entry> replacement;
        replacement.reserve(incoming_.size());
        for (auto i = incoming_.size(); i != 0; --i) {
            const T& value = incoming_[i - 1].value;
            T product = replacement.empty() ? value : std::as_const(op_)(value, replacement.back().product);
            replacement.push_back(Entry{value, std::move_if_noexcept(product)});
        }
        outgoing_.swap(replacement);
        incoming_.clear();
    }
public:
    explicit SlidingWindowAggregation(const T& identity, Op op = Op{})
        : op_(std::move_if_noexcept(op)), identity_(identity) {}
    SlidingWindowAggregation(const SlidingWindowAggregation&) = default;
    SlidingWindowAggregation& operator=(const SlidingWindowAggregation& other) {
        if (this != &other) { SlidingWindowAggregation replacement(other); swap(replacement); }
        return *this;
    }
    SlidingWindowAggregation(SlidingWindowAggregation&& other)
        : op_(other.op_), identity_(other.identity_), incoming_(std::move(other.incoming_)), outgoing_(std::move(other.outgoing_)) {
        other.incoming_.clear(); other.outgoing_.clear();
    }
    SlidingWindowAggregation& operator=(SlidingWindowAggregation&& other) {
        if (this != &other) { SlidingWindowAggregation replacement(std::move(other)); swap(replacement); }
        return *this;
    }
    std::size_t size() const noexcept { return incoming_.size() + outgoing_.size(); }
    bool empty() const noexcept { return incoming_.empty() && outgoing_.empty(); }
    void push_back(const T& value) {
        // Entryは値を2個持つため、両配列の要素数の和はsize_tの最大値未満。
        T product = incoming_.empty() ? value : std::as_const(op_)(incoming_.back().product, value);
        incoming_.push_back(Entry{value, std::move_if_noexcept(product)});
    }
    void pop_front() {
        check_nonempty();
        if (outgoing_.empty()) transfer();
        outgoing_.pop_back();
    }
    const T& front() const {
        check_nonempty();
        return outgoing_.empty() ? incoming_.front().value : outgoing_.back().value;
    }
    const T& back() const {
        check_nonempty();
        return incoming_.empty() ? outgoing_.front().value : incoming_.back().value;
    }
    T all_prod() const {
        if (outgoing_.empty()) return incoming_.empty() ? identity_ : incoming_.back().product;
        if (incoming_.empty()) return outgoing_.back().product;
        return op_(outgoing_.back().product, incoming_.back().product);
    }
    void clear() noexcept { incoming_.clear(); outgoing_.clear(); }
    void swap(SlidingWindowAggregation& other) noexcept(std::is_nothrow_swappable_v<T> && std::is_nothrow_swappable_v<Op>) {
        using std::swap;
        swap(op_, other.op_); swap(identity_, other.identity_);
        incoming_.swap(other.incoming_); outgoing_.swap(other.outgoing_);
    }
};
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page