/// <summary> /// Get word's longest suffix and value /// </summary> /// <param name="word"> /// A <see cref="String"/> /// </param> /// <returns> /// A <see cref="Pair`2"/> /// </returns> public KeyValuePair <string, T>?getLongestSuffixAndValue(string word) { SuffixTreeNode <T> cnode = root; int longestSuffixIndex = -1; T valueToReturn = default(T); char c; for (int i = word.Length - 1; i >= 0; i--) { c = word[i]; cnode = cnode[c]; if (cnode != null) { if (!cnode.ValueIsNull) { longestSuffixIndex = i; valueToReturn = cnode.Value; } } else { break; } } if (longestSuffixIndex != -1) { return(new KeyValuePair <string, T>(word.Substring(longestSuffixIndex), valueToReturn)); } return(null); }
/// <summary> /// Add suffix to the Suffix Tree /// </summary> /// <param name="suffix"> /// A <see cref="String"/> /// </param> /// <param name="val"> /// A <see cref="T"/> /// </param> public void addSuffix(string suffix, T val) { SuffixTreeNode <T> node = root; char c; for (int i = suffix.Length - 1; i >= 0; i--) { c = suffix[i]; node = node.addEdge(c); } node.ValueIsNull = false; node.Value = val; }
private SuffixTreeNode <T> addEdge(char c, T val, bool isNull) { SuffixTreeNode <T> node; if (!edges.TryGetValue(c, out node)) { node = new SuffixTreeNode <T>(val); edges.TryAdd(c, node); if (isNull) { node.isNull = true; } } return(node); }
/// <summary> /// Checks if Suffix Tree contains word /// </summary> /// <param name="word"> /// A <see cref="String"/> /// </param> /// <returns> /// A <see cref="System.Boolean"/> /// </returns> public bool contains(string word) { SuffixTreeNode <T> cnode = root; char c; for (int i = word.Length - 1; i >= 0; i--) { c = word[i]; cnode = cnode[c]; if (cnode == null) { return(false); } } if (cnode != null && !cnode.ValueIsNull) { return(true); } return(false); }
/// <summary> /// Get all the suffixes in the word and their values /// </summary> /// <param name="word"> /// A <see cref="String"/> /// </param> /// <returns> /// A <see cref="List`1"/> /// </returns> public ConcurrentDictionary <string, T> getLongestSuffixesAndValues(string word) { char c; SuffixTreeNode <T> cnode = root; ConcurrentDictionary <string, T> res = new ConcurrentDictionary <string, T>(); for (int i = word.Length - 1; i >= 0; i--) { c = word[i]; cnode = cnode[c]; if (cnode != null) { if (!cnode.ValueIsNull) { res.TryAdd(word.Substring(i), cnode.Value); } } else { break; } } return(res); }
public SuffixTree() { root = new SuffixTreeNode <T>(); properties = new ConcurrentDictionary <string, int>(); }