True, I can only work with what I have, and that is the information I have (or rather don't)you deduce your conclusion from *your* imaginary implementation, which is not *mine*.
So I just took what I know from experience to be the "usual" case.
Which was my point all along.This takes O(n). Which is pretty fast. Actually, I think, is the best result when it comes to random order.
I'll go even one step toward you and say, probably, less, as it only searches the next item, and not all.Since hasNext always advance the hidden _iter, the iterator does EXACTLY the same number of iteration over the sequence.
As I said in my previous post:
Which is the core of my argument.Using the filtering iterator approach, you will be looping through at least parts of your set for every iterator operation.
If use your iterator in such a "sterile" example like you posted, the difference is only semantical, as you point out.
But I take it, that if you take the trouble to create an iterator, you will want to use it in real use cases.
This usually involves creating local iterators, and each call to next() on them is going to do a comparison, or many, depending on how far is the desired item from current position of the iterator.
But when you use a filtered list, you perform your filtering only once, and use it in all the places you need to instead of creating a new iterator (which is forced to filter again when you use it).
I know what you meat by that so I want go in length to split hairs.Of course, it's OBVIOUS that the simpler implementation above requires less steps, less calls, and thus is more efficient, but both solution has the same time complexity.
But more function calls, do lower the efficiency as you stated, and in large sets, that can be a major performance hit.
Hope I was clearer this time.
I guess there might be some use cases where filtering iterators make sense, maybe on rather small sets, which are highly diverse (that is, with large amount of types), and in such cases, instead of filtering the set many times (per type) you can just use an filtering iterator.
But for large sets which are not very diverse, I think they are not practical, except for the ease of use (from a developer perspective)
Yes it does, and I agree, that in you case, probably, this will make sense.1. the first is calculated once ONLY if you don't have to change the filtering function often (as I do)
2. it's easy to cache the second example using a QList when you access the data in the second while loop. But in my program, since data and filtering functions are mutable, cache is a minor advantage. Data is asked almost once when it's needed, and the next call is almost sure to access to new data, hence requiring a caching refresh.
I hope this answers your points.





Reply With Quote
That is my *real* use case.

Bookmarks