예제 #1
0
		public void MoveTo(SuffixNode node, int distanceFromNode)
		{
			CurrentNode = node;
			Distance = distanceFromNode;
		}
예제 #2
0
		public SuffixTree()
		{
			Root = new SuffixNode(this);
		}
예제 #3
0
		public void MoveTo(SuffixNode node)
		{
			CurrentNode = node;
			Distance = 0;
		}
예제 #4
0
		/// <summary>
		/// Adds a new leaf at the current position (also a new branch, if we are not currently on a node), 
		/// and sets Next to the branch node (the leaf's parent).
		/// </summary>
		/// <returns>Whether the branch node already existed</returns>
		protected bool AddBranch(int k)
		{
			var parent = LastNode;
			if (Distance != 0)
			{
				// Adding branch between two nodes
				var next = CurrentNode;

				// split edge & add branch node
				// (next also describes the edge between parent and next)
				var branchNode = new SuffixNode(parent, next.From, next.From + Distance - 1);
				parent.Children.Remove(next);		// HashSet Add/Remove operations are constant
				parent.Children.Add(branchNode);
				branchNode.Children.Add(next);
				next.Parent = branchNode;
				next.From += Distance;

				// branchNode becomes parent of the new leaf
				parent = branchNode;
			}
			// else Add branch to existing node

			// add new leaf
			var leaf = new SuffixNode(parent, k);
			parent.Children.Add(leaf);

#if DEBUG
			Console.WriteLine("   Added leaf {0} {1}: {2}", leaf, Distance == 0 ? "to existing branch" : "to new branch", parent);
#endif

			CurrentNode = parent;
			return Distance == 0;
		}
예제 #5
0
		/// <summary>
		/// Adds a new link
		/// </summary>
		private void AddLink(SuffixNode startNode, SuffixNode linkedNode)
		{
#if DEBUG
			Console.WriteLine("   Added link from {0} to {1}", startNode, linkedNode);
#endif
			// link is only a simple pointer
			var newLink = new SuffixLink
			{
				Node = linkedNode
			};
			Links.Add(startNode, newLink);
		}
예제 #6
0
		/// <summary>
		/// Move to the link that the given branch links to
		/// </summary>
		/// <returns>The length of the substring for that link</returns>
		private void MoveToLink(SuffixNode branch)
		{
			SuffixLink link;
			if (Links.TryGetValue(branch, out link))
			{
				// Move to link and return saved length
				MoveTo(link.Node);
#if DEBUG
				Console.WriteLine("   Moved along link to: {0}", CurrentNode);
#endif
			}
			else if (CurrentNode != Tree.Root)
			{
				throw new SuffixTreeBuilderException("Unable to build SuffixTree: Internal node did not have a link: " + CurrentNode);
			}
		}
예제 #7
0
		internal SuffixNode(SuffixNode parent, int from, int to) : 
			this(parent.Tree, from, to)
		{
			Parent = parent;
			Parent.Children.Add(this);
		}
예제 #8
0
		/// <summary>
		/// Ctor for leaves
		/// </summary>
		internal SuffixNode(SuffixNode parent, int from) :
			this(parent, from, -1)
		{
		}