Am I getting this all right?

Bill Baxter dnewsgroup at billbaxter.com
Thu Dec 14 16:42:21 PST 2006


Jarrett Billingsley wrote:
> "Jason House" <jhouse at mitre.org> wrote in message 
> news:elsctg$5ca$1 at digitaldaemon.com...
>> An earlier post showed stack with push and pop for an array.  How does one 
>> do a
>> queue? a set?
> 
> You could also use an associative array to model a set.

That's the right way to go.  Using arrays for sets is going to be 
sllloooooow.

> bool[int] set;
> set[4] = true;
> set[6] = true;
> set[10] = true;
> 
> if(4 in set)
>     writefln("4 is in the set");
> 
> foreach(v, nothing; set)
>     writefln(v);

Or just use
foreach(v; set.keys)
    writefln(v);

> set.remove(6);
> 
> The "nothing" in the foreach loop is because the values in the associative 
> array are useless -- they're just there because they have to be.
> 
> In fact, why don't I just make a little set class to make it a little 
> prettier.

The only reason not would be because opApply is slower than the builtin 
foreach on an array.  At least for regular arrays.  Not sure if the same 
is true for AA's.

Also does anyone know if .keys and .values are O(1) operations?  Or do 
they have to allocate a new array and copy keys/values?  That could be 
an important thing to know.  Should be part of the spec, I think.

--bb


More information about the Digitalmars-d-learn mailing list