public static void BubbleDown <T>(this T[] source, int length, int index, IDataComparer <T> comparer)
        {
            int curIdx = index;

            while (curIdx < length)
            {
                int leftIndex = LeftIndex(curIdx);
                if (leftIndex >= length)
                {
                    return;
                }

                int rightIndex  = RightIndex(curIdx);
                int maxChildIdx = leftIndex;
                if (rightIndex < length && comparer.Compare(source[rightIndex], source[leftIndex]).IsFirstMore)
                {
                    maxChildIdx = rightIndex;
                }

                if (comparer.Compare(source[maxChildIdx], source[curIdx]).IsFirstMore)
                {
                    source.Swap(curIdx, maxChildIdx);
                    curIdx = maxChildIdx;
                    continue;
                }
                return;
            }
        }
예제 #2
0
            int IComparer.Compare(object x, object y)
            {
                object key  = ((FilterKey)x).Key;
                object key2 = ((FilterKey)y).Key;

                return(m_comparer.Compare(key, key2));
            }
예제 #3
0
 public static T[] ApplyInsertionSort <T>(this T[] source, IDataComparer <T> comparer)
 {
     for (int idx = 1; idx < source.Length; idx++)
     {
         T   currentItem = source[idx];
         int prev        = idx - 1;
         while (prev >= 0 && comparer.Compare(source[prev], currentItem).IsFirstMore)
         {
             source[prev + 1] = source[prev];
             prev--;
         }
         source[prev + 1] = currentItem;
     }
     return(source);
 }
        public static T[] ApplySelectionSort <T>(this T[] source, IDataComparer <T> comparer)
        {
            for (int swapIdx = 0; swapIdx < source.Length - 1; swapIdx++)
            {
                int minIdx = swapIdx;
                for (int idx = swapIdx + 1; idx < source.Length; idx++)
                {
                    var item1 = source[minIdx];
                    var item2 = source[idx];
                    if (comparer.Compare(item1, item2).IsFirstMore)
                    {
                        minIdx = idx;
                    }
                }

                source.Swap(swapIdx, minIdx);
            }
            return(source);
        }
예제 #5
0
        public static T[] ApplyBubbleSort <T>(this T[] source, IDataComparer <T> comparer)
        {
            bool isSorted     = false;
            int  lastUnsorted = source.Length - 1;

            while (!isSorted)
            {
                isSorted = true;
                for (int idx = 0; idx < lastUnsorted; idx++)
                {
                    var item1 = source[idx];
                    var item2 = source[idx + 1];
                    if (comparer.Compare(item1, item2).IsFirstMore)
                    {
                        source.Swap(idx, idx + 1);
                        isSorted = false;
                    }
                }
                lastUnsorted--;
            }
            return(source);
        }
예제 #6
0
        internal override void Update(object[] expressions, IErrorContext iErrorContext)
        {
            object obj = expressions[0];

            Microsoft.ReportingServices.ReportProcessing.DataAggregate.DataTypeCode typeCode = DataAggregate.GetTypeCode(obj);
            if (DataAggregate.IsNull(typeCode))
            {
                return;
            }
            if (!DataAggregate.IsVariant(typeCode) || DataTypeUtility.IsSpatial(typeCode))
            {
                iErrorContext.Register(ProcessingErrorCode.rsMinMaxOfNonSortableData, Severity.Warning);
                throw new ReportProcessingException(ErrorCode.rsInvalidOperation);
            }
            if (m_currentMin == null)
            {
                m_currentMin     = obj;
                m_expressionType = typeCode;
                return;
            }
            bool validComparisonResult;
            int  num = m_comparer.Compare(m_currentMin, obj, throwExceptionOnComparisonFailure: false, extendedTypeComparisons: false, out validComparisonResult);

            if (!validComparisonResult)
            {
                if (typeCode != m_expressionType)
                {
                    iErrorContext.Register(ProcessingErrorCode.rsAggregateOfMixedDataTypes, Severity.Warning);
                    throw new ReportProcessingException(ErrorCode.rsInvalidOperation);
                }
                iErrorContext.Register(ProcessingErrorCode.rsMinMaxOfNonSortableData, Severity.Warning);
            }
            else if (num > 0)
            {
                m_currentMin     = obj;
                m_expressionType = typeCode;
            }
        }
예제 #7
0
        public static void MergeByMinElements <T>(this T[] arr, int start, int split, int end, IDataComparer <T> comparer)
        {
            var buffer    = new T[end - start + 1];
            var firstIdx  = start;
            var secondIdx = split + 1;
            int resIdx    = 0;

            while (firstIdx <= split && secondIdx <= end)
            {
                if (comparer.Compare(arr[firstIdx], arr[secondIdx]).IsFirstMore)
                {
                    buffer[resIdx] = arr[secondIdx];
                    secondIdx++;
                }
                else
                {
                    buffer[resIdx] = arr[firstIdx];
                    firstIdx++;
                }
                resIdx++;
            }

            while (firstIdx <= split)
            {
                buffer[resIdx] = arr[firstIdx];
                firstIdx++;
                resIdx++;
            }

            while (secondIdx <= end)
            {
                buffer[resIdx] = arr[secondIdx];
                secondIdx++;
                resIdx++;
            }

            buffer.CopyTo(arr, start);
        }
예제 #8
0
        public static bool BinarySearch <T>(this T[] arr, T item, IDataComparer <T> comparer)
        {
            int left  = 0;
            int right = arr.Length - 1;

            while (left <= right)
            {
                int middle        = (right - left) / 2 + left;
                var compareResult = comparer.Compare(item, arr[middle]);
                if (compareResult.IsEqual)
                {
                    return(true);
                }
                if (compareResult.IsFirstLess)
                {
                    right = middle - 1;
                }
                if (compareResult.IsFirstMore)
                {
                    left = middle + 1;
                }
            }
            return(false);
        }