Ejemplo n.º 1
0
        CheckSort <TSource, TKey>(IEnumerable <TSource> source,
                                  Expression <Func <TSource, TKey> > keySelector,
                                  IComparer <TKey> comparer,
                                  bool isDescending)
        {
            Func <TSource, TKey> keySel = keySelector.Compile();

            comparer = TypeSystem.GetComparer <TKey>(comparer);

            IEnumerator <TSource> elems = source.GetEnumerator();

            if (elems.MoveNext())
            {
                TSource curElem = elems.Current;
                yield return(curElem);

                TKey curKey = keySel(curElem);
                while (elems.MoveNext())
                {
                    TSource nextElem = elems.Current;
                    yield return(nextElem);

                    TKey nextKey = keySel(nextElem);
                    int  cmp     = comparer.Compare(curKey, nextKey);
                    int  cmpRes  = (isDescending) ? -cmp : cmp;
                    if (cmpRes > 0)
                    {
                        throw new DryadLinqException(SR.SourceNotOrdered);
                    }
                    curKey = nextKey;
                }
            }
        }
Ejemplo n.º 2
0
        MergeSort <TSource, TKey>(IEnumerable <TSource>[] sources,
                                  Func <TSource, TKey> keySelector,
                                  IComparer <TKey> comparer,
                                  bool isDescending)
        {
            comparer = TypeSystem.GetComparer <TKey>(comparer);
            IEnumerable <TSource>[] currentLayer = sources;
            int currentLayerCount = sources.Length;

            IEnumerable <TSource>[] nextLayer = new IEnumerable <TSource> [currentLayerCount / 2 + 1];
            while (currentLayerCount != 1)
            {
                int nextLayerCount = currentLayerCount / 2;
                int idx            = 0;
                for (int i = 0; i < nextLayerCount; i++)
                {
                    nextLayer[i] = BinaryMergeSort <TSource, TKey>(currentLayer[idx],
                                                                   currentLayer[idx + 1],
                                                                   keySelector,
                                                                   comparer,
                                                                   isDescending);
                    idx += 2;
                }
                if (idx < currentLayerCount)
                {
                    nextLayer[nextLayerCount] = currentLayer[idx];
                    nextLayerCount++;
                }
                currentLayer      = nextLayer;
                currentLayerCount = nextLayerCount;
            }
            return(currentLayer[0]);
        }
Ejemplo n.º 3
0
        BinaryMergeSort <TSource, TKey>(IEnumerable <TSource> source1,
                                        IEnumerable <TSource> source2,
                                        Func <TSource, TKey> keySelector,
                                        IComparer <TKey> comparer,
                                        bool isDescending)
        {
            comparer = TypeSystem.GetComparer <TKey>(comparer);

            IEnumerator <TSource> leftElems  = source1.GetEnumerator();
            IEnumerator <TSource> rightElems = source2.GetEnumerator();

            if (leftElems.MoveNext())
            {
                if (rightElems.MoveNext())
                {
                    TKey leftKey  = keySelector(leftElems.Current);
                    TKey rightKey = keySelector(rightElems.Current);
                    while (true)
                    {
                        int cmp    = comparer.Compare(leftKey, rightKey);
                        int cmpRes = (isDescending) ? -cmp : cmp;
                        if (cmpRes > 0)
                        {
                            yield return(rightElems.Current);

                            if (!rightElems.MoveNext())
                            {
                                yield return(leftElems.Current);

                                break;
                            }
                            rightKey = keySelector(rightElems.Current);
                        }
                        else
                        {
                            yield return(leftElems.Current);

                            if (!leftElems.MoveNext())
                            {
                                yield return(rightElems.Current);

                                leftElems = rightElems;
                                break;
                            }
                            leftKey = keySelector(leftElems.Current);
                        }
                    }
                }
            }
            else
            {
                leftElems = rightElems;
            }

            while (leftElems.MoveNext())
            {
                yield return(leftElems.Current);
            }
        }
