bug icon indicating copy to clipboard operation
bug copied to clipboard

Champ (Map/Set) Iterators should expose their knownSize

Open joshlemer opened this issue 6 years ago • 3 comments

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

joshlemer avatar Jul 02 '19 17:07 joshlemer

(-.-)

dwijnand avatar Jul 02 '19 20:07 dwijnand

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 avatar Jul 05 '19 03:07 NthPortal

@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.

joshlemer avatar Jul 05 '19 14:07 joshlemer