[MachineOutliner][NFC] Remove Parent field from SuffixTreeNode

This is only used for calculating ConcatLen. This isn't necessary,
since it's easily derived from the traversal setting suffix indices.

Remove that. Rename CurrIdx to CurrNodeLen to better describe what's
going on.

llvm-svn: 346349
This commit is contained in:
Jessica Paquette 2018-11-07 19:56:13 +00:00
parent 917b06fd9a
commit df5b09b8ce
1 changed files with 14 additions and 28 deletions

View File

@ -164,9 +164,6 @@ struct SuffixTreeNode {
/// construction algorithm O(N^2) rather than O(N). /// construction algorithm O(N^2) rather than O(N).
SuffixTreeNode *Link = nullptr; SuffixTreeNode *Link = nullptr;
/// The parent of this node. Every node except for the root has a parent.
SuffixTreeNode *Parent = nullptr;
/// The length of the string formed by concatenating the edge labels from the /// The length of the string formed by concatenating the edge labels from the
/// root to this node. /// root to this node.
unsigned ConcatLen = 0; unsigned ConcatLen = 0;
@ -191,9 +188,8 @@ struct SuffixTreeNode {
return *EndIdx - StartIdx + 1; return *EndIdx - StartIdx + 1;
} }
SuffixTreeNode(unsigned StartIdx, unsigned *EndIdx, SuffixTreeNode *Link, SuffixTreeNode(unsigned StartIdx, unsigned *EndIdx, SuffixTreeNode *Link)
SuffixTreeNode *Parent) : StartIdx(StartIdx), EndIdx(EndIdx), Link(Link) {}
: StartIdx(StartIdx), EndIdx(EndIdx), Link(Link), Parent(Parent) {}
SuffixTreeNode() {} SuffixTreeNode() {}
}; };
@ -286,7 +282,7 @@ private:
assert(StartIdx <= LeafEndIdx && "String can't start after it ends!"); assert(StartIdx <= LeafEndIdx && "String can't start after it ends!");
SuffixTreeNode *N = new (NodeAllocator.Allocate()) SuffixTreeNode *N = new (NodeAllocator.Allocate())
SuffixTreeNode(StartIdx, &LeafEndIdx, nullptr, &Parent); SuffixTreeNode(StartIdx, &LeafEndIdx, nullptr);
Parent.Children[Edge] = N; Parent.Children[Edge] = N;
return N; return N;
@ -309,7 +305,7 @@ private:
unsigned *E = new (InternalEndIdxAllocator) unsigned(EndIdx); unsigned *E = new (InternalEndIdxAllocator) unsigned(EndIdx);
SuffixTreeNode *N = new (NodeAllocator.Allocate()) SuffixTreeNode *N = new (NodeAllocator.Allocate())
SuffixTreeNode(StartIdx, E, Root, Parent); SuffixTreeNode(StartIdx, E, Root);
if (Parent) if (Parent)
Parent->Children[Edge] = N; Parent->Children[Edge] = N;
@ -320,33 +316,24 @@ private:
/// respective suffixes. /// respective suffixes.
/// ///
/// \param[in] CurrNode The node currently being visited. /// \param[in] CurrNode The node currently being visited.
/// \param CurrIdx The current index of the string being visited. /// \param CurrNodeLen The concatenation of all node sizes from the root to
void setSuffixIndices(SuffixTreeNode &CurrNode, unsigned CurrIdx) { /// this node. Used to produce suffix indices.
void setSuffixIndices(SuffixTreeNode &CurrNode, unsigned CurrNodeLen) {
bool IsLeaf = CurrNode.Children.size() == 0 && !CurrNode.isRoot(); bool IsLeaf = CurrNode.Children.size() == 0 && !CurrNode.isRoot();
// Store the length of the concatenation of all strings from the root to // Store the concatenation of lengths down from the root.
// this node. CurrNode.ConcatLen = CurrNodeLen;
if (!CurrNode.isRoot()) {
if (CurrNode.ConcatLen == 0)
CurrNode.ConcatLen = CurrNode.size();
if (CurrNode.Parent)
CurrNode.ConcatLen += CurrNode.Parent->ConcatLen;
}
// Traverse the tree depth-first. // Traverse the tree depth-first.
for (auto &ChildPair : CurrNode.Children) { for (auto &ChildPair : CurrNode.Children) {
assert(ChildPair.second && "Node had a null child!"); assert(ChildPair.second && "Node had a null child!");
setSuffixIndices(*ChildPair.second, CurrIdx + ChildPair.second->size()); setSuffixIndices(*ChildPair.second,
CurrNodeLen + ChildPair.second->size());
} }
// Is this node a leaf? // Is this node a leaf? If it is, give it a suffix index.
if (IsLeaf) { if (IsLeaf)
// If yes, give it a suffix index and bump its parent's occurrence count. CurrNode.SuffixIdx = Str.size() - CurrNodeLen;
CurrNode.SuffixIdx = Str.size() - CurrIdx;
assert(CurrNode.Parent && "CurrNode had no parent!");
}
} }
/// Construct the suffix tree for the prefix of the input ending at /// Construct the suffix tree for the prefix of the input ending at
@ -451,7 +438,6 @@ private:
// Make the old node a child of the split node and update its start // Make the old node a child of the split node and update its start
// index. This is the node n from the diagram. // index. This is the node n from the diagram.
NextNode->StartIdx += Active.Len; NextNode->StartIdx += Active.Len;
NextNode->Parent = SplitNode;
SplitNode->Children[Str[NextNode->StartIdx]] = NextNode; SplitNode->Children[Str[NextNode->StartIdx]] = NextNode;
// SplitNode is an internal node, update the suffix link. // SplitNode is an internal node, update the suffix link.