Skip to content

[Support] Set SuffixIdx and ConcatLen during node insertion #144467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LeisureGensoul
Copy link

Since the suffix indices and concatLens are already determined, they can be set directly during node insertion.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jun 17, 2025

@llvm/pr-subscribers-llvm-support

Author: Gensoul (LeisureGensoul)

Changes

Since the suffix indices and concatLens are already determined, they can be set directly during node insertion.


Full diff: https://github.com/llvm/llvm-project/pull/144467.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Support/SuffixTree.h (-4)
  • (modified) llvm/lib/Support/SuffixTree.cpp (+8-31)
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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants