Ejemplo n.º 1
0
        private void TestBucketSort_Performance(string[] p)
        {
            int errCode = 0;

            try {
                // test constant length method
                System.Diagnostics.Stopwatch sw   = new System.Diagnostics.Stopwatch();
                ILQueueList <string, byte>   res1 = ILBucketSort.BucketSort <string, char, byte>(p, null, new ILASCIIKeyMapper(), ILBucketSort.SortMethod.ConstantLength);
                sw.Stop();
                Info("sorting variable length: " + sw.ElapsedMilliseconds + " ms needed.");
                string [] res = res1.ToArray();
                // test variable length method
                sw.Reset();
                sw.Start();
                ILQueueList <string, byte> res2 = ILBucketSort.BucketSort <string, char, byte>(p, null, new ILASCIIKeyMapper(), ILBucketSort.SortMethod.VariableLenth);
                sw.Stop();
                if (!StringsEqual(res1.ToArray(), res2.ToArray()))
                {
                    throw new Exception("different result for methods detected!");
                }
                Success("Sorted " + p.Length + " strings.");
            } catch (Exception e) {
                Error(errCode, e.Message + "(" + PrintStrings(p) + ")");
            }
        }
Ejemplo n.º 2
0
        private void TestBucketSort(string[] p, string[] p_2)
        {
            int errCode = 0;

            try {
                ILQueueList <string, byte> res = ILBucketSort.BucketSort <string, char, byte>(p, null, new ILASCIIKeyMapper(), ILBucketSort.SortMethod.VariableLenth);

                if (!StringsEqual(p_2, res.ToArray()))
                {
                    throw new Exception("invalid values detected!");
                }
                Success(PrintStrings(p));
            } catch (Exception e) {
                Error(errCode, e.Message + "(" + PrintStrings(p) + ")");
            }
        }
Ejemplo n.º 3
0
        bucketSort_variableLength <ElementType, SubelementType, IndexType>(
            IEnumerable <ElementType> input,
            ILKeyMapper <ElementType, SubelementType> mapper)
        {
            ILQueueList <ElementType, IndexType> ret;
            int m = mapper.NumberOfKeys;

            ILQueueList <ElementType, IndexType>[] buckets = new ILQueueList <ElementType, IndexType> [m];
            ILQueueList <ElementType, IndexType>   Q       = new ILQueueList <ElementType, IndexType>();
            IEnumerable <ElementType> inp = input;

            #region compute lmax
            int maxLen = 0, tmp = 0;
            foreach (ElementType elem in inp)
            {
                tmp = mapper.SubelementsCount(elem);
                if (tmp > maxLen)
                {
                    maxLen = tmp;
                }
            }
            #endregion
            #region create Lengths array
            ILQueueList <ElementType, IndexType>[] Lengths = new ILQueueList <ElementType, IndexType> [maxLen];
            foreach (ElementType elem in inp)
            {
                int len = mapper.SubelementsCount(elem) - 1;
                if (Lengths[len] == null)
                {
                    Lengths[len] = new ILQueueList <ElementType, IndexType>();
                }
                Lengths[len].Enqueue(elem);
            }
            #endregion
            #region create bucket indices for each position: Noempty array
            ILQueueList <int, byte>[] Noempty = new ILQueueList <int, byte> [maxLen];
            ILLogicalArray            alreadyFound         = new ILLogicalArray(maxLen, m);
            byte tr = (byte)1;
            foreach (ElementType s in inp)
            {
                for (int l = mapper.SubelementsCount(s); l-- > 0;)
                {
                    int hpos = mapper.Map(s, l, 0);
                    if (alreadyFound.GetValue(l, hpos) == 0)
                    {
                        alreadyFound.SetValue(tr, l, hpos);
                        if (Noempty[l] == null)
                        {
                            // create list and init with new value
                            Noempty[l] = new ILQueueList <int, byte>();
                        }
                        Noempty[l].Enqueue(hpos);
                    }
                }
            }
            alreadyFound.Dispose();
            for (int i = 0; i < maxLen; i++)
            {
                // sort lists for each length
                Noempty[i] = bucketSort_constantLength <int, int, byte>(Noempty[i], new ILIntLimitedKeyMapper(m));
            }
            #endregion
            #region sort
            ILListItem <ElementType, IndexType> curElement;
            for (int l = maxLen; l-- > 0;)
            {
                // sort Length[l] list
                if (Lengths[l] != null)
                {
                    Q.AddToStart(Lengths[l]);
                }
                // sort from last sorting loop (l+1)
                while (Q.Count > 0)
                {
                    curElement = Q.Dequeue();
                    int hpos = mapper.Map(curElement.Data, l, 0);
                    if (buckets[hpos] == null)
                    {
                        buckets[hpos] = new ILQueueList <ElementType, IndexType>();
                    }
                    buckets[hpos].Enqueue(curElement);
                }
                // collect queues
                foreach (int c in Noempty[l])
                {
                    Q.Enqueue(buckets[c]);
                    buckets[c].Clear();
                }
            }
            #endregion
            return(Q);
        }
Ejemplo n.º 4
0
        bucketSort_constantLength <ElementType, SubelementType, IndexType>(
            IEnumerable <ElementType> input,
            IEnumerable <IndexType> indices,
            ILKeyMapper <ElementType, SubelementType> mapper)
        {
            int m   = mapper.NumberOfKeys;
            int tmp = 0;

            ILQueueList <ElementType, IndexType>[] buckets = new ILQueueList <ElementType, IndexType> [m];
            ILQueueList <ElementType, IndexType>   Q       = new ILQueueList <ElementType, IndexType>();
            // find longest element
            int maxLen = 0;

            foreach (ElementType elem in input)
            {
                tmp = mapper.SubelementsCount(elem);
                if (tmp > maxLen)
                {
                    maxLen = tmp;
                }
            }
            // sort into buckets
            IEnumerator <IndexType> indIt = indices.GetEnumerator();

            indIt.MoveNext();
            for (int k = maxLen; k-- > 0;)
            {
                // walk along the input
                if (k == maxLen - 1)
                {
                    foreach (ElementType elem in input)
                    {
                        // put current into bucket
                        int bpos = mapper.Map(elem, k, 0);
                        if (buckets[bpos] == null)
                        {
                            // must create list first
                            buckets[bpos] = new ILQueueList <ElementType, IndexType>();
                        }
                        // append to list
                        buckets[bpos].Enqueue(elem, indIt.Current);
                        indIt.MoveNext();
                    }
                }
                else
                {
                    while (Q.Count > 0)
                    {
                        // put current into bucket
                        ILListItem <ElementType, IndexType> elem = Q.Dequeue();
                        int bpos = mapper.Map(elem.Data, k, 0);
                        if (buckets[bpos] == null)
                        {
                            // must create list first
                            buckets[bpos] = new ILQueueList <ElementType, IndexType>();
                        }
                        // append to list
                        buckets[bpos].Enqueue(elem);
                    }
                }
                // concatenate all buckets
                for (int i = 0; i < buckets.Length; i++)
                {
                    if (buckets[i] != null && buckets[i].Count > 0)
                    {
                        Q.Enqueue(buckets[i]);
                        buckets[i].Clear();
                    }
                }
                // goto previous position in strings
            }
            return(Q);
        }