[llvm] 66520c0 - [NFC] SuffixTree: Move EmptyIdx into SuffixTreeNode and add a root allocator

Jessica Paquette via llvm-commits llvm-commits at lists.llvm.org
Thu May 11 21:34:23 PDT 2023


Author: Jessica Paquette
Date: 2023-05-11T20:41:49-07:00
New Revision: 66520c04cf28ab3e6f700f8a5d0b294100fc9abc

URL: https://github.com/llvm/llvm-project/commit/66520c04cf28ab3e6f700f8a5d0b294100fc9abc
DIFF: https://github.com/llvm/llvm-project/commit/66520c04cf28ab3e6f700f8a5d0b294100fc9abc.diff

LOG: [NFC] SuffixTree: Move EmptyIdx into SuffixTreeNode and add a root allocator

This makes it clearer that EmptyIdx is related to the node.

Also add an allocator for the root so that in the main SuffixTree code we don't
see gross stuff like a nullptr parent etc.

Added: 
    

Modified: 
    llvm/include/llvm/Support/SuffixTree.h
    llvm/lib/Support/SuffixTree.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/SuffixTree.h b/llvm/include/llvm/Support/SuffixTree.h
index e8aa9d4f2546..372b83d1172a 100644
--- a/llvm/include/llvm/Support/SuffixTree.h
+++ b/llvm/include/llvm/Support/SuffixTree.h
@@ -20,9 +20,6 @@
 
 namespace llvm {
 
-/// Represents an undefined index in the suffix tree.
-const unsigned EmptyIdx = -1;
-
 /// A node in a suffix tree which represents a substring or suffix.
 ///
 /// Each node has either no children or at least two children, with the root
@@ -40,6 +37,8 @@ const unsigned EmptyIdx = -1;
 /// suffix in \p SuffixIdx.
 struct SuffixTreeNode {
 public:
+  /// Represents an undefined index in the suffix tree.
+  static const unsigned EmptyIdx = -1;
   enum class NodeKind { ST_Leaf, ST_Internal };
 
 private:
@@ -233,7 +232,7 @@ class SuffixTree {
     SuffixTreeInternalNode *Node = nullptr;
 
     /// The index of the first character in the substring currently being added.
-    unsigned Idx = EmptyIdx;
+    unsigned Idx = SuffixTreeNode::EmptyIdx;
 
     /// The length of the substring we have to add at the current step.
     unsigned Len = 0;
@@ -265,6 +264,11 @@ class SuffixTree {
                                              unsigned StartIdx, unsigned EndIdx,
                                              unsigned Edge);
 
+  /// Allocate the root node and add it to the tree.
+  ///
+  /// \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();

diff  --git a/llvm/lib/Support/SuffixTree.cpp b/llvm/lib/Support/SuffixTree.cpp
index 689849ae9168..d94e3f2c722e 100644
--- a/llvm/lib/Support/SuffixTree.cpp
+++ b/llvm/lib/Support/SuffixTree.cpp
@@ -25,7 +25,7 @@ static size_t numElementsInSubstring(const SuffixTreeNode *N) {
 }
 
 SuffixTree::SuffixTree(const ArrayRef<unsigned> &Str) : Str(Str) {
-  Root = insertInternalNode(nullptr, EmptyIdx, EmptyIdx, 0);
+  Root = insertRoot();
   Active.Node = Root;
 
   // Keep track of the number of suffixes we have to add of the current
@@ -60,7 +60,7 @@ SuffixTree::insertInternalNode(SuffixTreeInternalNode *Parent,
                                unsigned StartIdx, unsigned EndIdx,
                                unsigned Edge) {
   assert(StartIdx <= EndIdx && "String can't start after it ends!");
-  assert(!(!Parent && StartIdx != EmptyIdx) &&
+  assert(!(!Parent && StartIdx != SuffixTreeNode::EmptyIdx) &&
          "Non-root internal nodes must have parents!");
   auto *N = new (InternalNodeAllocator.Allocate())
       SuffixTreeInternalNode(StartIdx, EndIdx, Root);
@@ -69,6 +69,11 @@ SuffixTree::insertInternalNode(SuffixTreeInternalNode *Parent,
   return N;
 }
 
+SuffixTreeInternalNode *SuffixTree::insertRoot() {
+  return insertInternalNode(/*Parent = */ nullptr, SuffixTreeNode::EmptyIdx,
+                            SuffixTreeNode::EmptyIdx, /*Edge = */ 0);
+}
+
 void SuffixTree::setSuffixIndices() {
   // List of nodes we need to visit along with the current length of the
   // string.


        


More information about the llvm-commits mailing list