Sets

BCS ao at pathlink.com
Mon Feb 5 15:28:44 PST 2007


Reply to Michiel,

> Derek Parnell wrote:
> 
>> I just use: bool[Keytype] setName;
>> 
> The problem is that the set of possible keys isn't fixed (I'll often
> use char[] as key-type). So with your idea, there are two
> possibilities for elements not in the set. Either they're not in the
> array at all, or they are, but their value is false.
> 
> This gives me a very PHPish feeling. :)
> 
> I'd feel better about it if sets were built into D. But I know using a
> dummy value is an option. And that's what I'll use if I have no other
> choice.
> 

you might look at the (<key> "in" <set>) syntax

bool[Keys] set;

set[key1] = false;
set[key2] = true;

(key1 in set) // is true (!= null to be totally correct)
(key2 in set) // is true
(key3 in set) // is false (== null)

a good set syntax would be nice

void[key] set;  // no storage for each member
set[key1] = true;  // adds member
set[key1] = false;  // removes member
if(set[key1]) // test for member

set.keys // get set
...






More information about the Digitalmars-d mailing list