February 06, 2007
Dawid Ciężarkiewicz wrote:
> Michiel wrote:
> 
> 
>>Is there a built-in way to create a set in D? Like an associative array,
>>only without the value?
> 
> 
> Sets are so useful, and aa with void value could work as sets quite well.
> 
> Walter ... pleeease? :)

Hear, hear!

 - Gregor Richards
February 06, 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


> 
February 08, 2007
> I think creating a whole different data structure just for the set isn't
worth the
> trouble.

Sets done properly:

type
  TCarManufacturer = (cmFord, cmRenault, cmVauxhall, cmFiat); //
enumeration - each element has an ordinal

// value - not numerical
type
  TCarManufacturers = set of TCarManufacturer;

const
  DEFAULT_CAR_MANUF = [cmFord, cmVauhall]; //square brackets denote "set" in
code

var
  myset: TCarManufacturers;
  myintersection: TCarManufacturers;
begin
  myset := [cmFord]; //Assign "ford" to the set

  myset := myset + DEFAULT_CAR_MANUF; //still only contains one "cmFord"

  myintersection := []; //empty set - sets don't need to be initialised
however...

  myintersection := myset * [cmFord]; //intersection

  myset := myset - myintersection; //removes the common values

end;

The following operators take sets as operands.

Operator     Operation      Operand types   Result type    Example
+                 union              set                     set
Set1 + Set2
-                 difference       set                      set
S - T
*                 intersection     set                     set
S * T
<=               subset            set                      Boolean       Q
<= MySet
>=               superset         set                      Boolean       S1 = S2
=                 equality          set                       Boolean
S2 = MySet
<>               inequality        set                      Boolean
MySet <> S1
in                 membership   ordinal, set           Boolean       A in
Set1


1 2
Next ›   Last »