www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dtl - TypeInfo, containers and compare

reply "Ben Hinkle" <bhinkle mathworks.com> writes:
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
next sibling parent Berin Loritsch <bloritsch d-haven.org> writes:
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
prev sibling parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
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
Jul 22 2004