I'm trying to implement a lazy collection wrapper. Figuring out where the first element is, if any, takes O(n). So I cached that location in a lazy stored property. The problem is that the implementation of the immutable startIndex
can't call lazy properties! (They're automatically mutable, even if they're never reset after initialization.)
I guess I could do shenanigans with a class
wrapper for the cache, but I wonder if there are better solutions. One question is if I need to do this at all; since this is going to be a wrapper accessed through LazyCollection
, I could just force a recalculation every time startIndex
is used. But that makes startIndex
, and isEmpty
and underestimatedCount
, O(n) instead of O(1) every time. With my lazy
idea, at least I could get amortized O(1) for all three.