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