bug
bug copied to clipboard
Champ (Map/Set) Iterators should expose their knownSize
All iterators over the CHAMP Maps/Sets currently expose a knownSize of -1. Since Maps/Sets cache their size, it should at the very least be possible to track how many calls to next() have occurred, and decrement a counter. Or if that is too inconvenient (since there are many overrides of next() for each variant of the Iterator), then perhaps atleast return rootNode.size in the case where the Iterator is still in starting position.
scala> import collection.immutable._
import collection.immutable._
scala> HashMap(1 -> 1, 2 -> 2).iterator.knownSize
res1: Int = -1
scala> HashSet(1,2).iterator.knownSize
res2: Int = -1
(-.-)
it's not clear to me how much effort should be put into making iterators know their size, especially if only before they're iterated over
@NthPortal I'm just thinking that in the vast majority of cases, knownSize is called on a fresh iterator in order to preallocate or short circuit, and then never called again, so if it's too costly to maintain a count throughout the iteration, we'll still get almost all the benefits by knowing the size at the start.