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

            return(new SplitResult(head, rest));
        }
Пример #4
0
        internal virtual void Add(StringPartition keyRest, TValue value)
        {
            var zipResult = m_Key.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;

            default:
                break;
            }
        }
Пример #5
0
 private static IEnumerable <string> GetAllSuffixes(int minSuffixLength, string word)
 {
     for (int i = word.Length - minSuffixLength; i >= 0; i--)
     {
         var partition = new StringPartition(word, i);
         yield return(partition.ToString());
     }
 }
Пример #6
0
        private void SplitOne(ZipResult zipResult, TValue value)
        {
            var leftChild = new PatriciaTrieNode <TValue>(zipResult.ThisRest, m_Values, m_Children);

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

            m_Children.Add(zipResult.ThisRest[0], leftChild);
        }
Пример #7
0
 protected void GetOrCreateChild(StringPartition key, TValue value)
 {
     if (!m_Children.TryGetValue(key[0], out PatriciaTrieNode <TValue> child))
     {
         child = new PatriciaTrieNode <TValue>(key, value);
         m_Children.Add(key[0], child);
     }
     else
     {
         child.Add(key, value);
     }
 }
Пример #8
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);
        }
Пример #9
0
        private void SplitTwo(ZipResult zipResult, TValue value)
        {
            var leftChild  = new PatriciaTrieNode <TValue>(zipResult.ThisRest, m_Values, m_Children);
            var rightChild = new PatriciaTrieNode <TValue>(zipResult.OtherRest, value);

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

            char leftKey = zipResult.ThisRest[0];

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

            m_Children.Add(rightKey, rightChild);
        }
Пример #10
0
 protected override TrieNodeBase <TValue> GetChildOrNull(string query, int position)
 {
     if (query == null)
     {
         throw new ArgumentNullException("query");
     }
     if (m_Children.TryGetValue(query[position], out PatriciaTrieNode <TValue> child))
     {
         var queryPartition = new StringPartition(query, position, child.m_Key.Length);
         if (child.m_Key.StartsWith(queryPartition))
         {
             return(child);
         }
     }
     return(null);
 }
Пример #11
0
 internal override void Add(StringPartition keyRest, TValue value)
 {
     GetOrCreateChild(keyRest, value);
 }
Пример #12
0
 public ZipResult(StringPartition commonHead, StringPartition thisRest, StringPartition otherRest)
 {
     m_CommonHead = commonHead;
     m_ThisRest   = thisRest;
     m_OtherRest  = otherRest;
 }
Пример #13
0
 public bool Equals(StringPartition other)
 => string.Equals(m_Origin, other.m_Origin) && Length == other.Length &&
 m_StartIndex == other.m_StartIndex;
Пример #14
0
 public SplitResult(StringPartition head, StringPartition rest)
 {
     m_Head = head;
     m_Rest = rest;
 }
Пример #15
0
 public bool Equals(StringPartition other)
 {
     return(string.Equals(m_Origin, other.m_Origin) && m_PartitionLength == other.m_PartitionLength &&
            m_StartIndex == other.m_StartIndex);
 }
Пример #16
0
 protected PatriciaTrieNode(StringPartition key, TValue value)
     : this(key, new Queue <TValue>(new[] { value }), new Dictionary <char, PatriciaTrieNode <TValue> >())
 {
 }