View mode: basic / threaded / horizontal-split · Log in · Help
July 22, 2004
TypeInfo, containers and compare
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
July 22, 2004
Re: TypeInfo, containers and compare
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.
July 22, 2004
Re: TypeInfo, containers and compare
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 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
>
>
Top | Discussion index | About this forum | D home