競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
過去の状態を残しながら、任意の版の末尾に値を追加できます。版を表す version_type は std::size_t です。空の版は PersistentStack<T>::kEmpty です。操作に渡す版は同じオブジェクトで取得したものを使います。検査有効時は存在しない版で std::out_of_range を送出します。
T がコピー可能ならスタックもコピーできます。代入後は代入先の以前の版と参照を使えません。
emplacetemplate<class... Args> version_type emplace(version_type version, Args &&...args);
指定した版の末尾に引数から値を構築し、新しい版を返します。元の版はそのまま使えます。償却 O(1) 時間です。
pushversion_type push(version_type version, const T &value);
version_type push(version_type version, T &&value);
値をコピーまたはムーブして末尾に追加します。償却 O(1) 時間です。
popversion_type pop(version_type version) const;
末尾を除いた版を返します。空なら kEmpty を返します。O(1) 時間です。
topconst T &top(version_type version) const;
末尾の値を参照します。版は非空が前提です。検査有効時は空なら std::out_of_range を送出します。以降の追加でも参照は有効です。O(1) 時間です。
size(version) は指定した版の要素数を返します。O(1) 時間です。empty(version) は指定した版が空かを判定します。O(1) 時間です。node_count() はオブジェクト全体で保存している追加ノード数です。O(1) 時間です。空での構築は O(1) 時間です。追加回数を N として使用領域は O(N+1) です。
sirkit::PersistentStack<int> stack;
const auto first = stack.push(stack.kEmpty, 3);
const auto a = stack.push(first, 7);
const auto b = stack.push(first, 9);
assert(stack.top(a) == 7 && stack.top(b) == 9);
assert(stack.pop(b) == first);
PersistentStack<T>(): 空のスタックを作る。version_typeはsize_t、kEmptyは空の版。O(1)。 emplace(version,args...): 指定版の末尾に値を構築した新しい版。元の版を保持する。償却O(1)。 push(version,value): 値をコピーまたはムーブして追加する。償却O(1)。 pop(version): 末尾を除いた既存の版。空ならkEmpty。O(1)。 top(version): 非空の版の末尾をconst参照で返す。以降の追加でも参照は有効。O(1)。 size(version)/empty(version): 版の要素数・空判定。O(1)。 node_count(): 保存している全追加ノード数。O(1)。領域は追加回数Nに対してO(N+1)。 版は同じオブジェクトで取得した値。検査有効時は存在しない版と空版のtopでout_of_range。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Notebook | persistent_stack_notebook.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/persistent_stack.md
//
// API一覧
// PersistentStack<T>(): 空のスタックを作る。version_typeはsize_t、kEmptyは空の版。O(1)。
// emplace(version,args...): 指定版の末尾に値を構築した新しい版。元の版を保持する。償却O(1)。
// push(version,value): 値をコピーまたはムーブして追加する。償却O(1)。
// pop(version): 末尾を除いた既存の版。空ならkEmpty。O(1)。
// top(version): 非空の版の末尾をconst参照で返す。以降の追加でも参照は有効。O(1)。
// size(version)/empty(version): 版の要素数・空判定。O(1)。
// node_count(): 保存している全追加ノード数。O(1)。領域は追加回数Nに対してO(N+1)。
// 版は同じオブジェクトで取得した値。検査有効時は存在しない版と空版のtopでout_of_range。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <concepts>
#include <cstddef>
#include <deque>
#include <stdexcept>
#include <utility>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_PERSISTENT_STACK_DEFINED
#define SIRKIT_PERSISTENT_STACK_DEFINED
template <class T>
class PersistentStack
{
public:
using version_type = std::size_t;
static constexpr version_type kEmpty = 0;
PersistentStack() = default;
PersistentStack(const PersistentStack &) requires std::constructible_from<T, const T &> = default;
PersistentStack(PersistentStack &&) = default;
PersistentStack &operator=(PersistentStack other) noexcept
{
nodes_.swap(other.nodes_);
return *this;
}
template <class... Args>
requires std::constructible_from<T, Args...>
[[nodiscard]] version_type emplace(version_type version, Args &&...args)
{
const auto new_size = size(version) + 1;
nodes_.emplace_back(version, new_size, std::forward<Args>(args)...);
return nodes_.size();
}
[[nodiscard]] version_type push(version_type version, const T &value)
requires std::constructible_from<T, const T &>
{
return emplace(version, value);
}
[[nodiscard]] version_type push(version_type version, T &&value)
requires std::constructible_from<T, T &&>
{
return emplace(version, std::move(value));
}
[[nodiscard]] version_type pop(version_type version) const
{
CheckVersion(version);
return version == kEmpty ? kEmpty : nodes_[version - 1].previous;
}
[[nodiscard]] const T &top(version_type version) const
{
CheckVersion(version);
#ifndef SKIP_RUNTIME_CHECKS
if (version == kEmpty)
{
throw std::out_of_range("PersistentStack: empty version");
}
#endif
return nodes_[version - 1].value;
}
[[nodiscard]] std::size_t size(version_type version) const
{
CheckVersion(version);
return version == kEmpty ? 0 : nodes_[version - 1].size;
}
[[nodiscard]] bool empty(version_type version) const
{
CheckVersion(version);
return version == kEmpty;
}
[[nodiscard]] std::size_t node_count() const noexcept
{
return nodes_.size();
}
private:
struct Node
{
T value;
version_type previous;
std::size_t size;
template <class... Args>
explicit Node(version_type parent, std::size_t length, Args &&...args)
: value(std::forward<Args>(args)...), previous(parent), size(length)
{
}
};
void CheckVersion([[maybe_unused]] version_type version) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (version > nodes_.size())
{
throw std::out_of_range("PersistentStack: invalid version");
}
#endif
}
std::deque<Node> nodes_;
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1