Example #1
0
        /// <summary>
        /// Uses a binary search algorithm to quickly determine the collection of the
        /// <see cref="ICollectionBlock{TKey,TValue}"/> within 'blocks' of the block
        /// that contains the given key value using the <see cref="ISortComparer{TKey,TValue}"/>
        /// as a lookup comparator.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        private int FindBlockContaining(TKey key, ISortComparer <TKey, TValue> comparer)
        {
            if (Count == 0)
            {
                return(-1);
            }

            int low  = 0;
            int high = Blocks.Count - 1;

            while (low <= high)
            {
                int mid   = (low + high) / 2;
                var block = Blocks[mid];

                // Is what we are searching for lower than the bottom value?
                if (comparer.Compare(block.Bottom, key) > 0)
                {
                    high = mid - 1;
                }
                // No, then is it greater than the highest value?
                else if (comparer.Compare(block.Top, key) < 0)
                {
                    low = mid + 1;
                }
                // Must be inside this block then!
                else
                {
                    return(mid);
                }
            }

            return(-(low + 1));            // key not found.
        }
Example #2
0
        /// <summary>
        /// Uses a binary search algorithm to quickly determine the collection of
        /// the <see cref="ICollectionBlock{TKey,TValue}"/> within 'blocks' of the block
        /// that contains the given key value using the <see cref="ISortComparer{TKey,TValue}"/>
        /// as a lookup comparator.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        private int FindLastBlock(TKey key, ISortComparer <TKey, TValue> comparer)
        {
            if (Count == 0)
            {
                return(-1);
            }

            int low  = 0;
            int high = Blocks.Count - 1;

            while (low <= high)
            {
                if (high - low <= 2)
                {
                    for (int i = high; i >= low; --i)
                    {
                        var block1 = Blocks[i];
                        if (comparer.Compare(block1.Bottom, key) <= 0)
                        {
                            if (comparer.Compare(block1.Top, key) >= 0)
                            {
                                return(i);
                            }
                            return(-(i + 1) - 1);
                        }
                    }
                    return(-(low + 1));
                }

                int mid   = (low + high) / 2;
                var block = Blocks[mid];

                // Is what we are searching for lower than the bottom value?
                if (comparer.Compare(block.Bottom, key) > 0)
                {
                    high = mid - 1;
                }
                // No, then is it greater than the highest value?
                else if (comparer.Compare(block.Top, key) < 0)
                {
                    low = mid + 1;
                }
                // Equal, so highest must be someplace between mid and high.
                else
                {
                    low = mid;
                    if (low == high)
                    {
                        return(low);
                    }
                }
            }

            return(-(low + 1));            // key not found.
        }
            /// <inheritdoc/>
            public long BinarySearch(TKey key, ISortComparer <TKey, TValue> comparer)
            {
                var  arr  = GetArray(true);
                long low  = 0;
                long high = count - 1;

                while (low <= high)
                {
                    var mid = (low + high) / 2;
                    int cmp = comparer.Compare(arr[mid], key);

                    if (cmp < 0)
                    {
                        low = mid + 1;
                    }
                    else if (cmp > 0)
                    {
                        high = mid - 1;
                    }
                    else
                    {
                        return(mid);                        // key found
                    }
                }
                return(-(low + 1));                // key not found.
            }
            /// <inheritdoc/>
            public long SearchFirst(TKey key, ISortComparer <TKey, TValue> comparer)
            {
                var  arr  = GetArray(true);
                long low  = 0;
                long high = count - 1;

                while (low <= high)
                {
                    if (high - low <= 2)
                    {
                        for (var i = low; i <= high; ++i)
                        {
                            int cmp1 = comparer.Compare(arr[i], key);
                            if (cmp1 == 0)
                            {
                                return(i);
                            }

                            if (cmp1 > 0)
                            {
                                return(-(i + 1));
                            }
                        }

                        return(-(high + 2));
                    }

                    var mid = (low + high) / 2;
                    int cmp = comparer.Compare(arr[mid], key);

                    if (cmp < 0)
                    {
                        low = mid + 1;
                    }
                    else if (cmp > 0)
                    {
                        high = mid - 1;
                    }
                    else
                    {
                        high = mid;
                    }
                }
                return(-(low + 1));                // key not found.
            }
        /// <summary>
        /// Compare!
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public override int Compare(object x, object y)
        {
            string x1 = ((ListViewItem)x).SubItems[col].Text;
            string y1 = ((ListViewItem)y).SubItems[col].Text;

            if (comparer == null)
            {
                // Default is the StringComparer
                ISortComparer c = new Yaowi.Common.Collections.StringComparer();
                c.SortOrder = sortorder;
                return(c.Compare(x1, y1));
            }
            else
            {
                comparer.SortOrder = sortorder;
                return(comparer.Compare(x1, y1));
            }
        }