Jump to page: 1 2 3
Thread overview
sorting of array containing user defined types
May 29, 2004
Andre Fornacon
May 29, 2004
J Anderson
May 29, 2004
andre
May 29, 2004
Arcane Jill
May 29, 2004
andre
May 29, 2004
J Anderson
May 29, 2004
andre
May 29, 2004
J Anderson
May 29, 2004
Sarat Venugopal
May 29, 2004
J Anderson
May 29, 2004
Sarat Venugopal
May 29, 2004
Sean Kelly
May 29, 2004
Sean Kelly
D containers (was Re: sorting...)
May 30, 2004
Arcane Jill
May 30, 2004
Walter
May 30, 2004
Arcane Jill
May 30, 2004
J Anderson
May 31, 2004
Antti Sykäri
Jun 04, 2004
Matthew
Jun 04, 2004
Matthew
May 29, 2004
andre
May 30, 2004
Walter
May 30, 2004
Walter
May 29, 2004
hello d programmers,

i hope someone can help me with the following problem:
how can i sort an array of userdefined types - e.g.:
mysort.d:

import std.c.stdio;

struct qtr
{
	char[] m_server;
	char[] m_volume;
	uint m_used;

	char[] toString()
	{
		char[] rv=new char[256];
		int len=sprintf(rv,"%.*s:%.*s:%d",m_server,m_volume,m_used);
		return rv[0 .. len];
	}
}

int main(char[][] args)
{
	static qtr[] ql=[{"s1","v1",10},{"s0","v0",5},{"s2","v2",15}];

	printf("ql before sort : ql.len=%d\n",ql.length);
	foreach(int n,qtr q;ql)
		printf("(%d) %.*s\n",n,q.toString());

	ql.sort;

	printf("ql after sort : ql.len=%d\n",ql.length);
	foreach(int n,qtr q;ql)
		printf("(%d) %.*s\n",n,q.toString());

	return 0;
}

compiling this:

dmd mysort.d
gcc mysort.o -o mysort -lphobos -lpthread -lm
mysort.o: In function `_Dmain':
mysort.o(.gnu.linkonce.t_Dmain+0x75): undefined reference to `_init_21TypeInfo_S6mysort3qtr'
collect2: ld returned 1 exit status

how can i achieve this in the d programming language ?
in C(++) i can use qsort and a custom compare function.
i found a few vague for a d qsort in the phobos sources, but wasnt able to to get it used.

i already played with classes derived from TypeInfo and overloading opCmp - and got it to link,
but not running.
i'm pretty shure it must be something realy simple i overlooked in the documentation ?!.

thanks for your help !
andre
May 29, 2004
Andre Fornacon wrote:

> hello d programmers,
>
> i hope someone can help me with the following problem:
> how can i sort an array of userdefined types - e.g.:
> mysort.d:
>
> import std.c.stdio;
>
> struct qtr
> {
>     char[] m_server;
>     char[] m_volume;
>     uint m_used;
>
>     char[] toString()
>     {
>         char[] rv=new char[256];
>         int len=sprintf(rv,"%.*s:%.*s:%d",m_server,m_volume,m_used);
>         return rv[0 .. len];
>     }
> }
>
> int main(char[][] args)
> {
>     static qtr[] ql=[{"s1","v1",10},{"s0","v0",5},{"s2","v2",15}];
>
>     printf("ql before sort : ql.len=%d\n",ql.length);
>     foreach(int n,qtr q;ql)
>         printf("(%d) %.*s\n",n,q.toString());
>
>     ql.sort;
>
>     printf("ql after sort : ql.len=%d\n",ql.length);
>     foreach(int n,qtr q;ql)
>         printf("(%d) %.*s\n",n,q.toString());
>
>     return 0;
> }
>
> compiling this:
>
> dmd mysort.d
> gcc mysort.o -o mysort -lphobos -lpthread -lm
> mysort.o: In function `_Dmain':
> mysort.o(.gnu.linkonce.t_Dmain+0x75): undefined reference to  `_init_21TypeInfo_S6mysort3qtr'
> collect2: ld returned 1 exit status
>
> how can i achieve this in the d programming language ?
> in C(++) i can use qsort and a custom compare function.
> i found a few vague for a d qsort in the phobos sources, but wasnt able to  to get it used.
>
> i already played with classes derived from TypeInfo and overloading opCmp  - and got it to link,
> but not running.
> i'm pretty shure it must be something realy simple i overlooked in the  documentation ?!.
>
> thanks for your help !
> andre

