Exemplo n.º 1
0
        public SearchResult1d SearchXBetween(double x, SearchResult1d result)
        {
            if (collection.Count == 0)
            {
                return(SearchResult1d.Empty);
            }

            int lastIndex = collection.Count - 1;

            if (x < collection[0].X)
            {
                return(SearchResult1d.Empty);
            }
            else if (collection[lastIndex].X < x)
            {
                return(SearchResult1d.Empty);
            }

            int startIndex = !result.IsEmpty ? Math.Min(result.Index, lastIndex) : 0;

            // searching ascending
            if (collection[startIndex].X < x)
            {
                for (int i = startIndex + 1; i <= lastIndex; i++)
                {
                    if (collection[i].X >= x)
                    {
                        return new SearchResult1d {
                                   Index = i - 1
                        }
                    }
                }
                ;
            }
            else             // searching descending
            {
                for (int i = startIndex - 1; i >= 0; i--)
                {
                    if (collection[i].X <= x)
                    {
                        return new SearchResult1d {
                                   Index = i
                        }
                    }
                }
                ;
            }

            throw new InvalidOperationException("Should not appear here.");
        }
        public SearchResult1d SearchBetween(TMember x, SearchResult1d result)
        {
            if (collection.Count == 0)
            {
                return(SearchResult1d.Empty);
            }

            int lastIndex = collection.Count - 1;

            if (x.CompareTo(selector(collection[0])) < 0)
            {
                return(SearchResult1d.Empty);
            }
            else if (selector(collection[lastIndex]).CompareTo(x) < 0)
            {
                return(SearchResult1d.Empty);
            }

            int startIndex = !result.IsEmpty ? Math.Min(result.Index, lastIndex) : 0;

            // searching ascending
            if (selector(collection[startIndex]).CompareTo(x) < 0)
            {
                for (int i = startIndex + 1; i <= lastIndex; i++)
                {
                    if (selector(collection[i]).CompareTo(x) >= 0)
                    {
                        return new SearchResult1d {
                                   Index = i - 1
                        }
                    }
                }
                ;
            }
            else             // searching descending
            {
                for (int i = startIndex - 1; i >= 0; i--)
                {
                    if (selector(collection[i]).CompareTo(x) <= 0)
                    {
                        return new SearchResult1d {
                                   Index = i
                        }
                    }
                }
                ;
            }

            throw new InvalidOperationException("Should not appear here.");
        }
        public SearchResult1d SearchFirstLess(TMember x)
        {
            if (collection.Count == 0)
            {
                return(SearchResult1d.Empty);
            }

            SearchResult1d result = SearchResult1d.Empty;

            for (int i = 0; i < collection.Count; i++)
            {
                if (selector(collection[i]).CompareTo(x) >= 0)
                {
                    result.Index = i;
                    break;
                }
            }

            return(result);
        }
        public SearchResult1d SearchGreater(TMember x)
        {
            if (collection.Count == 0)
            {
                return(SearchResult1d.Empty);
            }

            SearchResult1d result = SearchResult1d.Empty;

            for (int i = collection.Count - 1; i >= 0; i--)
            {
                if (selector(collection[i]).CompareTo(x) <= 0)
                {
                    result.Index = i;
                    break;
                }
            }

            return(result);
        }