예제 #1
0
        /// <summary>
        /// See this[]
        /// </summary>
        /// <param name="index">The index</param>
        /// <returns>The sub key pattern</returns>
        private KeyPattern GetAtIndex(BigInteger index)
        {
            //calculate the wildcard positions on which we want to split:
            int[] splittingPositions = new int[pattern.wildcardList.Count];
            for (int k = pattern.wildcardList.Count - 1; k >= 0; k--)
            {
                splittingPositions[k] = (int)(index % splittingQuotient[k]);
                index /= splittingQuotient[k];
            }
            Debug.Assert(index == 0);

            //split up the sub pattern parts:
            KeyPattern subpart = new KeyPattern(pattern.GetPattern());

            subpart.wildcardList = new ArrayList();
            for (int k = 0; k < pattern.wildcardList.Count; k++)
            {
                Wildcard subwc     = ((Wildcard)pattern.wildcardList[k]);
                char[]   values    = new char[256];
                int      sublength = subwc.size() / splittingQuotient[k];
                for (int i = 0; i < sublength; i++)
                {
                    values[i] = subwc.getChar(i + splittingPositions[k] * sublength);
                }
                Wildcard newwc = new Wildcard(values, sublength);
                subpart.wildcardList.Add(newwc);
            }

            return(subpart);
        }