You need to overload opCmp, however you have to do it like:

class A
{

int opCmp(Object obj)
{
  A objA =  cast(A) obj;
  ...ect...
}
}

The trick is that you have to casting from object.
I've asked Walter about getting this fixed (so you don't require casting) but I've got no reply.  So I don't even know if this ever will be fixed or if Walter thinks its a bug or not.

Then you can go:

A [] a;

...

a.sort;

-- 
-Anderson: http://badmama.com.au/~anderson/
May 29, 2004
On 2004-05-29, J Anderson <REMOVEanderson@badmama.com.au> wrote:
> Andre Fornacon wrote:
>
>> hello d programmers,
>>
>> i hope someone can help me with the following problem:
>> how can i sort an array of userdefined types - e.g.:
>> mysort.d:
>>
>> import std.c.stdio;
>>
>> struct qtr
>> {
>>     char[] m_server;
>>     char[] m_volume;
>>     uint m_used;
>>
>>     char[] toString()
>>     {
>>         char[] rv=new char[256];
>>         int len=sprintf(rv,"%.*s:%.*s:%d",m_server,m_volume,m_used);
>>         return rv[0 .. len];
>>     }
>> }
>>
>> int main(char[][] args)
>> {
>>     static qtr[] ql=[{"s1","v1",10},{"s0","v0",5},{"s2","v2",15}];
>>
>>     printf("ql before sort : ql.len=%d\n",ql.length);
>>     foreach(int n,qtr q;ql)
>>         printf("(%d) %.*s\n",n,q.toString());
>>
>>     ql.sort;
>>
>>     printf("ql after sort : ql.len=%d\n",ql.length);
>>     foreach(int n,qtr q;ql)
>>         printf("(%d) %.*s\n",n,q.toString());
>>
>>     return 0;
>> }
>>
>> compiling this:
>>
>> dmd mysort.d
>> gcc mysort.o -o mysort -lphobos -lpthread -lm
>> mysort.o: In function `_Dmain':
>> mysort.o(.gnu.linkonce.t_Dmain+0x75): undefined reference to
>> `_init_21TypeInfo_S6mysort3qtr'
>> collect2: ld returned 1 exit status
>>
>> how can i achieve this in the d programming language ?
>> in C(++) i can use qsort and a custom compare function.
>> i found a few vague for a d qsort in the phobos sources, but wasnt
>> able to  to get it used.
>>
>> i already played with classes derived from TypeInfo and overloading
>> opCmp  - and got it to link,
>> but not running.
>> i'm pretty shure it must be something realy simple i overlooked in
>> the  documentation ?!.
>>
>> thanks for your help !
>> andre
>
> You need to overload opCmp, however you have to do it like:
>
> class A
> {
>
> int opCmp(Object obj)
> {
>    A objA =  cast(A) obj;
>    ...ect...
> }
> }
>
> The trick is that you have to casting from object.
> I've asked Walter about getting this fixed (so you don't require
> casting) but I've got no reply.  So I don't even know if this ever will
> be fixed or if Walter thinks its a bug or not.
>
> Then you can go:
>
> A [] a;
>
> ...
>
> a.sort;
>

hello,
first thanks for your very fast reply !!!
it works now - great.
i played with the following opCmp signatures:
opCmp(void*) and opCmp(qtr q)

stupid me forgot to try  'opCmp(Object)' one ...

another question:
* any chance todo this for structs instead of classes ?!

* how do i do sorts for different values , e.g.
  (1) sort by the m_used field,
  (2) sort by another field (m_server)

that mean i need to used different comparision operator,
in other language (C(++),perl) thats no problem.
looks like the current capabilities for sorting are a bit limiting ?!

