[llvm] [Support] Set SuffixIdx and ConcatLen during node insertion (PR #144467)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 16 21:55:37 PDT 2025
https://github.com/LeisureGensoul created https://github.com/llvm/llvm-project/pull/144467
Since the suffix indices and concatLens are already determined, they can be set directly during node insertion.
>From c9d3b25f3f5a558c47d0ad59de6e64535b82f6af Mon Sep 17 00:00:00 2001
From: Gensoul <gensoul030929 at outlook.com>
Date: Tue, 17 Jun 2025 12:22:18 +0800
Subject: [PATCH] Set SuffixIdx and ConcatLen directly
---
llvm/include/llvm/Support/SuffixTree.h | 4 ---
llvm/lib/Support/SuffixTree.cpp | 39 ++++++--------------------
2 files changed, 8 insertions(+), 35 deletions(-)
diff --git a/llvm/include/llvm/Support/SuffixTree.h b/llvm/include/llvm/Support/SuffixTree.h
index 4c78235abf508..47cb69eda050c 100644
--- a/llvm/include/llvm/Support/SuffixTree.h
+++ b/llvm/include/llvm/Support/SuffixTree.h
@@ -114,10 +114,6 @@ class SuffixTree {
/// \returns A pointer to the root.
SuffixTreeInternalNode *insertRoot();
- /// Set the suffix indices of the leaves to the start indices of their
- /// respective suffixes.
- void setSuffixIndices();
-
/// Construct the suffix tree for the prefix of the input ending at
/// \p EndIdx.
///
diff --git a/llvm/lib/Support/SuffixTree.cpp b/llvm/lib/Support/SuffixTree.cpp
index a31287836e860..c38b3c07bf740 100644
--- a/llvm/lib/Support/SuffixTree.cpp
+++ b/llvm/lib/Support/SuffixTree.cpp
@@ -45,9 +45,7 @@ SuffixTree::SuffixTree(const ArrayRef<unsigned> &Str,
SuffixesToAdd = extend(PfxEndIdx, SuffixesToAdd);
}
- // Set the suffix indices of each leaf.
assert(Root && "Root node can't be nullptr!");
- setSuffixIndices();
// Collect all leaf nodes of the suffix tree. And for each internal node,
// record the range of leaf nodes that are descendants of it.
@@ -60,6 +58,9 @@ SuffixTreeNode *SuffixTree::insertLeaf(SuffixTreeInternalNode &Parent,
assert(StartIdx <= LeafEndIdx && "String can't start after it ends!");
auto *N = new (LeafNodeAllocator.Allocate())
SuffixTreeLeafNode(StartIdx, &LeafEndIdx);
+ // Since the suffix indices are already determined,
+ // they can be set directly.
+ N->setSuffixIdx(StartIdx - Parent.getConcatLen());
Parent.Children[Edge] = N;
return N;
}
@@ -73,8 +74,12 @@ SuffixTree::insertInternalNode(SuffixTreeInternalNode *Parent,
"Non-root internal nodes must have parents!");
auto *N = new (InternalNodeAllocator.Allocate())
SuffixTreeInternalNode(StartIdx, EndIdx, Root);
- if (Parent)
+ if (Parent) {
+ // Since the concatLens are already determined,
+ // they can be set directly.
+ N->setConcatLen(Parent->getConcatLen() + numElementsInSubstring(N));
Parent->Children[Edge] = N;
+ }
return N;
}
@@ -83,34 +88,6 @@ SuffixTreeInternalNode *SuffixTree::insertRoot() {
SuffixTreeNode::EmptyIdx, /*Edge = */ 0);
}
-void SuffixTree::setSuffixIndices() {
- // List of nodes we need to visit along with the current length of the
- // string.
- SmallVector<std::pair<SuffixTreeNode *, unsigned>> ToVisit;
-
- // Current node being visited.
- SuffixTreeNode *CurrNode = Root;
-
- // Sum of the lengths of the nodes down the path to the current one.
- unsigned CurrNodeLen = 0;
- ToVisit.push_back({CurrNode, CurrNodeLen});
- while (!ToVisit.empty()) {
- std::tie(CurrNode, CurrNodeLen) = ToVisit.pop_back_val();
- // Length of the current node from the root down to here.
- CurrNode->setConcatLen(CurrNodeLen);
- if (auto *InternalNode = dyn_cast<SuffixTreeInternalNode>(CurrNode))
- for (auto &ChildPair : InternalNode->Children) {
- assert(ChildPair.second && "Node had a null child!");
- ToVisit.push_back(
- {ChildPair.second,
- CurrNodeLen + numElementsInSubstring(ChildPair.second)});
- }
- // No children, so we are at the end of the string.
- if (auto *LeafNode = dyn_cast<SuffixTreeLeafNode>(CurrNode))
- LeafNode->setSuffixIdx(Str.size() - CurrNodeLen);
- }
-}
-
void SuffixTree::setLeafNodes() {
// A stack that keeps track of nodes to visit for post-order DFS traversal.
SmallVector<SuffixTreeNode *> ToVisit;
More information about the llvm-commits
mailing list