Am I getting this all right?
Bill Baxter
dnewsgroup at billbaxter.com
Thu Dec 14 17:49:53 PST 2006
Jarrett Billingsley wrote:
> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message
> news:elsr1j$lu0$1 at digitaldaemon.com...
>
>> Or just use
>> foreach(v; set.keys)
>> writefln(v);
>
> That prints "true true true". :)
I don't think so. set.values would give you that.
>> 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.
>
> I think it'd be slower for AAs too because in the class opApply function you
> have to use foreach on the AA that holds the data. But really I guess it's
> only one method call, so it probably wouldn't be bad at all. After all,
> custom implementations of opApply work virtually the same way as the
> built-in ones.
>
>> 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.
>
> They allocate new arrays. But when you foreach an AA, it doesn't call them.
Hmm. Ok, so you really do want to use foreach(...; set) if at all possible.
Sure would be nice if there were a .keyiter that could be used with
foreach that was efficient and didn't require allocations.
Too bad the iterator discussions fizzled out without anything getting
decided.
--bb
More information about the Digitalmars-d-learn
mailing list