예제 #2
0
        /**
         * tests, if 'wildcardKey' matches 'pattern'.
         **/
        public static bool testWildcardKey(string wildcardKey, string pattern)
        {
            try
            {
                int kcount = 0;
                int pcount = 0;
                while (kcount < wildcardKey.Length && pcount < pattern.Length)
                {
                    if (pattern[pcount] != '[')
                    {
                        if (pattern[pcount] != wildcardKey[kcount])
                        {
                            return(false);
                        }
                        kcount++;
                        pcount++;
                    }
                    else
                    {
                        Wildcard patternWc = new Wildcard(pattern.Substring(pcount, pattern.IndexOf(']', pcount) + 1 - pcount));
                        while (pattern[pcount++] != ']')
                        {
                            ;
                        }
                        Wildcard wildcardKeyWc = null;
                        if (wildcardKey[kcount] == '[')
                        {
                            wildcardKeyWc = new Wildcard(wildcardKey.Substring(kcount, wildcardKey.IndexOf(']', kcount) + 1 - kcount), patternWc);
                            if (wildcardKeyWc.getLength() == 0)
                            {
                                throw new Exception("Invalid wildcard pattern: wildcard must contain at least one character!");
                            }
                            while (wildcardKey[++kcount] != ']')
                            {
                                ;
                            }
                        }
                        else if (wildcardKey[kcount] != '*')
                        {
                            wildcardKeyWc = new Wildcard("" + wildcardKey[kcount]);
                        }

                        if (!patternWc.contains(wildcardKeyWc) && !(wildcardKey[kcount] == '*'))
                        {
                            return(false);
                        }
                        kcount++;
                    }
                }
                if (pcount != pattern.Length || kcount != wildcardKey.Length)
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #3
0
        public string getKey(int add)
        {
            string res           = "";
            int    div           = 1;
            int    wildcardCount = wildcardList.Count - 1;
            int    i             = pattern.Length - 1;

            while (i >= 0)
            {
                if (pattern[i] != ']')
                {
                    res += pattern[i--];
                }
                else
                {
                    Wildcard wc = (Wildcard)wildcardList[wildcardCount--];
                    if (add < div)
                    {
                        res += wc.getChar();
                    }
                    else
                    {
                        res += wc.getChar((add / div) % wc.size());
                        div *= wc.size();
                    }
                    while (pattern[i--] != '[')
                    {
                        ;
                    }
                }
            }
            char[] r = res.ToCharArray();
            Array.Reverse(r);
            return(new string(r));
        }
예제 #4
0
        private void TakeWildcardRangeFromReference(Wildcard referenceWildcard, char startChar, char endChar)
        {
            bool startFound = false;
            bool endFound   = false;

            for (int ri = 0; ri < referenceWildcard.length; ri++)
            {
                var currentRC = referenceWildcard.values[ri];
                if (!startFound && currentRC == startChar)
                {
                    startFound = true;
                }
                if (startFound && !endFound)
                {
                    values[length++] = currentRC;
                }
                if (currentRC == endChar)
                {
                    endFound = true;
                    break;
                }
            }

            if (!startFound || !endFound)
            {
                throw new Exception("Invalid wildcard format with respect to reference wildcard!");
            }
        }
예제 #5
0
        public Wildcard[] split()
        {
            if (length <= 1)
            {
                return(null);
            }
            int length1 = this.length - this.counter;

            Wildcard[] wcs = new Wildcard[2];
            wcs[0]         = new Wildcard();
            wcs[0].counter = 0;
            wcs[0].length  = length1 / 2;
            wcs[1]         = new Wildcard();
            wcs[1].counter = 0;
            wcs[1].length  = length1 - wcs[0].length;
            for (int i = 0; i < wcs[0].length; i++)
            {
                wcs[0].values[i] = values[this.counter + i];
            }
            for (int i = 0; i < wcs[1].length; i++)
            {
                wcs[1].values[i] = values[i + this.counter + wcs[0].length];
            }
            wcs[0].isSplit = true;
            wcs[1].isSplit = true;
            return(wcs);
        }
예제 #6
0
        /// <summary>
        /// Compares "wildcard" with "fullwildcard" and returns the movement of "wildcard", i.e. which intervals exists between the elements of "wildcard".
        /// </summary>
        /// <param name="wildcard"></param>
        /// <param name="fullwildcard"></param>
        /// <returns>The movements</returns>
        private IKeyMovement getWildcardMovement(Wildcard wildcard, Wildcard fullwildcard)
        {
            //check if linear:
            int a;
            int b;


            int i = 0;

            while (fullwildcard.getChars()[i] != wildcard.getChars()[0])
            {
                i++;
            }

            b = i;
            i++;

            while (fullwildcard.getChars()[i] != wildcard.getChars()[1])
            {
                i++;
            }

            a = i - b;

            bool linear = true;

            for (int c = 0; c < wildcard.getLength(); c++)
            {
                if (fullwildcard.getChars()[c * a + b] != wildcard.getChars()[c])
                {
                    linear = false;
                    break;
                }
            }

            if (linear)
            {
                return(new LinearKeyMovement(a, b, wildcard.getLength()));
            }

            //not linear, so just list the keys:
            List <int> keyList = new List <int>();

            for (int c = 0; c < wildcard.getLength(); c++)
            {
                for (int x = 0; x < fullwildcard.getLength(); x++)
                {
                    if (fullwildcard.getChars()[x] == wildcard.getChars()[c])
                    {
                        keyList.Add(x);
                    }
                }
            }

            return(new ListKeyMovement(keyList));
        }
예제 #7
0
 public Wildcard(Wildcard wc)
 {
     isSplit = wc.isSplit;
     length  = wc.length;
     counter = wc.counter;
     for (int i = 0; i < 256; i++)
     {
         values[i] = wc.values[i];
     }
 }
예제 #8
0
        public bool Contains(KeyPattern pattern)
        {
            if (pattern.wildcardList.Count != this.pattern.wildcardList.Count)
            {
                return(false);
            }
            if (pattern.GetPattern() != this.pattern.GetPattern())
            {
                return(false);
            }

            bool equal = true;

            for (int k = 0; k < pattern.wildcardList.Count; k++)
            {
                Wildcard wc     = ((Wildcard)pattern.wildcardList[k]);
                Wildcard thiswc = ((Wildcard)this.pattern.wildcardList[k]);
                if (wc.size() != (thiswc.size() / splittingQuotient[k]))
                {
                    return(false);
                }

                bool bolContains2 = true;
                int  begin        = equal ? splittingCounter[k] : 0;
                for (int j = begin; j < splittingQuotient[k]; j++)
                {
                    bool bolContains = true;
                    for (int i = 0; i < wc.size(); i++)
                    {
                        if (wc.getChar(i - wc.count()) != thiswc.getChar(i + j * wc.size()))
                        {
                            bolContains = false;
                            break;
                        }
                    }
                    if (bolContains)
                    {
                        equal        = (j == splittingCounter[k]);
                        bolContains2 = true;
                        break;
                    }
                }
                if (!bolContains2)
                {
                    return(false);
                }
            }
            return(!equal);
        }
예제 #9
0
        public Wildcard(string valuePattern, Wildcard referenceWildcard = null)
        {
            isSplit = false;
            counter = 0;
            if (valuePattern.Length == 1)
            {
                length    = 1;
                values[0] = valuePattern[0];
            }
            else
            {
                length = 0;
                int i = 1;
                while (valuePattern[i] != ']')
                {
                    if (valuePattern[i + 1] == '-')
                    {
                        var startChar = valuePattern[i];
                        var endChar   = valuePattern[i + 2];
                        if (startChar > endChar)
                        {
                            throw new Exception("Invalid wildcard format!");
                        }

                        if (referenceWildcard != null)
                        {
                            TakeWildcardRangeFromReference(referenceWildcard, startChar, endChar);
                        }
                        else
                        {
                            for (char c = startChar; c <= endChar; c++)
                            {
                                values[length++] = c;
                            }
                        }

                        i += 2;
                    }
                    else
                    {
                        values[length++] = valuePattern[i];
                    }
                    i++;
                }
            }

            Array.Sort(values, 0, length);
        }
예제 #10
0
        private void setWildcardKey(string wildcardKey)
        {
            int pcount = 0;

            wildcardList = new ArrayList();
            int i = 0;

            while (i < wildcardKey.Length)
            {
                if (pattern[pcount] == '[')
                {
                    Wildcard patternWc = new Wildcard(pattern.Substring(pcount, pattern.IndexOf(']', pcount) + 1 - pcount));

                    if (wildcardKey[i] == '*')
                    {
                        wildcardList.Add(patternWc);
                    }
                    else if (wildcardKey[i] == '[')
                    {
                        Wildcard wc = new Wildcard(wildcardKey.Substring(i, wildcardKey.IndexOf(']', i) + 1 - i), patternWc);
                        wildcardList.Add(wc);
                        while (wildcardKey[++i] != ']')
                        {
                            ;
                        }
                    }
                    else
                    {
                        Wildcard wc = new Wildcard("" + wildcardKey[i]);
                        wildcardList.Add(wc);
                    }
                    while (pattern[++pcount] != ']')
                    {
                        ;
                    }
                }
                pcount++;
                i++;
            }
        }
예제 #11
0
        public KeyPattern Pop()
        {
            if (stack.Count != 0)
            {
                counter++;
                return((KeyPattern)stack.Pop());
            }

            if (end)
            {
                return(null);
            }

            KeyPattern part = new KeyPattern(pattern.GetPattern());

            part.wildcardList = new ArrayList();
            for (int k = 0; k < pattern.wildcardList.Count; k++)
            {
                Wildcard wc     = ((Wildcard)pattern.wildcardList[k]);
                char[]   values = new char[256];
                int      length = wc.size() / splittingQuotient[k];
                for (int i = 0; i < length; i++)
                {
                    values[i] = wc.getChar(i + splittingCounter[k] * length);
                }
                Wildcard newwc = new Wildcard(values, length);
                part.wildcardList.Add(newwc);
            }

            if (!SuccCounter())
            {
                end = true;
            }

            counter++;
            return(part);
        }
예제 #12
0
        public KeyPattern[] split()
        {
            KeyPattern[] patterns = new KeyPattern[2];
            for (int i = 0; i < 2; i++)
            {
                patterns[i] = new KeyPattern(pattern);
                patterns[i].wildcardList = new ArrayList();
            }
            bool s = false;

            for (int i = 0; i < wildcardList.Count; i++)
            {
                Wildcard wc = ((Wildcard)wildcardList[i]);
                if (!s && (wc.size() - wc.count()) > 1)
                {
                    Wildcard[] wcs = wc.split();
                    patterns[0].wildcardList.Add(wcs[0]);
                    patterns[1].wildcardList.Add(wcs[1]);
                    s = true;
                }
                else
                {
                    patterns[0].wildcardList.Add(new Wildcard(wc));
                    Wildcard copy = new Wildcard(wc);
                    if (s)
                    {
                        copy.resetCounter();
                    }
                    patterns[1].wildcardList.Add(copy);
                }
            }
            if (!s)
            {
                return(null);
            }
            return(patterns);
        }
예제 #13
0
        public string getKey()
        {
            string res           = "";
            int    wildcardCount = 0;
            int    i             = 0;

            while (i < pattern.Length)
            {
                if (pattern[i] != '[')
                {
                    res += pattern[i++];
                }
                else
                {
                    Wildcard wc = (Wildcard)wildcardList[wildcardCount++];
                    res += wc.getChar();
                    while (pattern[i++] != ']')
                    {
                        ;
                    }
                }
            }
            return(res);
        }
예제 #14
0
 public bool contains(Wildcard wc)
 {
     if (wc == null)
     {
         return(false);
     }
     for (int i = 0; i < wc.length; i++)
     {
         bool contains = false;
         for (int j = 0; j < this.length; j++)
         {
             if (this.values[j] == wc.values[i])
             {
                 contains = true;
                 break;
             }
         }
         if (!contains)
         {
             return(false);
         }
     }
     return(true);
 }