コード例 #1
0
        public static int[] DelegCondition(int[] array, SearchCond cond)
        {
            var suitsTo = new List <int>();

            foreach (var element in array)
            {
                if (cond(element))
                {
                    suitsTo.Add(element);
                }
            }

            return(suitsTo.ToArray());
        }
コード例 #2
0
        /// <summary>  Recursively parse an XPath-like query.</summary>
        /// <param name="relativeTo">The SifElement this iteration is relative to. For the
        /// first call to this method, the <i>relativeTo</i> parameter is usually
        /// a SifDataObject such as StudentPersonal to which the XPath query
        /// string is relative. With each subsequent call it is the SifElement
        /// or SimpleField that was previously processed.</param>
        /// <param name="curSegment">The current segment of the path that is being processed.
        /// For the first call to this method, the <i>curSegment</i> should be
        /// the portion of the XPath query string up to the first forward slash,
        /// exclusive.</param>
        /// <param name="nextSegment">The remaining portion of the path to be processed.
        /// For the first call to this method, the <i>nextSegment</i> should be
        /// the portion of the XPath query string following the first forward
        /// slash.</param>
        /// <returns> The Element satisfying the query, or <c>null</c> if no
        /// match was found (unless the <i>create</i> parameter is true). If the
        /// query resolves to an attribute, a SimpleField object is returned. If
        /// it resolves to an element, a SifElement object is returned. In both
        /// cases the caller can obtain the text value of the attribute or
        /// element by calling its <c>getTextValue</c> method.
        /// </returns>
        private Element _xpath(SifElement relativeTo, string curSegment, string nextSegment)
        {
            SifElement nextEle = null;

            int attr = curSegment.IndexOf('@');
            int bracket = curSegment.IndexOf('[');
            if (bracket != -1)
            {
                if (attr == -1)
                {
                    throw new AdkSchemaException("Invalid query: \"" + curSegment +
                                                  "\" must be in the form [@Attribute='value']");
                }

                string subEleTag = curSegment.Substring(0, (bracket) - (0));
                SifElement subEle = relativeTo.GetChild(subEleTag);
                if (subEle == null)
                {
                    return null;
                }

                int endBracket = curSegment.IndexOf(']', bracket);
                if (bracket == -1)
                {
                    throw new AdkSchemaException("Invalid query: \"" + curSegment +
                                                  "\" must be in the form [@Attribute='value']");
                }

                List<SearchCond> condsList = new List<SearchCond>(10);
                string conds = curSegment.Substring(bracket + 1, (endBracket) - (bracket + 1));
                string[] tokens = conds.Split(',');
                foreach (string thisTok in tokens)
                {
                    if (thisTok[0] != '@')
                    {
                        throw new AdkSchemaException("Attribute names must be preceded with the @ character: " +
                                                      thisTok);
                    }

                    int eq = thisTok.IndexOf('=');
                    if (eq == -1)
                    {
                        throw new AdkSchemaException("Attribute value must be in the form [@Attribute='value']: " +
                                                      thisTok);
                    }

                    //  Lookup the referenced attribute
                    SimpleField attrEle = subEle.GetField(thisTok.Substring(1, (eq) - (1)));
                    if (attrEle == null)
                    {
                        return null;
                    }

                    //  Add the attribute/value to the list
                    string aval = _attrValue(thisTok.Substring(eq + 1));
                    SearchCond sc = new SearchCond(attrEle.ElementDef, aval);
                    condsList.Add(sc);
                }

                //  Search the parent's subEleTag children for matching attributes.
                //  All attributes in the condsList must match.
                SifElementList ch = relativeTo.GetChildList(subEleTag);
                int chLen = ch.Count;
                for (int i = 0; i < chLen && nextEle == null; i++)
                {
                    SifElement cmpEle;
                    cmpEle = ch[i];

                    int matched = 0;

                    //  Compare the attributes
                    for (int x = 0; x < condsList.Count; x++)
                    {
                        SearchCond sc = condsList[x];
                        SimpleField atr = cmpEle.GetField(sc.fAttr);
                        if (atr == null)
                        {
                            break;
                        }
                        if (atr.TextValue.Equals(sc.fValue))
                        {
                            matched++;
                        }
                    }

                    //  If all attributes matched, this is a match
                    if (matched == condsList.Count)
                    {
                        nextEle = cmpEle;

                        //  Continue the search if nextSegment has a value
                        if (nextSegment != null && nextSegment.Length > 0)
                        {
                            int ii = nextSegment.IndexOf('/');

                            Element ee =
                                _xpath(nextEle, ii == -1 ? nextSegment : nextSegment.Substring(0, (ii) - (0)),
                                        ii == -1 ? null : nextSegment.Substring(ii + 1));
                            if (ee != null)
                            {
                                return ee;
                            }
                            else
                            {
                                nextEle = null;
                            }
                        }
                    }
                }

                if (nextEle == null)
                {
                    return null;
                }
            }
            else
            {
                //  Search for the named attribute/element
                if (attr != -1)
                {
                    return relativeTo.GetField(curSegment.Substring(1));
                }
                else
                {
                    nextEle = relativeTo.GetChild(curSegment);
                    if (nextEle == null)
                    {
                        if (nextSegment == null || (nextSegment.Length > 0 && nextSegment[0] == '@'))
                        {
                            return relativeTo.GetField(curSegment);
                        }

                        return null;
                    }
                }
            }

            //  Continue the search if nextSegment has a value
            if ((nextSegment != null && nextSegment.Length > 0))
            {
                int i = nextSegment.IndexOf('/');

                return
                    _xpath(nextEle, i == -1 ? nextSegment : nextSegment.Substring(0, (i) - (0)),
                            i == -1 ? null : nextSegment.Substring(i + 1));
            }

            return nextEle;
        }
