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
/// 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:
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;
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();
}
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
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);
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.