Jump to page: 1 2
Thread overview
array.sort(cmp) ?
Aug 04, 2005
z
Aug 05, 2005
Regan Heath
Aug 05, 2005
Regan Heath
Aug 05, 2005
Regan Heath
Aug 05, 2005
Shammah Chancellor
Aug 05, 2005
Regan Heath
Aug 05, 2005
zhou
Aug 05, 2005
Shammah Chancellor
Aug 05, 2005
Regan Heath
Aug 05, 2005
Niko Korhonen
Aug 07, 2005
Ben Hinkle
Aug 08, 2005
Stewart Gordon
Aug 22, 2005
Burton Radons
August 04, 2005
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);
}



August 05, 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

I'd love this.  I've run into this a few times, and it's ugly to have to use qsort and write a comparator function (which can't be a nested function or function literal as nested functions don't have C linkage..).


August 05, 2005
On Thu, 4 Aug 2005 19:05:15 +0000 (UTC), <z@gg.com> wrote:
> 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)

Or a delegate. With a delegate you could pass any addition information required for the sort in the class.

Regan
August 05, 2005
On Thu, 4 Aug 2005 19:05:15 +0000 (UTC), <z@gg.com> wrote:
> 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);
> }

I just had an idea. Could the 'sort' property be a get/set property and thus assignable? eg.

int function(File lhs, File rhs) foo;
files.sort = foo; //assigns sort function
files.sort;       //calls sort function

?

Regan
August 05, 2005
On Fri, 05 Aug 2005 14:09:00 +1200, Regan Heath <regan@netwin.co.nz> wrote:
> On Thu, 4 Aug 2005 19:05:15 +0000 (UTC), <z@gg.com> wrote:
>> 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);
>> }
>
> I just had an idea. Could the 'sort' property be a get/set property and thus assignable? eg.

Or rather:

int foo(File lhs, File rhs) {  .. }
files.sort = &foo; //assigns sort function
files.sort;        //calls sort function

Regan
August 05, 2005
In article <opsu0iy2a523k2f5@nrage.netwin.co.nz>, Regan Heath says...
>
>On Fri, 05 Aug 2005 14:09:00 +1200, Regan Heath <regan@netwin.co.nz> wrote:
>> On Thu, 4 Aug 2005 19:05:15 +0000 (UTC), <z@gg.com> wrote:
>>> 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);
>>> }
>>
>> I just had an idea. Could the 'sort' property be a get/set property and thus assignable? eg.
>
>Or rather:
>
>int foo(File lhs, File rhs) {  .. }
>files.sort = &foo; //assigns sort function
>files.sort;        //calls sort function
>
>Regan

If a class can be sorted by different properties, it is intuitive that it should
be able to be compared by
multiple properties.  The following example should handle both things.
Although, Initially I tried to call
my delegate opCmp just to see if operator overloading would work on a delegate.
(Since you can have
a delegate as a member of the class and it looks like a method to everyone
else.)  I personally think two
things should happen although it's not relevant to this example.  That A,
delegates should be able to
have initializers inside of classes, like in functions.  And B, that operator
overloading should look for
delegates with the same name as the function that would overload it.

Example code:

#class Foo {
#	public int value;
#	public int delegate(in Foo) opCmpDelegate;
#
#	int CompareAlwaysLess(in Foo x) { return -1; };
#	int CompareForReal(Foo x) { return this.value - x.value; }
#	this(int initArg) { opCmpDelegate = &CompareAlwaysLess; value = initArg; }
#	int opCmp(Foo x) { return opCmpDelegate(x); };
#}
#
#int main(char[][] args)
#{
#	Foo a = new Foo(20);
#	Foo b = new Foo(10);
#
#	writefln( a < b ? "true" : "false" );
#	a.opCmpDelegate = &a.CompareForReal;
#	writefln( a < b ? "true" : "false" );
#
#	return 0;
#}


August 05, 2005
On Fri, 5 Aug 2005 03:09:42 +0000 (UTC), Shammah Chancellor <Shammah_member@pathlink.com> wrote:
> In article <opsu0iy2a523k2f5@nrage.netwin.co.nz>, Regan Heath says...
>>
>> On Fri, 05 Aug 2005 14:09:00 +1200, Regan Heath <regan@netwin.co.nz> wrote:
>>> On Thu, 4 Aug 2005 19:05:15 +0000 (UTC), <z@gg.com> wrote:
>>>> 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);
>>>> }
>>>
>>> I just had an idea. Could the 'sort' property be a get/set property and
>>> thus assignable? eg.
>>
>> Or rather:
>>
>> int foo(File lhs, File rhs) {  .. }
>> files.sort = &foo; //assigns sort function
>> files.sort;        //calls sort function
>>
>> Regan
>
> If a class can be sorted by different properties, it is intuitive that it should
> be able to be compared by
> multiple properties.  The following example should handle both things.
> Although, Initially I tried to call
> my delegate opCmp just to see if operator overloading would work on a delegate.
> (Since you can have
> a delegate as a member of the class and it looks like a method to everyone
> else.)  I personally think two
> things should happen although it's not relevant to this example.  That A,
> delegates should be able to
> have initializers inside of classes, like in functions.  And B, that operator
> overloading should look for
> delegates with the same name as the function that would overload it.
>
> Example code:
>
> #class Foo {
> #	public int value;
> #	public int delegate(in Foo) opCmpDelegate;
> #	
> #	int CompareAlwaysLess(in Foo x) { return -1; };
> #	int CompareForReal(Foo x) { return this.value - x.value; }
> #	this(int initArg) { opCmpDelegate = &CompareAlwaysLess; value = initArg; }
> #	int opCmp(Foo x) { return opCmpDelegate(x); };
> #}
> #
> #int main(char[][] args)
> #{
> #	Foo a = new Foo(20);
> #	Foo b = new Foo(10);
> #	
> #	writefln( a < b ? "true" : "false" );
> #	a.opCmpDelegate = &a.CompareForReal;
> #	writefln( a < b ? "true" : "false" );
> #	
> #	return 0;
> #}

