D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2872 - Length, opIndex for Map
Summary: Length, opIndex for Map
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P4 enhancement
Assignee: Andrei Alexandrescu
URL:
Keywords: patch
: 3871 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-04-21 20:01 UTC by David Simcha
Modified: 2015-06-09 01:26 UTC (History)
2 users (show)

See Also:


Attachments
Revised map (4.73 KB, patch)
2009-10-27 20:58 UTC, David Simcha
Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2009-04-21 20:01:46 UTC
Kind of a meta-enhancement, but it would be nice if all of the generic range types in std.range and std.algorithm would try to have length properties if the range that they're wrapping does.  For example, as far as I can tell, it wouldn't be hard to make Map have a length property that forwards to the underlying range's length property if it exists.

This would primarily be useful when copying generic ranges to an array, as this can be done much more efficiently if the length is known ahead of time and the array can be allocated exactly once, rather than appending.
Comment 1 David Simcha 2009-05-13 06:47:11 UTC
Now that I've played around more with the new Phobos, this is really just an issue with map.  Also, opIndex for map seems like a reasonable proposition.
Comment 2 David Simcha 2009-10-27 20:58:34 UTC
Created attachment 482 [details]
Revised map
Comment 3 David Simcha 2009-10-27 21:00:10 UTC
Here's a revised map that supports bidirectional ranges, slicing, length and random access if the underlying range supports these.  

You may want to note in the docs, however, that random access indexing isn't cached and therefore should only be used frequently if fun is cheap.
Comment 4 David Simcha 2010-06-17 19:28:06 UTC
*** Issue 3871 has been marked as a duplicate of this issue. ***
Comment 5 David Simcha 2010-06-17 19:33:07 UTC
Fixed in SVN.