示例#1
0
        public ZipResult ZipWith(StringPartition other)
        {
            var splitIndex = 0;

            using (var thisEnumerator = GetEnumerator())
                using (var otherEnumerator = other.GetEnumerator())
                {
                    while (thisEnumerator.MoveNext() && otherEnumerator.MoveNext())
                    {
                        if (thisEnumerator.Current != otherEnumerator.Current)
                        {
                            break;
                        }
                        splitIndex++;
                    }
                }

            var thisSplitted  = Split(splitIndex);
            var otherSplitted = other.Split(splitIndex);

            var commonHead = thisSplitted.Head;
            var restThis   = thisSplitted.Rest;
            var restOther  = otherSplitted.Rest;

            return(new ZipResult(commonHead, restThis, restOther));
        }
示例#2
0
 protected PatriciaTrieNode(StringPartition key, Queue <TValue> values,
                            Dictionary <char, PatriciaTrieNode <TValue> > children)
 {
     mValues   = values;
     mKey      = key;
     mChildren = children;
 }
示例#3
0
        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));
        }
示例#4
0
        public bool StartsWith(StringPartition other)
        {
            if (Length < other.Length)
            {
                return(false);
            }

            return(!other.Where((t, i) => this[i] != t).Any());
        }
示例#5
0
        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);
        }
示例#6
0
 protected void GetOrCreateChild(StringPartition key, TValue value)
 {
     if (!mChildren.TryGetValue(key[0], out var child))
     {
         child = new PatriciaTrieNode <TValue>(key, value);
         mChildren.Add(key[0], child);
     }
     else
     {
         child.Add(key, value);
     }
 }
示例#7
0
        protected override TrieNodeBase <TValue> GetChildOrNull(string query, int position)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            if (!mChildren.TryGetValue(query[position], out var child))
            {
                return(null);
            }
            var queryPartition = new StringPartition(query, position, child.mKey.Length);

            return(child.mKey.StartsWith(queryPartition) ? child : null);
        }
示例#8
0
        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);
        }
示例#9
0
        internal virtual void Add(StringPartition keyRest, TValue value)
        {
            var 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;
            }
        }
示例#10
0
 protected PatriciaTrieNode(StringPartition key, TValue value)
     : this(key, new Queue <TValue>(new[] { value }), new Dictionary <char, PatriciaTrieNode <TValue> >())
 {
 }
示例#11
0
 public bool Equals(StringPartition other)
 {
     return(string.Equals(mOrigin, other.mOrigin) && Length == other.Length &&
            mStartIndex == other.mStartIndex);
 }
示例#12
0
 internal override void Add(StringPartition keyRest, TValue value)
 {
     GetOrCreateChild(keyRest, value);
 }
示例#13
0
 public ZipResult(StringPartition commonHead, StringPartition thisRest, StringPartition otherRest)
 {
     CommonHead = commonHead;
     ThisRest   = thisRest;
     OtherRest  = otherRest;
 }