thank you very much !
-- 
// Andre  -> afo <at> zlug <dot> org
//
// "Why do I get this urge to go bowling everytime I see Tux?" ;-)
May 29, 2004
>another question:
>* any chance todo this for structs instead of classes ?!

I haven't tried this personally, but I see no reason why it shouldn't work. For structs, you can presumably use the good old fashioned qsort function. Its interface should, I believe, be exactly the same as in C.

Arcane Jill



May 29, 2004
On 2004-05-29, Arcane Jill <Arcane_member@pathlink.com> wrote:
>>another question:
>>* any chance todo this for structs instead of classes ?!
>
> I haven't tried this personally, but I see no reason why it shouldn't work. For structs, you can presumably use the good old fashioned qsort function. Its interface should, I believe, be exactly the same as in C.
>
> Arcane Jill

hi,

thanks for the hint - you are right (of course).
it does work.
i wasnt aware that D array are fully compatible with C "array".
and i'm not that sure if it fully intentional ?!

imho it looks *very* ugly, to much 'extern (C)',casting,... for me ;-)

and to quote from http://www.digitalmars.com/d/ctod.html#sort :
---------------------------------------------------------------
The D Way
Sorting couldn't be easier:

type[] array;
...
array.sort;// sort array in-place
---------------------------------------------------------------
sad that it works only for builtin types and classes.
are user defined structs *that* uncommon ?

i was thinking of something like

         array.sort(compare_function);

with compare_function defaulting to 'type.opCmp()'


for those interested in the code:

import std.c.stdio;

struct qtr
{
	char[] m_server;
	char[] m_volume;
	uint m_used;

	char[] toString()
	{
		char[] rv=new char[256];
		int len=sprintf(rv,"%.*s:%.*s:%d",m_server,m_volume,m_used);
		return rv[0 .. len];
	}
}

extern (C) void qsort(void* base,size_t nmemb,size_t size,int (*cmp)(void*,void*));

extern (C) int qtr_compare(void* p1,void* p2)
{
	qtr* q1=cast(qtr*) p1;
	qtr* q2=cast(qtr*) p2;

	return q1.m_used < q2.m_used;
}

int main(char[][] args)
{
	static qtr[] ql=[{"s1","v1",10},{"s0","v0",5},{"s2","v2",15}];

	printf("ql before sort : ql.len=%d\n",ql.length);
	foreach(int n,qtr q;ql)
		printf("(%d) %.*s\n",n,q.toString());

	qsort(cast(void*)ql,ql.length,qtr.size,&qtr_compare);

	printf("ql after sort : ql.len=%d\n",ql.length);
	foreach(int n,qtr q;ql)
		printf("(%d) %.*s\n",n,q.toString());

	return 0;
}

greetings
-- 
// Andre  -> afo <at> zlug <dot> org
//
// "Why do I get this urge to go bowling everytime I see Tux?" ;-)
May 29, 2004
>* how do i do sorts for different values , e.g.
>  (1) sort by the m_used field,
>  (2) sort by another field (m_server)
>  
>
You could wrap the class inside another class, or use a state-machine.

>that mean i need to used different comparision operator,
>in other language (C(++),perl) thats no problem.
>looks like the current capabilities for sorting are a bit limiting ?!
>
>thank you very much !
>  
>
I don't see how C++ is more powerful in this regard, you can always do the same in D.

-- 
-Anderson: http://badmama.com.au/~anderson/
May 29, 2004
On 2004-05-29, J Anderson <REMOVEanderson@badmama.com.au> wrote:
>
>>* how do i do sorts for different values , e.g.
>>  (1) sort by the m_used field,
>>  (2) sort by another field (m_server)
>> 
>>
> You could wrap the class inside another class, or use a state-machine.
>
>>that mean i need to used different comparision operator,
>>in other language (C(++),perl) thats no problem.
>>looks like the current capabilities for sorting are a bit limiting ?!
>>
>>thank you very much !
>> 
>>
> I don't see how C++ is more powerful in this regard,

