August 05, 2005
z@gg.com wrote:
 > so I wonder if we should add another build-in method for array (T):
> 
> array.sort(int function(T, T) cmp)
> 
> to allow user pass in comparator other than the default opCmp().

That would be great! This is a common scenario and currently we have to roll out our own custom sort routine each time we need to customize anything. This is something that a modern standard library (and considering the C standard library provided a sort with custom comparator, even not so modern) should *definitely* do.

Basically I find myself using MinTL most of the time for my container needs since it seems to be a *pretty god damn badass library*, at least when compared to what's available in language level and Phobos.

-- 
Niko Korhonen
SW Developer
August 07, 2005
<z@gg.com> wrote in message news:dctotb$ql5$1@digitaldaemon.com...
> Currently array has a build-in method .sort, which use the default opCmp.
>
> However it often need to sort an array in different ways: e.g. an arry of
> File
> object, we may need to
>
> File[] files;
>
> files.sort(BySize);
> files.sort(ByDate);
> files.sort(ByName);
> files.sort(ByExtention);
>
> so I wonder if we should add another build-in method for array (T):
>
> array.sort(int function(T, T) cmp)
>
> to allow user pass in comparator other than the default opCmp().
>
> Or we should provide a global template function in the standard library:
>
> template(T) {
> sort(T[] array, int function(T, T) cmp);
> }

This is slightly off topic but I've added some sorting routines to MinTL.
The most directly related to this thread is
  template(T:T[]) {
    sort(T[] array, int delegate(T*, T*) cmp = null);
  }
where the default (null) comparison delegate means to sort by the default
comparison function for type T (from its TypeInfo).
While I was at it I added sorting to ArrayList, Deque, List and HashAA. The
first two use quicksort/insertion-sort and the second two use a linked-list
merge-sort. Slices are sorted in-place. The HashAA is an associative array
ordered by insertion order and so the sort (which is only allowed for the
key type) allows for associative arrays in different orders while still
maintaining the performance of a hashtable. For more info go to the standard
place http://home.comcast.net/~benhinkle/mintl/index.html

-Ben


August 08, 2005
z@gg.com wrote:
<snip>
> so I wonder if we should add another build-in method for array (T):
> 
> array.sort(int function(T, T) cmp)
<snip>

*** This request has been marked as a duplicate of http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/18851 ***

We have a wish list here:

http://www.wikiservice.at/wiki4d/wiki.cgi?FeatureRequestList

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on on the 'group where everyone may benefit.
August 22, 2005
Because I just had need of porting this function again, here you go:

    template QuickSort (T, alias Compare)
    {
        T [] QuickSort (T [] list)
        {
            const size_t maxspan = 7;

            static void swap (T *a, T *b)
            {
                T c = *a;

                *a = *b;
                *b = c;
            }

            T *base;
            T *[40] stack;
            T **sp = stack;
            T *i, j, limit;

            base = list.ptr;
            limit = base + list.length;

            while (1)
            {
                while (limit - base > maxspan)
                {
                    swap ((cast (size_t) (limit - base) >> 1) + base, base);
                    i = base + 1;
                    j = limit - 1;

                    if (Compare (*i, *j) > 0)
                        swap (i, j);
                    if (Compare (*base, *j) > 0)
                        swap (base, j);
                    if (Compare (*i, *base) > 0)
                        swap (i, base);

                    while (1)
                    {
                        do i ++;
                        while (Compare (*i, *base) < 0);

                        do j --;
                        while (Compare (*j, *base) > 0);

                        if (i > j)
                            break;
                        swap (i, j);
                    }

                    swap (base, j);
                    if (j - base > limit - i)
                    {
                        sp [0] = base;
                        sp [1] = j;
                        base = i;
                    }
                    else
                    {
                        sp [0] = i;
                        sp [1] = limit;
                        limit = j;
                    }

                    sp += 2;
                }

                i = base + 1;

                while (i < limit)
                {
                    j = i;
                    while (j > base && Compare (*(i - 1), *j) > 0)
                    {
                        swap (j - 1, j);
                        j --;
                    }

                    i ++;
                }

                if (sp > stack)
                {
                    sp -= 2;
                    base = sp [0];
                    limit = sp [1];
                }
                else
                    break;
            }

            return list;
        }
    }

    template QuickSort (T)
    {
        T [] QuickSort (T [] list)
        {
            int compare (T a, T b)
            {
                return a < b ? -1 : +1;
            }

            return .QuickSort! (T, compare) (list);
        }
    }

This is used like:

    int compare (int a, int b)
    {
        return a - b;
    }

    QuickSort! (int, compare) (list);

An apples-to-apples comparison with list.sort shows that this function is about 130% faster when sorting random data, all due to the compare inlining and compile-time type sizing (this is otherwise just a straight port of what's in Phobos).
1 2
Next ›   Last »