Exemplo n.º 1
0
        /// <summary>
        /// Sorts the given row set in the order of the scheme.
        /// </summary>
        /// <param name="rowSet"></param>
        /// <remarks>
        /// The values in <paramref name="rowSet"/> must be references to rows in
        /// the domain of the table this scheme represents.
        /// <para>
        /// The returned set must be stable, meaning if values are equal they
        /// keep the same ordering.
        /// </para>
        /// <para>
        /// Note that the default implementation of this method can often be
        /// optimized. For example, <see cref="InsertSearch"/> uses a secondary
        /// RID list to sort items if the given list is over a certain size.
        /// </para>
        /// </remarks>
        /// <returns>
        /// Returns a <see cref="BlockIndex"/> that represents the given
        /// <paramref name="rowSet"/> sorted in the order of this scheme.
        /// </returns>
        public IIndex <long> GetOrderedIndex(IList <long> rowSet)
        {
            // The length of the set to order
            int rowSetLength = rowSet.Count;

            // Trivial cases where sorting is not required:
            // NOTE: We use readOnly objects to save some memory.
            if (rowSetLength == 0)
            {
                return(EmptyList);
            }
            if (rowSetLength == 1)
            {
                return(OneList);
            }

            // This will be 'row set' sorted by its entry lookup.  This must only
            // contain indices to rowSet entries.
            BlockIndex <long> newSet = new BlockIndex <long>();

            if (rowSetLength <= 250000)
            {
                // If the subset is less than or equal to 250,000 elements, we generate
                // an array in memory that contains all values in the set and we sort
                // it.  This requires use of memory from the heap but is faster than
                // the no heap use method.
                DataObject[] subsetList = new DataObject[rowSetLength];
                for (int i = 0; i < rowSetLength; ++i)
                {
                    subsetList[i] = GetCellContents(rowSet[i]);
                }

                // The comparator we use to sort
                IIndexComparer <long> comparator = new SubsetIndexComparer(subsetList);

                // Fill new_set with the set { 0, 1, 2, .... , row_set_length }
                for (int i = 0; i < rowSetLength; ++i)
                {
                    DataObject cell = subsetList[i];
                    newSet.InsertSort(cell, i, comparator);
                }
            }
            else
            {
                // This is the no additional heap use method to sorting the sub-set.

                // The comparator we use to sort
                IIndexComparer <long> comparator = new SchemeIndexComparer(this, rowSet);

                // Fill new_set with the set { 0, 1, 2, .... , row_set_length }
                for (int i = 0; i < rowSetLength; ++i)
                {
                    DataObject cell = GetCellContents(rowSet[i]);
                    newSet.InsertSort(cell, i, comparator);
                }
            }

            return(newSet);
        }
Exemplo n.º 2
0
        public IIndex <int> Order(IEnumerable <int> rows)
        {
            var rowSet = rows.ToList();

            // The length of the set to order
            int rowSetLength = rowSet.Count;

            // Trivial cases where sorting is not required:
            // NOTE: We use readOnly objects to save some memory.
            if (rowSetLength == 0)
            {
                return(EmptyList);
            }
            if (rowSetLength == 1)
            {
                return(OneList);
            }

            // This will be 'row set' sorted by its entry lookup.  This must only
            // contain indices to rowSet entries.
            var newSet = new BlockIndex <int>();

            if (rowSetLength <= 250000)
            {
                // If the subset is less than or equal to 250,000 elements, we generate
                // an array in memory that contains all values in the set and we sort
                // it.  This requires use of memory from the heap but is faster than
                // the no heap use method.
                var subsetList = new List <DataObject>(rowSetLength);
                foreach (long row in rowSet)
                {
                    subsetList.Add(GetValue(row));
                }

                // The comparator we use to sort
                var comparer = new SubsetIndexComparer(subsetList.ToArray());

                // Fill new_set with the set { 0, 1, 2, .... , row_set_length }
                for (int i = 0; i < rowSetLength; ++i)
                {
                    var cell = subsetList[i];
                    newSet.InsertSort(cell, i, comparer);
                }
            }
            else
            {
                // This is the no additional heap use method to sorting the sub-set.

                // The comparator we use to sort
                var comparer = new IndexComparer(this, rowSet);

                // Fill new_set with the set { 0, 1, 2, .... , row_set_length }
                for (int i = 0; i < rowSetLength; ++i)
                {
                    var cell = GetValue(rowSet[i]);
                    newSet.InsertSort(cell, i, comparer);
                }
            }

            return(newSet);
        }