Refactor some check-phase postorder iterator use. (#4175)

Allow directly constructing a PostorderIterator, to get rid of
`tree.postorder(node_id).end()` indirect construction. For ranges that
don't need tree data, make it clearer that they're not validated.

Note, this subtly gets rid of a subtree size use in the
`tree.postorder(node_id).end()` case (to get the discarded `begin()`
value).
This commit is contained in:
Jon Ross-Perkins
2024-07-27 02:15:56 +00:00
committed by GitHub
parent 66147dee4f
commit 43c0b0a1f2
4 changed files with 31 additions and 33 deletions
+9 -13
View File
@@ -22,21 +22,10 @@ auto Tree::postorder() const -> llvm::iterator_range<PostorderIterator> {
auto Tree::postorder(NodeId n) const
-> llvm::iterator_range<PostorderIterator> {
CARBON_CHECK(n.is_valid());
// The postorder ends after this node, the root, and begins at the start of
// its subtree.
int end_index = n.index + 1;
int start_index = end_index - node_impls_[n.index].subtree_size;
return llvm::iterator_range<PostorderIterator>(
PostorderIterator(NodeId(start_index)),
PostorderIterator(NodeId(end_index)));
}
auto Tree::postorder(NodeId begin, NodeId end) const
-> llvm::iterator_range<PostorderIterator> {
CARBON_CHECK(begin.is_valid() && end.is_valid());
return llvm::iterator_range<PostorderIterator>(
PostorderIterator(begin), PostorderIterator(NodeId(end.index + 1)));
int start_index = n.index - node_impls_[n.index].subtree_size + 1;
return PostorderIterator::MakeRange(NodeId(start_index), n);
}
auto Tree::children(NodeId n) const -> llvm::iterator_range<SiblingIterator> {
@@ -307,6 +296,13 @@ auto Tree::Verify() const -> ErrorOr<Success> {
return Success();
}
auto Tree::PostorderIterator::MakeRange(NodeId begin, NodeId end)
-> llvm::iterator_range<PostorderIterator> {
CARBON_CHECK(begin.is_valid() && end.is_valid());
return llvm::iterator_range<PostorderIterator>(
PostorderIterator(begin), PostorderIterator(NodeId(end.index + 1)));
}
auto Tree::PostorderIterator::Print(llvm::raw_ostream& output) const -> void {
output << node_;
}