NoDuplicates without using (more) templates

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Sep 27 03:26:15 UTC 2020


On 9/26/20 11:15 PM, Steven Schveighoffer wrote:
> But I figured out, if I combine both the mangleof and stringof, you get 
> something that is bound to be unique for each value AND type.

That's a pretty awesome hack. Currently the reification code keeps a 
separate enum to distinguish between values and types. This trick could 
help with getting rid of that.

One disadvantage is that once you concatenate you lose the interning 
(mangled types are interned so you only need to compare pointers to 
decide whether two types are equal). But, in the words of Steven Wright, 
you can't have everything; where would you put it?

> I'm not sure which one would perform better. One has better complexity, but the other is more correct (you could possibly split out lambdas or other problematic things into their own sub-function). But both I think might be a step up over the current NoDuplicates, which is horrendously complicated. 

For reference this is the reification-based code:

alias NoDuplicates(Args...) = dereify!({
     auto ids = reifyArray!Args;
     size_t newLength = ids.length;
     for (size_t i = 0; i < newLength; ++i) {
         newLength = 1 + i + ids[i + 1 .. $].remove!(x => x == 
ids[i]).length;
     }
     return ids[0 .. newLength];
}());

It calls into std.remove to (inefficiently) get rid of duplicates of the 
ith element from the array from the remainder of the array, thus 
compacting the array as it goes. At the end the result will be on the 
left end of the array.

Measuring the speed of each of these would be quite interesting.


More information about the Digitalmars-d mailing list