競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
先頭を取り出したり末尾に追加したりしながら、過去の状態も保持できるキューです。版 Version を保存しておけば、同じ版から何通りもの操作を試せます。
Version は版を表す値型です。空の版は Version{} でも作れます。キューをコピーすると節点の保存領域を共有します。非空の版と要素への参照は、その保存領域を所有するキューが残る間有効です。非空の版はそれを作った保存領域を共有するキューに渡します。並行して更新・照会する場合は外部で同期します。
要素型 T はコピーまたはムーブで構築できる型を使います。累計の追加数 $P$ は $2^{32}-1$ 以下です。保存領域は $O(P+1)$ です。
PersistentQueuetemplate <class T>
class PersistentQueue;
PersistentQueue();
空のキューを作ります。 $O(1)$ です。
empty_versionstatic Version empty_version() noexcept;
どのキューにも渡せる空の版を返します。 $O(1)$ です。
pushVersion push(Version version, T value);
指定版の末尾に値を追加した版を返します。元の版はそのまま使えます。 償却 $O(1)$ 時間・1節点です。
popVersion pop(Version version) const;
空でない版の先頭を除いた版を返します。元の版はそのまま使えます。 最悪 $O(1+\log(P+1))$ 時間で節点を追加しません。
frontconst T &front(Version version) const;
空でない版の先頭要素を読み取り専用参照で返します。 $O(1)$ です。
sizestd::size_t size(Version version) const;
指定版の要素数を返します。 $O(1)$ です。
emptybool empty(Version version) const;
指定版が空なら true を返します。 $O(1)$ です。
sirkit::PersistentQueue<int> queue;
const auto one = queue.push(queue.empty_version(), 5);
const auto two = queue.push(one, 8);
const auto branch = queue.push(one, 13);
const auto rest = queue.pop(two);
const int first = queue.front(rest); // 8
const int branch_head = queue.front(branch); // 5
永続配列 は固定長で、添字を指定して値を読み書きできます。
使用例 / Verify / Library Checker
PersistentQueue<T>(): 保存領域を作る。Version{} / empty_version()は空の版。
push(version,value): 末尾へ追加した版を返す。償却O(1)時間・追加ノード1個。
pop(version): 先頭を除いた版を返す。最悪O(log(P+1))時間・追加ノードなし。Pは累計push数。
front(version): 先頭へのconst T&を返す。size(version) / empty(version)も各O(1)。
push/pop後も元の版は使える。コピーしたキューは保存領域を共有する。
非空のVersionとfrontの参照は保存領域の所有者が残る間だけ有効。並行更新は外部同期が必要。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Persistent Queue | persistent_queue.cpp | ✓ 現行AC | AC |
✓ 現行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_queue.md
//
// API一覧
// PersistentQueue<T>(): 保存領域を作る。Version{} / empty_version()は空の版。
// push(version,value): 末尾へ追加した版を返す。償却O(1)時間・追加ノード1個。
// pop(version): 先頭を除いた版を返す。最悪O(log(P+1))時間・追加ノードなし。Pは累計push数。
// front(version): 先頭へのconst T&を返す。size(version) / empty(version)も各O(1)。
// push/pop後も元の版は使える。コピーしたキューは保存領域を共有する。
// 非空のVersionとfrontの参照は保存領域の所有者が残る間だけ有効。並行更新は外部同期が必要。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <cstdint>
#include <deque>
#include <limits>
#include <memory>
#include <stdexcept>
#include <utility>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_PERSISTENT_QUEUE_DEFINED
#define SIRKIT_PERSISTENT_QUEUE_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
inline void CheckPersistentQueueNodeCount(std::size_t count, std::size_t limit)
{
if (count >= limit)
{
throw std::length_error("PersistentQueue: too many pushes");
}
}
}
template <class T>
class PersistentQueue
{
using Index = std::uint32_t;
struct Node
{
Index parent, jump, depth;
T value;
};
struct Storage
{
std::deque<Node> nodes;
};
public:
class Version
{
friend class PersistentQueue;
public:
Version() = default;
private:
Index front_ = 0, back_ = 0;
#ifndef SKIP_RUNTIME_CHECKS
const Storage *owner_ = nullptr;
#endif
};
PersistentQueue() : storage_(std::make_shared<Storage>())
{
}
[[nodiscard]] static Version empty_version() noexcept
{
return {};
}
[[nodiscard]] bool empty(Version version) const
{
CheckVersion(version);
return version.front_ == 0;
}
[[nodiscard]] std::size_t size(Version version) const
{
CheckVersion(version);
if (version.front_ == 0)
{
return 0;
}
return At(version.back_).depth - At(version.front_).depth + 1;
}
[[nodiscard]] const T &front(Version version) const
{
CheckNonempty(version);
return At(version.front_).value;
}
[[nodiscard]] Version push(Version version, T value)
{
CheckVersion(version);
if (!storage_)
{
storage_ = std::make_shared<Storage>();
}
auto &nodes = storage_->nodes;
/*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::CheckPersistentQueueNodeCount(nodes.size(), std::numeric_limits<Index>::max());
const auto id = static_cast<Index>(nodes.size() + 1);
const auto parent = version.back_;
const auto previous_jump = Jump(parent);
const auto next_jump = Jump(previous_jump);
const auto depth = Depth(parent);
const auto previous_depth = Depth(previous_jump);
// 先頭2ブロックが同長なら、新しいノードと合わせて長さ2s+1のブロックにする。
const auto jump = depth - previous_depth == previous_depth - Depth(next_jump) ? next_jump : parent;
nodes.emplace_back(parent, jump, depth + 1, std::move_if_noexcept(value));
return MakeVersion(version.front_ == 0 ? id : version.front_, id);
}
[[nodiscard]] Version pop(Version version) const
{
CheckNonempty(version);
if (version.front_ == version.back_)
{
return {};
}
const auto target_depth = At(version.front_).depth + 1;
auto id = version.back_;
while (At(id).depth > target_depth)
{
const auto &node = At(id);
id = Depth(node.jump) >= target_depth ? node.jump : node.parent;
}
return MakeVersion(id, version.back_);
}
private:
std::shared_ptr<Storage> storage_;
[[nodiscard]] const Node &At(Index index) const noexcept
{
return storage_->nodes[index - 1];
}
[[nodiscard]] Index Depth(Index index) const noexcept
{
return index == 0 ? 0 : At(index).depth;
}
[[nodiscard]] Index Jump(Index index) const noexcept
{
return index == 0 ? 0 : At(index).jump;
}
[[nodiscard]] Version MakeVersion(Index front, Index back) const noexcept
{
Version version;
version.front_ = front;
version.back_ = back;
#ifndef SKIP_RUNTIME_CHECKS
version.owner_ = storage_.get();
#endif
return version;
}
void CheckVersion(Version version) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (version.front_ != 0 && version.owner_ != storage_.get())
{
throw std::invalid_argument("PersistentQueue: foreign version");
}
#else
(void)version;
#endif
}
void CheckNonempty(Version version) const
{
CheckVersion(version);
#ifndef SKIP_RUNTIME_CHECKS
if (version.front_ == 0)
{
throw std::out_of_range("PersistentQueue: empty version");
}
#endif
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1