Esempio n. 1
0
 protected PatriciaTrieNode(StringPartition key, Queue <TValue> values,
                            Dictionary <char, PatriciaTrieNode <TValue> > children)
 {
     this.qValues  = values;
     this.localKey = key;
     this.children = children;
 }
Esempio n. 2
0
        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));
        }
Esempio n. 3
0
        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));
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
            }
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
        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;
            }
        }
Esempio n. 10
0
 protected PatriciaTrieNode(StringPartition key, TValue value)
     : this(key, new Queue <TValue>(new[] { value }), new Dictionary <char, PatriciaTrieNode <TValue> >())
 {
 }
Esempio n. 11
0
 public ZipResult(StringPartition commonHead, StringPartition thisRest, StringPartition otherRest)
 {
     this.commonHead = commonHead;
     this.thisRest   = thisRest;
     this.otherRest  = otherRest;
 }
Esempio n. 12
0
 internal override void Add(StringPartition keyRest, TValue value)
 {
     GetOrCreateChild(keyRest, value);
 }
Esempio n. 13
0
 public bool Equals(StringPartition other)
 {
     return(string.Equals(stringOrigin, other.stringOrigin) && stringPartitionLength == other.stringPartitionLength &&
            stringStartIndex == other.stringStartIndex);
 }