protected PatriciaTrieNode(StringPartition key, Queue <TValue> values, Dictionary <char, PatriciaTrieNode <TValue> > children) { this.qValues = values; this.localKey = key; this.children = children; }
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)); }
public SplitResult Split(int splitAt) { var head = new StringPartition(stringOrigin, stringStartIndex, splitAt); var rest = new StringPartition(stringOrigin, stringStartIndex + splitAt, Length - splitAt); return(new SplitResult(head, rest)); }
private void SplitOne(ZipResult zipResult, TValue value) { var leftChild = new PatriciaTrieNode <TValue>(zipResult.ThisRest, qValues, children); children = new Dictionary <char, PatriciaTrieNode <TValue> >(); qValues = new Queue <TValue>(); AddValue(value); localKey = zipResult.CommonHead; children.Add(zipResult.ThisRest[0], leftChild); }
protected void GetOrCreateChild(StringPartition key, TValue value) { PatriciaTrieNode <TValue> child; if (!children.TryGetValue(key[0], out child)) { child = new PatriciaTrieNode <TValue>(key, value); children.Add(key[0], child); } else { child.Add(key, value); } }
private void SplitTwo(ZipResult zipResult, TValue value) { var leftChild = new PatriciaTrieNode <TValue>(zipResult.ThisRest, qValues, children); var rightChild = new PatriciaTrieNode <TValue>(zipResult.OtherRest, value); children = new Dictionary <char, PatriciaTrieNode <TValue> >(); qValues = new Queue <TValue>(); localKey = zipResult.CommonHead; var leftKey = zipResult.ThisRest[0]; children.Add(leftKey, leftChild); var rightKey = zipResult.OtherRest[0]; children.Add(rightKey, rightChild); }
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); }
protected override TrieNodeBase <TValue> GetChildOrNull(string query, int position) { if (query == null) { throw new ArgumentNullException("query"); } PatriciaTrieNode <TValue> child; if (children.TryGetValue(query[position], out child)) { var queryPartition = new StringPartition(query, position, child.localKey.Length); if (child.localKey.StartsWith(queryPartition)) { return(child); } } return(null); }
internal virtual void Add(StringPartition keyRest, TValue value) { var zipResult = localKey.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; } }
protected PatriciaTrieNode(StringPartition key, TValue value) : this(key, new Queue <TValue>(new[] { value }), new Dictionary <char, PatriciaTrieNode <TValue> >()) { }
public ZipResult(StringPartition commonHead, StringPartition thisRest, StringPartition otherRest) { this.commonHead = commonHead; this.thisRest = thisRest; this.otherRest = otherRest; }
internal override void Add(StringPartition keyRest, TValue value) { GetOrCreateChild(keyRest, value); }
public bool Equals(StringPartition other) { return(string.Equals(stringOrigin, other.stringOrigin) && stringPartitionLength == other.stringPartitionLength && stringStartIndex == other.stringStartIndex); }