Ejemplo n.º 4
0
 internal RangePartition(LambdaExpression keySelector,
                         IComparer <TKey> comparer,
                         bool isDescending,
                         Int32 parCnt)
     : base(PartitionType.Range)
 {
     this.m_count         = parCnt;
     this.m_keySelector   = keySelector;
     this.m_partitionKeys = null;
     this.m_comparer      = TypeSystem.GetComparer <TKey>(comparer);
 }
Ejemplo n.º 5
0
        internal override bool IsPartitionedBy(LambdaExpression keySel, object comp)
        {
            // Match the key selector functions:
            if (!this.IsPartitionedBy(keySel))
            {
                return(false);
            }

            // Check the comparers:
            IComparer <TKey> comp1 = TypeSystem.GetComparer <TKey>(comp);

            if (comp1 == null)
            {
                return(false);
            }
            return(this.m_comparer.Equals(comp1));
        }
Ejemplo n.º 6
0
        // Check if the array is ordered.
        internal static bool IsOrdered <T>(T[] array, IComparer <T> comparer, bool isDescending)
        {
            comparer = TypeSystem.GetComparer <T>(comparer);
            if (array.Length < 2)
            {
                return(true);
            }
            T elem = array[0];

            for (int i = 1; i < array.Length; i++)
            {
                int cmp    = comparer.Compare(elem, array[i]);
                int cmpRes = (isDescending) ? -cmp : cmp;
                if (cmpRes > 0)
                {
                    return(false);
                }
                elem = array[i];
            }
            return(true);
        }
Ejemplo n.º 7
0
        internal RangePartition(LambdaExpression keySelector,
                                TKey[] partitionKeys,
                                IComparer <TKey> comparer,
                                bool?isDescending,
                                Int32 parCnt)
            : base(PartitionType.Range)
        {
            this.m_count         = (partitionKeys == null) ? parCnt : (partitionKeys.Length + 1);
            this.m_keySelector   = keySelector;
            this.m_partitionKeys = partitionKeys;
            this.m_comparer      = TypeSystem.GetComparer <TKey>(comparer);
            if (isDescending == null)
            {
                if (partitionKeys == null)
                {
                    throw new DryadLinqException(HpcLinqErrorCode.PartitionKeysNotProvided,
                                                 SR.PartitionKeysNotProvided);
                }

                bool?detectedIsDescending;

                if (!HpcLinqUtil.ComputeIsDescending(partitionKeys, m_comparer, out detectedIsDescending))
                {
                    throw new DryadLinqException(HpcLinqErrorCode.PartitionKeysAreNotConsistentlyOrdered,
                                                 SR.PartitionKeysAreNotConsistentlyOrdered);
                }

                this.m_isDescending = detectedIsDescending ?? false;
            }
            else
            {
                this.m_isDescending = isDescending.GetValueOrDefault();
                if (partitionKeys != null &&
                    !HpcLinqUtil.IsOrdered(partitionKeys, this.m_comparer, this.m_isDescending))
                {
                    throw new DryadLinqException(HpcLinqErrorCode.IsDescendingIsInconsistent,
                                                 SR.IsDescendingIsInconsistent);
                }
            }
        }
Ejemplo n.º 8
0
        internal override bool IsSamePartition(PartitionInfo p)
        {
            RangePartition <TKey> p1 = p as RangePartition <TKey>;

            if (p1 == null)
            {
                return(false);
            }

            // Check the keys:
            if (this.Keys == null ||
                p1.Keys == null ||
                this.Keys.Length != p1.Keys.Length)
            {
                return(false);
            }

            IComparer <TKey> comp1 = TypeSystem.GetComparer <TKey>(p1.m_comparer);

            if (comp1 == null)
            {
                return(false);
            }
            if (this.IsDescending != p1.IsDescending)
            {
                comp1 = new MinusComparer <TKey>(comp1);
            }
            for (int i = 0; i < this.Keys.Length; i++)
            {
                if (this.m_comparer.Compare(this.Keys[i], p1.Keys[i]) != 0)
                {
                    return(false);
                }
            }

            // Check the comparers:
            return(this.m_comparer.Equals(p1.m_comparer));
        }