コード例 #3
0
        private static void Main()
        {
            Console.WriteLine("Integer array contains {0} elements. Number of measurements for each method: {1}", ArraySize, Iterations);

            var testArray = CreateIntArray(ArraySize);

            TimeSpan time = TimeSpan.Zero;

            for (int i = 0; i < Iterations; i++)
            {
                Stopwatch sw = Stopwatch.StartNew();
                Direct(testArray);
                sw.Stop();
                time += sw.Elapsed;
            }

            Console.WriteLine("average search time of direct search: {0}ms", time.TotalMilliseconds / Iterations);

            time = TimeSpan.Zero;
            SearchCond delegUsual = Cond;

            for (int i = 0; i < Iterations; i++)
            {
                Stopwatch sw = Stopwatch.StartNew();
                DelegCondition(testArray, delegUsual);
                sw.Stop();
                time += sw.Elapsed;
            }

            Console.WriteLine("average search time of search with condition provided through delegate: {0}ms", time.TotalMilliseconds / Iterations);

            time = TimeSpan.Zero;
            SearchCond delegAnon = delegate(int num) { return(num > 0); };

            for (int i = 0; i < Iterations; i++)
            {
                Stopwatch sw = Stopwatch.StartNew();
                DelegCondition(testArray, delegAnon);
                sw.Stop();
                time += sw.Elapsed;
            }

            Console.WriteLine("average search time of search with condition provided through delegate as anonymous method: {0}ms", time.TotalMilliseconds / Iterations);

            time = TimeSpan.Zero;
            SearchCond delegLambda = (x) => (x > 0);

            for (int i = 0; i < Iterations; i++)
            {
                Stopwatch sw = Stopwatch.StartNew();
                DelegCondition(testArray, delegLambda);
                sw.Stop();
                time += sw.Elapsed;
            }

            Console.WriteLine("average search time of search with condition provided through delegate as lambda expression: {0}ms", time.TotalMilliseconds / Iterations);

            time = TimeSpan.Zero;

            for (int i = 0; i < Iterations; i++)
            {
                Stopwatch sw = Stopwatch.StartNew();
                Linq(testArray);
                sw.Stop();
                time += sw.Elapsed;
            }

            Console.WriteLine("average search time of LINQ-expression search: {0}ms", time.TotalMilliseconds / Iterations);
            Console.ReadLine();
        }