[NFC] SuffixTree: Move EmptyIdx into SuffixTreeNode and add a root allocator
authorJessica Paquette <jpaquette@apple.com>
Fri, 12 May 2023 03:41:00 +0000 (20:41 -0700)
committerJessica Paquette <jpaquette@apple.com>
Fri, 12 May 2023 03:41:49 +0000 (20:41 -0700)
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.

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

index e8aa9d4..372b83d 100644 (file)
@@ -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 @@ private:
     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 @@ private:
                                              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();
index 689849a..d94e3f2 100644 (file)
@@ -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.