bearophile wrote:
Bill Baxter:
The rationale for .empty is that .length could be an O(n) operation
for some containers, but .empty should always be O(1). So, the negation of .length is not a general replacement for .empty.

I didn't know isEmpty() or opBool() must be O(1). But I remember
Andrei (and maybe other people too) strongly refuse my len() function
that sometimes returns length (in O(1)) and sometimes iterates all
items of a lazy iterable and returns their number in O(n) or more. So
you may be wrong.

The way Phobos does things is the following:

a) You must define .empty which completes in O(1).

b) If you can define .length with O(1), define it, otherwise don't.

Then Phobos defines walkLength() on a best-effort basis which is guaranteed to finish in O(n) but may finish faster. It uses .length if defined, or else it just iterates the range to exhaustion.

I think this is cleaner than what STL does.


Andrei

Reply via email to