digitalmars.D.dtl - TypeInfo, containers and compare
- Ben Hinkle (11/11) Jul 22 2004 For sorted containers I'm using the TypeInfo's "compare" as the default
- Berin Loritsch (10/21) Jul 22 2004 Are you saying you would like to do have something like this:
- Kris (15/26) Jul 22 2004 You might consider just making it a delegate field, since delegate works...
For sorted containers I'm using the TypeInfo's "compare" as the default comparison function and storing any custom comparison function in a field in the container. I'd like some ideas about ways to specify this function. Given that arrays always use the TypeInfo's compare to sort elements what do people think about making the sorted container always use the TypeInfo and if a user wants a custom compare they define a custom TypeInfo? I think it is possible to define TypeInfos but I haven't been able to find any posts on the newsgroups giving an example. I'm leaning towards keeping it as a field. Another wrinkle is supporting delegates as well as functions but for something like a comparison function delegates don't seem that useful. -Ben
Jul 22 2004
Ben Hinkle wrote:For sorted containers I'm using the TypeInfo's "compare" as the default comparison function and storing any custom comparison function in a field in the container. I'd like some ideas about ways to specify this function. Given that arrays always use the TypeInfo's compare to sort elements what do people think about making the sorted container always use the TypeInfo and if a user wants a custom compare they define a custom TypeInfo? I think it is possible to define TypeInfos but I haven't been able to find any posts on the newsgroups giving an example. I'm leaning towards keeping it as a field. Another wrinkle is supporting delegates as well as functions but for something like a comparison function delegates don't seem that useful.Are you saying you would like to do have something like this: int rel = container.compare(a,b); If the "compare" function is a delegate (that defaults to the TypeInfo compare), then it would be relatively easy to adjust it as necessary. container.compare=this.mycompare or something. In Java you have a "comparator" but I always thought that having a whole object just to implement a method was a bit overkill. The pattern is useful in many situations though.
Jul 22 2004
You might consider just making it a delegate field, since delegate works for TypeInfo.compare() also: int delegate (void *, void *) p; TypeInfo ti = typed (T); p = &ti.compare; p.compare ( ... ); I tend to think the minor overhead for delegates (one additional push?) is worth the extra flexibility. - Kris "Ben Hinkle" <bhinkle mathworks.com> wrote in message news:cdp84a$134e$1 digitaldaemon.com...For sorted containers I'm using the TypeInfo's "compare" as the default comparison function and storing any custom comparison function in a fieldinthe container. I'd like some ideas about ways to specify this function. Given that arrays always use the TypeInfo's compare to sort elements whatdopeople think about making the sorted container always use the TypeInfo and if a user wants a custom compare they define a custom TypeInfo? I think it is possible to define TypeInfos but I haven't been able to find any postsonthe newsgroups giving an example. I'm leaning towards keeping it as afield.Another wrinkle is supporting delegates as well as functions but for something like a comparison function delegates don't seem that useful. -Ben
Jul 22 2004