Example #1
0
        public long SearchFirst(TKey key, ISortComparer <TKey, TValue> comparer)
        {
            int  blockNum = FindFirstBlock(key, comparer);
            long sr;

            if (blockNum < 0)
            {
                // Guarenteed not found in any blocks so return start of insert block
                blockNum = (-(blockNum + 1));                 // - 1;
                sr       = -1;
            }
            else
            {
                // We got a block, so find out if it's in the block or not.
                var block = Blocks[blockNum];

                // Try and find it in the block,
                sr = block.SearchFirst(key, comparer);
            }

            long offset = 0;

            for (int i = 0; i < blockNum; ++i)
            {
                var block = Blocks[i];
                offset += block.Count;
            }

            return(sr >= 0 ? offset + sr : -offset + sr);
        }
            /// <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.
            }
Example #3
0
        public TValue RemoveSort(TKey key, TValue value, ISortComparer <TKey, TValue> comparer)
        {
            CheckImmutable();

            // Find the range of blocks that the value is in.
            int origBlockIndex = FindFirstBlock(key, comparer);
            int blockIndex     = origBlockIndex;
            int lastBlockIndex = Blocks.Count - 1;

            if (blockIndex < 0)
            {
                // Not found in a block,
                throw new InvalidOperationException("Value (" + key + ") was not found in the list.");
            }

            var block = Blocks[blockIndex];
            var i     = block.IndexOf(value);

            while (i == -1)
            {
                // If not found, go to next block
                ++blockIndex;
                if (blockIndex > lastBlockIndex)
                {
                    throw new InvalidOperationException("Value (" + key + ") was not found in the list.");
                }

                block = Blocks[blockIndex];
                // Try and find the value within this block
                i = block.IndexOf(value);
            }

            // Remove value from the block,
            return(RemoveFromBlock(blockIndex, block, i));
        }
Example #4
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 #5
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.
        }
Example #6
0
        public InsertSearchIndex(IndexInfo indexInfo, ITable table, IEnumerable <long> rows)
            : base(indexInfo, table)
        {
            comparer = new IndexComparer(this);
            list     = new SortedCollection <IndexKey, long>();

            if (rows != null)
            {
                foreach (var row in rows)
                {
                    list.Add(row);
                }
            }
        }
Example #7
0
        public bool Contains(TKey key, ISortComparer <TKey, TValue> comparer)
        {
            int blockIndex = FindBlockContaining(key, comparer);

            if (blockIndex < 0)
            {
                // We didn't find in the list, so return false.
                return(false);
            }

            // We got a block, so find out if it's in the block or not.
            var block = Blocks[blockIndex];

            // Find, if not there then return false.
            return(block.BinarySearch(key, comparer) >= 0);
        }
Example #8
0
        public SortableBindingList(IEnumerable <T> contents, ISortComparer <T> comparer)
        {
            if (contents != null)
            {
                AddList(contents);
            }

            if (comparer == null)
            {
                SortComparer = new GenericSortComparer <T>();
            }
            else
            {
                SortComparer = comparer;
            }
        }
 public static void Sort(this int[][] targetArray, ISortComparer comparer, ISorter sorter)
 {
     if (targetArray == null)
     {
         throw new ArgumentNullException("targetArray");
     }
     if (comparer == null)
     {
         throw new ArgumentNullException("comparer");
     }
     if (sorter == null)
     {
         throw new ArgumentNullException("sorter");
     }
     sorter.Sort(targetArray, comparer.Compare);
 }
Example #10
0
        /// <summary>
        /// Sorts the ListView by the specified column.
        /// </summary>
        /// <param name="column"></param>
        public void Sort(int column)
        {
            // Toggle ASC and DESC
            if (column == lastsortcolumn)
            {
                if (sortorder == VelerSoftware.SZC.ListViewStored.Collections.SortOrder.Ascending)
                {
                    sortorder = VelerSoftware.SZC.ListViewStored.Collections.SortOrder.Descending;
                }
                else
                {
                    sortorder = VelerSoftware.SZC.ListViewStored.Collections.SortOrder.Ascending;
                }
            }
            else
            {
                sortorder = VelerSoftware.SZC.ListViewStored.Collections.SortOrder.Ascending;
            }

            lastsortcolumn = column;

            // Get the columns comparer (if the column ist registered use the StringComparer by default)
            ISortComparer c = null;

            if (comparercollection.ContainsKey(this.ListView.Columns[column].Text))
            {
                c = comparercollection[this.ListView.Columns[column].Text];
            }
            else
            {
                c = new VelerSoftware.SZC.ListViewStored.Collections.NameComparer();
            }

            // Initialize the ListViewItemComparer
            ListViewItemComparer lvc = new ListViewItemComparer(column, c);

            lvc.SortOrder = sortorder;
            this.ListView.ListViewItemSorter = lvc;

            // Sort!
            this.ListView.Sort();

            // Set ColumnHeaders image
            SetImage(column, sortorder);
        }
            /// <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.
            }
Example #12
0
        public void InsertSort(TKey key, TValue value, ISortComparer <TKey, TValue> comparer)
        {
            CheckImmutable();

            int blockIndex = FindLastBlock(key, comparer);

            if (blockIndex < 0)
            {
                // Not found a block,
                // The block to insert the value,
                blockIndex = (-(blockIndex + 1)) - 1;
                if (blockIndex < 0)
                {
                    blockIndex = 0;
                }
            }

            // We got a block, so find out if it's in the block or not.
            var block = Blocks[blockIndex];

            // The point to insert in the block,
            var i = block.SearchLast(key, comparer);

            if (i < 0)
            {
                i = -(i + 1);
            }
            else
            {
                i = i + 1;
                // NOTE: A block can never become totally full so it's always okay to
                //   skip one ahead.
            }

            // Insert value into the block,
            InsertIntoBlock(value, blockIndex, block, i);
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="column"></param>
 /// <param name="comparer"></param>
 public ListViewItemComparer(int column, ISortComparer comparer)
 {
     col           = column;
     this.comparer = comparer;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="comparer"></param>
 public ListViewItemComparer(ISortComparer comparer)
 {
     col           = 0;
     this.comparer = comparer;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="column"></param>
 /// <param name="comparer"></param>
 public ListViewItemComparer(int column, ISortComparer comparer)
 {
     col = column;
       this.comparer = comparer;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="comparer"></param>
 public ListViewItemComparer(ISortComparer comparer)
 {
     col = 0;
       this.comparer = comparer;
 }