www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unique Enum Members

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Is there a way to calculate unique enum members without using 
sort, such as *not* done in my current implementation:

auto uniqueEnumMembers(T)()
{
     import std.traits: EnumMembers;
     import std.algorithm: sort, uniq;
     return [EnumMembers!T].sort().uniq;
}

Preferrably both at compile-time and run-time.
May 05 2016
parent Mark Isaacson <turck11 hotmail.com> writes:
On Thursday, 5 May 2016 at 12:54:08 UTC, Nordlöw wrote:
 Is there a way to calculate unique enum members without using 
 sort, such as *not* done in my current implementation:

 auto uniqueEnumMembers(T)()
 {
     import std.traits: EnumMembers;
     import std.algorithm: sort, uniq;
     return [EnumMembers!T].sort().uniq;
 }

 Preferrably both at compile-time and run-time.
Sure, if you can hash the underlying type this should do the trick in linear time: http://melpon.org/wandbox/permlink/Rvq60fv7jOIPuhXz
May 05 2016