the stl provides a very clean separation between container classes
(vector,list,map,...) and algorithms (sort,count,transform,replace_if,...)
combining both using the iterator interface is one of the great things
of the stl.
and you can use your own algorithms and/or your own container classes

if you move basic container classes like dynamic arrays, maps into the language
- which is imho a good thing - you have to put similar algorithms into
the language.
or at least provide an interface allowing to use
custom implementation of algorithmns for e.g. sorting.

> you can always do the same in D.

well i relly like the things which are much easier todo using D than C++
e.g. string handling, array's , use std library, etc.
but imho (advanced) sorting isnt one of these ...

greetings
-- 
// Andre  -> afo <at> zlug <dot> org
//
// "Why do I get this urge to go bowling everytime I see Tux?" ;-)
May 29, 2004
andre wrote:

>On 2004-05-29, J Anderson <REMOVEanderson@badmama.com.au> wrote:
>  
>
>>>* how do i do sorts for different values , e.g.
>>> (1) sort by the m_used field,
>>> (2) sort by another field (m_server)
>>> 
>>>
>>>      
>>>
>>You could wrap the class inside another class, or use a state-machine.
>>
>>    
>>
>>>that mean i need to used different comparision operator,
>>>in other language (C(++),perl) thats no problem.
>>>looks like the current capabilities for sorting are a bit limiting ?!
>>>
>>>thank you very much !
>>> 
>>>
>>>      
>>>
>>I don't see how C++ is more powerful in this regard, 
>>    
>>
>
>the stl provides a very clean separation between container classes
>(vector,list,map,...) and algorithms (sort,count,transform,replace_if,...)
>combining both using the iterator interface is one of the great things of the stl.
>and you can use your own algorithms and/or your own container classes
>
>if you move basic container classes like dynamic arrays, maps into the language
>- which is imho a good thing - you have to put similar algorithms into
>the language. or at least provide an interface allowing to use
>custom implementation of algorithmns for e.g. sorting.
>
>  
>
>>you can always do the same in D.
>>    
>>
>
>well i relly like the things which are much easier todo using D than C++
>e.g. string handling, array's , use std library, etc.
>but imho (advanced) sorting isnt one of these ...
>
>greetings
>  
>
I'm sure this will come out in a standard lib eventually.  Note that stl is a lib not part of C++ itself.  However for simple things D does maps and arrays in a much neater-to-use way (I'm looking at this from the client side rather then from the implementation side as you are).  It shouldn't be hard to map these into your own libraries if you so wish.

-- 
-Anderson: http://badmama.com.au/~anderson/
May 29, 2004
"J Anderson" <REMOVEanderson@badmama.com.au> wrote in message news:c9ac0l$2kh9$1@digitaldaemon.com...
> andre wrote:
>
> >On 2004-05-29, J Anderson <REMOVEanderson@badmama.com.au> wrote:
> >
> >
> >>>* how do i do sorts for different values , e.g.
> >>> (1) sort by the m_used field,
> >>> (2) sort by another field (m_server)
> >>>
> >>>
> >>>
> >>>
> >>You could wrap the class inside another class, or use a state-machine.
> >>
> >>
> >>
> >>>that mean i need to used different comparision operator,
> >>>in other language (C(++),perl) thats no problem.
> >>>looks like the current capabilities for sorting are a bit limiting ?!
> >>>
> >>>thank you very much !
> >>>
> >>>
> >>>
> >>>
> >>I don't see how C++ is more powerful in this regard,
> >>
> >>
> >
> >the stl provides a very clean separation between container classes (vector,list,map,...) and algorithms
(sort,count,transform,replace_if,...)
> >combining both using the iterator interface is one of the great things
> >of the stl.
> >and you can use your own algorithms and/or your own container classes
> >
> >if you move basic container classes like dynamic arrays, maps into the
language
> >- which is imho a good thing - you have to put similar algorithms into
> >the language.
> >or at least provide an interface allowing to use
> >custom implementation of algorithmns for e.g. sorting.
> >
> >
> >
> >>you can always do the same in D.
> >>
> >>
> >
> >well i relly like the things which are much easier todo using D than C++
> >e.g. string handling, array's , use std library, etc.
> >but imho (advanced) sorting isnt one of these ...
> >
> >greetings
> >
> >
> I'm sure this will come out in a standard lib eventually.  Note that stl is a lib not part of C++ itself.  However for simple things D does maps and arrays in a much neater-to-use way (I'm looking at this from the client side rather then from the implementation side as you are).  It shouldn't be hard to map these into your own libraries if you so wish.

