Exemple #1
0
        public ZipResult ZipWith(StringPartition other)
        {
            int splitIndex = 0;

            using (IEnumerator <char> thisEnumerator = this.GetEnumerator())
                using (IEnumerator <char> otherEnumerator = other.GetEnumerator())
                {
                    while (thisEnumerator.MoveNext() && otherEnumerator.MoveNext())
                    {
                        if (thisEnumerator.Current != otherEnumerator.Current)
                        {
                            break;
                        }
                        splitIndex++;
                    }
                }

            SplitResult thisSplitted  = this.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));
        }
Exemple #2
0
 protected PatriciaTrieNode(StringPartition key, Queue <TValue> values,
                            Dictionary <char, PatriciaTrieNode <TValue> > children)
 {
     this.m_Values   = values;
     this.m_Key      = key;
     this.m_Children = children;
 }
Exemple #3
0
        public SplitResult Split(int splitAt)
        {
            var head = new StringPartition(this.m_Origin, this.m_StartIndex, splitAt);
            var rest = new StringPartition(this.m_Origin, this.m_StartIndex + splitAt, this.Length - splitAt);

            return(new SplitResult(head, rest));
        }
Exemple #4
0
        private void SplitOne(ZipResult zipResult, TValue value)
        {
            var leftChild = new PatriciaTrieNode <TValue>(zipResult.ThisRest, this.m_Values, this.m_Children);

            this.m_Children = new Dictionary <char, PatriciaTrieNode <TValue> >();
            this.m_Values   = new Queue <TValue>();
            this.AddValue(value);
            this.m_Key = zipResult.CommonHead;

            this.m_Children.Add(zipResult.ThisRest[0], leftChild);
        }
Exemple #5
0
        protected void GetOrCreateChild(StringPartition key, TValue value)
        {
            PatriciaTrieNode <TValue> child;

            if (!this.m_Children.TryGetValue(key[0], out child))
            {
                child = new PatriciaTrieNode <TValue>(key, value);
                this.m_Children.Add(key[0], child);
            }
            else
            {
                child.Add(key, value);
            }
        }
Exemple #6
0
        public bool StartsWith(StringPartition other)
        {
            if (this.Length < other.Length)
            {
                return(false);
            }

            for (int i = 0; i < other.Length; i++)
            {
                if (this[i] != other[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #7
0
        private void SplitTwo(ZipResult zipResult, TValue value)
        {
            var leftChild  = new PatriciaTrieNode <TValue>(zipResult.ThisRest, this.m_Values, this.m_Children);
            var rightChild = new PatriciaTrieNode <TValue>(zipResult.OtherRest, value);

            this.m_Children = new Dictionary <char, PatriciaTrieNode <TValue> >();
            this.m_Values   = new Queue <TValue>();
            this.m_Key      = zipResult.CommonHead;

            char leftKey = zipResult.ThisRest[0];

            this.m_Children.Add(leftKey, leftChild);
            char rightKey = zipResult.OtherRest[0];

            this.m_Children.Add(rightKey, rightChild);
        }
Exemple #8
0
        protected override TrieNodeBase <TValue> GetChildOrNull(string query, int position)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            PatriciaTrieNode <TValue> child;

            if (this.m_Children.TryGetValue(query[position], out child))
            {
                var queryPartition = new StringPartition(query, position, child.m_Key.Length);
                if (child.m_Key.StartsWith(queryPartition))
                {
                    return(child);
                }
            }
            return(null);
        }
Exemple #9
0
        internal virtual void Add(StringPartition keyRest, TValue value)
        {
            ZipResult zipResult = this.m_Key.ZipWith(keyRest);

            switch (zipResult.MatchKind)
            {
            case MatchKind.ExactMatch:
                this.AddValue(value);
                break;

            case MatchKind.IsContained:
                this.GetOrCreateChild(zipResult.OtherRest, value);
                break;

            case MatchKind.Contains:
                this.SplitOne(zipResult, value);
                break;

            case MatchKind.Partial:
                this.SplitTwo(zipResult, value);
                break;
            }
        }
Exemple #10
0
 public bool Equals(StringPartition other)
 {
     return(string.Equals(this.m_Origin, other.m_Origin) && this.m_PartitionLength == other.m_PartitionLength &&
            this.m_StartIndex == other.m_StartIndex);
 }
Exemple #11
0
 public ZipResult(StringPartition commonHead, StringPartition thisRest, StringPartition otherRest)
 {
     this.m_CommonHead = commonHead;
     this.m_ThisRest   = thisRest;
     this.m_OtherRest  = otherRest;
 }
Exemple #12
0
 internal override void Add(StringPartition keyRest, TValue value)
 {
     this.GetOrCreateChild(keyRest, value);
 }
Exemple #13
0
 protected PatriciaTrieNode(StringPartition key, TValue value)
     : this(key, new Queue <TValue>(new[] { value }), new Dictionary <char, PatriciaTrieNode <TValue> >())
 {
 }