Skip to content

Commit eaca4a3

Browse files
committed
add trace
1 parent 3be3888 commit eaca4a3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

docs/marathon/trace.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Trace(経路復元)
3+
documentation_of: ./marathon/trace.cpp
4+
---
5+
6+
- `add(value, id)`
7+
- 採用した値 `value` と直前の状態の識別子 `id` を与えて、状態を追加する
8+
- 追加した状態の識別子が返ってくる
9+
- `get(id)`
10+
- 状態の識別子 `id` を与えると、初期状態からここに至るまでの経路を復元したものが返ってくる

marathon/trace.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
template <typename Tp>
2+
class Trace {
3+
public:
4+
Trace() = default;
5+
6+
int add(const Tp& value, int parent) {
7+
log_.emplace_back(value, parent);
8+
return (int)log_.size() - 1;
9+
}
10+
11+
vector<Tp> get(int index) const {
12+
vector<Tp> result;
13+
while (index >= 0) {
14+
result.emplace_back(log_[index].first);
15+
index = log_[index].second;
16+
}
17+
reverse(result.begin(), result.end());
18+
return result;
19+
}
20+
21+
private:
22+
vector<pair<Tp, int>> log_;
23+
};

0 commit comments

Comments
 (0)