summaryrefslogtreecommitdiff
path: root/src/support/topological_orders.h
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-08-07 21:45:34 -0400
committerGitHub <noreply@github.com>2024-08-07 18:45:34 -0700
commitc9fd92c25a74a70c9730f1b39b49ef3d91a1a7f1 (patch)
tree7e10942ac4068e6f646759853e7834763081c9a2 /src/support/topological_orders.h
parent2397f2af4512c31e1e54c0e0168302ab1ee06d58 (diff)
downloadbinaryen-c9fd92c25a74a70c9730f1b39b49ef3d91a1a7f1.tar.gz
binaryen-c9fd92c25a74a70c9730f1b39b49ef3d91a1a7f1.tar.bz2
binaryen-c9fd92c25a74a70c9730f1b39b49ef3d91a1a7f1.zip
Simplify TopologicalOrders (#6811)
Make `TopologicalOrders` its own iterator rather than having a separate iterator class that wraps a pointer to `TopologicalOrders`. This simplifies usage in cases where an iterator needs to be persistently stored. Notably, all of the tests continue working as they are.
Diffstat (limited to 'src/support/topological_orders.h')
-rw-r--r--src/support/topological_orders.h44
1 files changed, 18 insertions, 26 deletions
diff --git a/src/support/topological_orders.h b/src/support/topological_orders.h
index acab78ee5..48941c021 100644
--- a/src/support/topological_orders.h
+++ b/src/support/topological_orders.h
@@ -28,35 +28,29 @@ namespace wasm {
// https://en.wikipedia.org/wiki/Topological_sorting) that iteratively makes all
// possible choices for each position of the output order.
struct TopologicalOrders {
+ using value_type = const std::vector<size_t>;
+ using difference_type = std::ptrdiff_t;
+ using reference = const std::vector<size_t>&;
+ using pointer = const std::vector<size_t>*;
+ using iterator_category = std::input_iterator_tag;
+
// Takes an adjacency list, where the list for each vertex is a sorted list of
// the indices of its children, which will appear after it in the order.
TopologicalOrders(const std::vector<std::vector<size_t>>& graph);
- struct Iterator {
- using value_type = const std::vector<size_t>;
- using difference_type = std::ptrdiff_t;
- using reference = const std::vector<size_t>&;
- using pointer = const std::vector<size_t>*;
- using iterator_category = std::input_iterator_tag;
-
- TopologicalOrders* parent;
+ TopologicalOrders begin() { return TopologicalOrders(graph); }
+ TopologicalOrders end() { return TopologicalOrders({}); }
- bool isEnd() const { return !parent || parent->selectors.empty(); }
- bool operator==(const Iterator& other) const {
- return isEnd() == other.isEnd();
- }
- bool operator!=(const Iterator& other) const { return !(*this == other); }
- const std::vector<size_t>& operator*() { return parent->buf; }
- const std::vector<size_t>* operator->() { return &parent->buf; }
- Iterator& operator++() {
- parent->advance();
- return *this;
- }
- Iterator operator++(int) { return ++(*this); }
- };
-
- Iterator begin() { return {this}; }
- Iterator end() { return {nullptr}; }
+ bool operator==(const TopologicalOrders& other) const {
+ return selectors.empty() == other.selectors.empty();
+ }
+ bool operator!=(const TopologicalOrders& other) const {
+ return !(*this == other);
+ }
+ const std::vector<size_t>& operator*() { return buf; }
+ const std::vector<size_t>* operator->() { return &buf; }
+ TopologicalOrders& operator++();
+ TopologicalOrders operator++(int) { return ++(*this); }
private:
// The input graph given as an adjacency list with edges from vertices to
@@ -95,8 +89,6 @@ private:
// A stack of selectors, one for each vertex in a complete topological order.
// Empty if we've already seen every possible ordering.
std::vector<Selector> selectors;
-
- void advance();
};
} // namespace wasm