Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upOptimise `IteratorRandom::choose` for `size_hint` or `ExactSizeIterator` #511
Comments
dhardy
added
T-sequences
C-optimisation
E-easy
labels
Jun 15, 2018
This comment has been minimized.
This comment has been minimized.
|
Opinions on what we should do if So for example, a simple implementation strategy would be to check if |
This comment has been minimized.
This comment has been minimized.
|
To quote the doc:
Beyond that there's not much guidance; i.e. I think it should be acceptable to return "incorrect" results if |
This comment has been minimized.
This comment has been minimized.
|
There’s an exception for |
This comment has been minimized.
This comment has been minimized.
|
Yes, but does "may return whatever" mean:
|
This comment has been minimized.
This comment has been minimized.
|
There are no restrictions to So, while, there is a side-effect to using For Given a deterministic backing random number generator (
|
This comment has been minimized.
This comment has been minimized.
|
Ultimately, however, it is up to you to decide what the semantics are when |
This comment has been minimized.
This comment has been minimized.
If we have the above requirement I don't see that we could take advantage of But we can always wait until specialization stabilizes and use |
This comment has been minimized.
This comment has been minimized.
|
I suppose those constraints are necessary to avoid a change in The alternative is just to point out that the implementation of |
This comment has been minimized.
This comment has been minimized.
|
I agree that pointing out that Likewise to point out that If there's agreement that that's ok, then I'm happy to write up a PR? |
This comment has been minimized.
This comment has been minimized.
|
I don't think it needs to panic, but I think it's acceptable to introduce bias in the output (i.e. select some element as a fallback in case the iterator is not as long as Perhaps though we should bring this up elsewhere to get more opinions. I don't really know where people might write incorrect |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@sicking the answer seems to be that we're allowed to break things if |
dhardy commentedJun 15, 2018
IteratorRandom::chooseuses one random number per item since it has no way of knowing how many items will follow.We do not use
size_hintsince it is not guaranteed that the supplied bounds are correct; though since incorrect implementations are considered buggy we could try.Alternatively
ExactSizeIteratorcould be used, but probably not without specialisation.