Sets
Fredrik Olsson
peylow at gmail.com
Tue Feb 6 11:26:23 PST 2007
BCS skrev:
> 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
> ...
>
>
I have lost count of how many times I have pleaded for this:
Steal the Pascal way! It works marvelously.
Do not invent another mechanism for sets, do not make it a
template/library implementation.
For implementation most Pascal implementations uses bitfields for small
sets, and trees for larger sets.
To declare a set pascal uses:
set of type
I have previously suggests D could use:
type<>
Set literals in Pascal are declared as for example:
[1, 3..5]
I have previously suggested D could use:
<1, 3..5>
Pascal uses the in-operator to test for set membership, I say do the
same with D.
ch in <'w', 't', 'f'>
Pascal uses +-operator for set unions, --operator for difference, and *-
operator for intersection. GNUPascal add many more useful operators:
http://tinyurl.com/37efbm
Steal as many as is ever possible!
I think it is very unintuitive to use say mySet.add(foo) and
mySet.remove(foo) when I can use mySet += foo and mySet -= foo.
Oooh well... one could dream. Oh, and the usual disclaimer: DO NO
NITPICK ON SYNTAX, THEY ARE SUGGESTIONS, AND IF TECHNICAL RESTRICTIONS
APPLY; WHATEVER! I WANT THE FUNCTIONALITY BEHIND THE SYNTAX, NOT THE
SYNTAX ITSELF!!!
// Fredrik Olsson
>
More information about the Digitalmars-d
mailing list