On 2009-07-29 10:37:51 -0400, Andrei Alexandrescu
<[email protected]> said:
Michel Fortin wrote:
On 2009-07-28 19:59:30 -0400, Andrei Alexandrescu
<[email protected]> said:
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.
This looks like a good approach. I'm just not too thrilled by the name
"walkLength". But perhaps I'm the only one.
STL defines distance() but people tend to forget it's O(n). So I chose
a name that makes it rather obvious it's O(n).
In the programming guidelines document I've just posted on the wiki
(see other thread), I'm recommending using the prefix "calc" for any
accessor that performs an expensive operation. For length, that'd mean
"calcLength" (remains to be defined: "expensive operation").
The idea is to have a short word generic enough to apply to any kind of
expensive operation. "walk", while it makes sense for this specific
situation, is not generic enough to be used as a convention. And you're
right: "distance" is much worse than "walkLength".
--
Michel Fortin
[email protected]
http://michelf.com/