Bug 10269 – RandomSample should use popFrontExactly, not popFrontN, when skipping across input range

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-04T10:00:00Z
Last change time
2013-06-19T01:21:38Z
Assigned to
nobody
Creator
joseph.wakeling

Comments

Comment #0 by joseph.wakeling — 2013-06-04T10:00:22Z
When RandomSample skips across the input range it currently uses _input.popFrontN(s); This should be changed to popFrontExactly for faster performance. The value of s is guaranteed to be less than the number of elements remaining in _input, except in the case where the user has mistakenly provided too large a value.
Comment #1 by github-bugzilla — 2013-06-16T11:00:16Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/8da0f13596125478b8c3c71ae7fecbe5626fb076 Issue 10269 - RandomSample should use popFrontExactly, not popFrontN, when skipping across input range This small tweak has been accompanied by a couple of extra checks to ensure that users do not request more sample points than are available in the input, which could otherwise be the source of exceptions. There exists one remaining case where RandomSample may fail: if it is given an InputRange without the .length property, and the user indicates that the total number of items available is greater than what the InputRange actually contains. In this case an exception is thrown from std.array.popFront() line 450, "Attempting to popFront() past the end of an array", or from std.array.front() line 624, "Attempting to fetch the front of an empty array." https://github.com/D-Programming-Language/phobos/commit/201e5c2dd17e6c8f151c01276a2545c4d7e0d372 Merge pull request #1342 from WebDrake/randomsample-popfront Issue 10269 - RandomSample should use popFrontExactly, not popFrontN ...