Ok, but how does it work with an array?

Foo[] array;
foreach(Foo f; array) f.opCmpDelegate = &f.CompareForReal;
array.sort;

?

This seems wrong, it would allow different items to be sorted differently in the same array or sort operation. IMO the method of sorting should be defined per sort or per array, not per item.

Regan
August 05, 2005
In article <opsu0nedtt23k2f5@nrage.netwin.co.nz>, Regan Heath says...
>Ok, but how does it work with an array?
>
>Foo[] array;
>foreach(Foo f; array) f.opCmpDelegate = &f.CompareForReal;
>array.sort;
>
>?
>
>This seems wrong, it would allow different items to be sorted differently in the same array or sort operation. IMO the method of sorting should be defined per sort or per array, not per item.
>
>Regan

Exactly.  I still think STL-like one-liner is still the best:

quickSort(array.start, array.end, cmpFunc);
heapSort(array.start, array.end, cmpFunc);
..
stableSort(array.start, array.end, cmpFunc);
unstableSort(array.start, array.end, cmpFunc);


everything is stated on a single line: the [begin,end) of an array, the sorting method, and the comparator.

How would you devise something more clear to read, and more flexible to change? We really don't need to re-invent the wheels, especially those square ones.


August 05, 2005
In article <opsu0nedtt23k2f5@nrage.netwin.co.nz>, Regan Heath says...
>
>On Fri, 5 Aug 2005 03:09:42 +0000 (UTC), Shammah Chancellor <Shammah_member@pathlink.com> wrote:
>> In article <opsu0iy2a523k2f5@nrage.netwin.co.nz>, Regan Heath says...
>>>
>>> On Fri, 05 Aug 2005 14:09:00 +1200, Regan Heath <regan@netwin.co.nz> wrote:
>>>> On Thu, 4 Aug 2005 19:05:15 +0000 (UTC), <z@gg.com> wrote:
>>>>> 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);
>>>>> }
>>>>
>>>> I just had an idea. Could the 'sort' property be a get/set property and thus assignable? eg.
>>>
>>> Or rather:
>>>
>>> int foo(File lhs, File rhs) {  .. }
>>> files.sort = &foo; //assigns sort function
>>> files.sort;        //calls sort function
>>>
>>> Regan
>>
>> If a class can be sorted by different properties, it is intuitive that
>> it should
>> be able to be compared by
>> multiple properties.  The following example should handle both things.
>> Although, Initially I tried to call
>> my delegate opCmp just to see if operator overloading would work on a
>> delegate.
>> (Since you can have
>> a delegate as a member of the class and it looks like a method to
>> everyone
>> else.)  I personally think two
>> things should happen although it's not relevant to this example.  That A,
>> delegates should be able to
>> have initializers inside of classes, like in functions.  And B, that
>> operator
>> overloading should look for
>> delegates with the same name as the function that would overload it.
>>
>> Example code:
>>
>> #class Foo {
>> #	public int value;
>> #	public int delegate(in Foo) opCmpDelegate;
>> #
>> #	int CompareAlwaysLess(in Foo x) { return -1; };
>> #	int CompareForReal(Foo x) { return this.value - x.value; }
>> #	this(int initArg) { opCmpDelegate = &CompareAlwaysLess; value =
>> initArg; }
>> #	int opCmp(Foo x) { return opCmpDelegate(x); };
>> #}
>> #
>> #int main(char[][] args)
>> #{
>> #	Foo a = new Foo(20);
>> #	Foo b = new Foo(10);
>> #
>> #	writefln( a < b ? "true" : "false" );
>> #	a.opCmpDelegate = &a.CompareForReal;
>> #	writefln( a < b ? "true" : "false" );
>> #
>> #	return 0;
>> #}
>
>Ok, but how does it work with an array?
>
>Foo[] array;
>foreach(Foo f; array) f.opCmpDelegate = &f.CompareForReal;
>array.sort;
>
>?
>
>This seems wrong, it would allow different items to be sorted differently in the same array or sort operation. IMO the method of sorting should be defined per sort or per array, not per item.
>
>Regan


Hadn't thought of that.

opCmpDelegate should probably have been static.  But that leads to weird
behavior when you forget to
reset it and some other code calls it.

-Sha


August 05, 2005
On Fri, 5 Aug 2005 05:25:42 +0000 (UTC), Shammah Chancellor <Shammah_member@pathlink.com> wrote:
>> This seems wrong, it would allow different items to be sorted differently
>> in the same array or sort operation. IMO the method of sorting should be
>> defined per sort or per array, not per item.
>>
>> Regan
>
>
> Hadn't thought of that.
>
> opCmpDelegate should probably have been static.  But that leads to weird
> behavior when you forget to reset it and some other code calls it.

Yeah.. I followed that same thought pattern :)

Regan
« First   ‹ Prev
1 2