No. There is no "STL" nowadays, really. It is all just ISO C++.
std::vector<T> is just as much a first class citizen as T[]. The same with
std::sort() and qsort().

D is evolving, so there is no need to defend absense of certain things. I may be wrong, but the D map looks more like an associative array, which is a different beast from the map. Anyone looked into it?

Just being objective....

Best,
Sarat Venugopal
www.huelix.com


> -- 
> -Anderson: http://badmama.com.au/~anderson/


May 29, 2004
Sarat Venugopal wrote:

>"J Anderson" <REMOVEanderson@badmama.com.au> wrote in message
>news:c9ac0l$2kh9$1@digitaldaemon.com...
>  
>
>>andre wrote:
>>
>>    
>>
>>>On 2004-05-29, J Anderson <REMOVEanderson@badmama.com.au> wrote:
>>>
>>>
>>>      
>>>
>>>>>* how do i do sorts for different values , e.g.
>>>>>(1) sort by the m_used field,
>>>>>(2) sort by another field (m_server)
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>You could wrap the class inside another class, or use a state-machine.
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>>>that mean i need to used different comparision operator,
>>>>>in other language (C(++),perl) thats no problem.
>>>>>looks like the current capabilities for sorting are a bit limiting ?!
>>>>>
>>>>>thank you very much !
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>I don't see how C++ is more powerful in this regard,
>>>>
>>>>
>>>>        
>>>>
>>>the stl provides a very clean separation between container classes
>>>(vector,list,map,...) and algorithms
>>>      
>>>
>(sort,count,transform,replace_if,...)
>  
>
>>>combining both using the iterator interface is one of the great things
>>>of the stl.
>>>and you can use your own algorithms and/or your own container classes
>>>
>>>if you move basic container classes like dynamic arrays, maps into the
>>>      
>>>
>language
>  
>
>>>- which is imho a good thing - you have to put similar algorithms into
>>>the language.
>>>or at least provide an interface allowing to use
>>>custom implementation of algorithmns for e.g. sorting.
>>>
>>>
>>>
>>>      
>>>
>>>>you can always do the same in D.
>>>>
>>>>
>>>>        
>>>>
>>>well i relly like the things which are much easier todo using D than C++
>>>e.g. string handling, array's , use std library, etc.
>>>but imho (advanced) sorting isnt one of these ...
>>>
>>>greetings
>>>
>>>
>>>      
>>>
>>I'm sure this will come out in a standard lib eventually.  Note that stl
>>is a lib not part of C++ itself.  However for simple things D does maps
>>and arrays in a much neater-to-use way (I'm looking at this from the
>>client side rather then from the implementation side as you are).  It
>>shouldn't be hard to map these into your own libraries if you so wish.
>>    
>>
>
>No. There is no "STL" nowadays, really. It is all just ISO C++.
>std::vector<T> is just as much a first class citizen as T[]. The same with
>std::sort() and qsort().
>
>D is evolving, so there is no need to defend absense of certain things. I
>may be wrong, but the D map looks more like an associative array, which is a
>different beast from the map. Anyone looked into it?
>
>Just being objective....
>
>Best,
>Sarat Venugopal
>www.huelix.com
>
>  
>
I didn't mean that.  What I meant was that it should be pretty easy to do exactly what STL does in D as STL is a library not part of the language.  Also you can map a vector to a D array if you wish like:

class vector(type)
{
  private type [] A;
  type opIndex(int index) { return opIndex[index]; }
...
}

So it would be much easier to create something like stl in D then C++.

-- 
-Anderson: http://badmama.com.au/~anderson/
« First   ‹ Prev
1 2 3