public ZipResult ZipWith(StringPartition other) { int splitIndex = 0; using (IEnumerator <char> thisEnumerator = GetEnumerator()) using (IEnumerator <char> otherEnumerator = other.GetEnumerator()) { while (thisEnumerator.MoveNext() && otherEnumerator.MoveNext()) { if (thisEnumerator.Current != otherEnumerator.Current) { break; } splitIndex++; } } SplitResult thisSplitted = Split(splitIndex); SplitResult otherSplitted = other.Split(splitIndex); StringPartition commonHead = thisSplitted.Head; StringPartition restThis = thisSplitted.Rest; StringPartition restOther = otherSplitted.Rest; return(new ZipResult(commonHead, restThis, restOther)); }
protected PatriciaTrieNode(StringPartition key, Queue <TValue> values, Dictionary <char, PatriciaTrieNode <TValue> > children) { m_Values = values; m_Key = key; m_Children = children; }
public SplitResult Split(int splitAt) { var head = new StringPartition(m_Origin, m_StartIndex, splitAt); var rest = new StringPartition(m_Origin, m_StartIndex + splitAt, Length - splitAt); return(new SplitResult(head, rest)); }
internal virtual void Add(StringPartition keyRest, TValue value) { var zipResult = m_Key.ZipWith(keyRest); switch (zipResult.MatchKind) { case MatchKind.ExactMatch: AddValue(value); break; case MatchKind.IsContained: GetOrCreateChild(zipResult.OtherRest, value); break; case MatchKind.Contains: SplitOne(zipResult, value); break; case MatchKind.Partial: SplitTwo(zipResult, value); break; default: break; } }
private static IEnumerable <string> GetAllSuffixes(int minSuffixLength, string word) { for (int i = word.Length - minSuffixLength; i >= 0; i--) { var partition = new StringPartition(word, i); yield return(partition.ToString()); } }
private void SplitOne(ZipResult zipResult, TValue value) { var leftChild = new PatriciaTrieNode <TValue>(zipResult.ThisRest, m_Values, m_Children); m_Children = new Dictionary <char, PatriciaTrieNode <TValue> >(); m_Values = new Queue <TValue>(); AddValue(value); m_Key = zipResult.CommonHead; m_Children.Add(zipResult.ThisRest[0], leftChild); }
protected void GetOrCreateChild(StringPartition key, TValue value) { if (!m_Children.TryGetValue(key[0], out PatriciaTrieNode <TValue> child)) { child = new PatriciaTrieNode <TValue>(key, value); m_Children.Add(key[0], child); } else { child.Add(key, value); } }
public bool StartsWith(StringPartition other) { if (Length < other.Length) { return(false); } for (int i = 0; i < other.Length; i++) { if (this[i] != other[i]) { return(false); } } return(true); }
private void SplitTwo(ZipResult zipResult, TValue value) { var leftChild = new PatriciaTrieNode <TValue>(zipResult.ThisRest, m_Values, m_Children); var rightChild = new PatriciaTrieNode <TValue>(zipResult.OtherRest, value); m_Children = new Dictionary <char, PatriciaTrieNode <TValue> >(); m_Values = new Queue <TValue>(); m_Key = zipResult.CommonHead; char leftKey = zipResult.ThisRest[0]; m_Children.Add(leftKey, leftChild); char rightKey = zipResult.OtherRest[0]; m_Children.Add(rightKey, rightChild); }
protected override TrieNodeBase <TValue> GetChildOrNull(string query, int position) { if (query == null) { throw new ArgumentNullException("query"); } if (m_Children.TryGetValue(query[position], out PatriciaTrieNode <TValue> child)) { var queryPartition = new StringPartition(query, position, child.m_Key.Length); if (child.m_Key.StartsWith(queryPartition)) { return(child); } } return(null); }
internal override void Add(StringPartition keyRest, TValue value) { GetOrCreateChild(keyRest, value); }
public ZipResult(StringPartition commonHead, StringPartition thisRest, StringPartition otherRest) { m_CommonHead = commonHead; m_ThisRest = thisRest; m_OtherRest = otherRest; }
public bool Equals(StringPartition other) => string.Equals(m_Origin, other.m_Origin) && Length == other.Length && m_StartIndex == other.m_StartIndex;
public SplitResult(StringPartition head, StringPartition rest) { m_Head = head; m_Rest = rest; }
public bool Equals(StringPartition other) { return(string.Equals(m_Origin, other.m_Origin) && m_PartitionLength == other.m_PartitionLength && m_StartIndex == other.m_StartIndex); }
protected PatriciaTrieNode(StringPartition key, TValue value) : this(key, new Queue <TValue>(new[] { value }), new Dictionary <char, PatriciaTrieNode <TValue> >()) { }