Range length property

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 10 20:16:18 UTC 2018


On 4/10/18 4:08 PM, Jonathan M Davis wrote:
> On Tuesday, April 10, 2018 19:47:10 Nordlöw via Digitalmars-d-learn wrote:
>> I'm thinking of my own container Hashmap having its range
>> ByKeyValue requiring one extra word of memory to store the
>> iteration count which, in turn, can be used to calculate the
>> length of the remaining range. Is this motivated?
> 
> That would depend entirely on what you're trying to do, but in general, if a
> range has length, then some algorithms will be more efficient, and some
> algorithms do require length.

e.g. std.array.array is going to pre-allocate an array of the correct 
length and fill it in, vs. appending each element as it gets them from 
the range.

Personally, I would store the length because typically a container range 
is short-lived. It also jives with the container itself which likely has 
O(1) length.

-Steve


More information about the Digitalmars-d-learn mailing list