Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a C++ class which models an object. This object has a list of data, which can be quite large. And retrieving this data can be expensive. So when i need to access the items in the list, my object will return one batch at a time (say, 2Kb of data).

Is there a way to write a custom iterator or enumerator so i can loop over all items and have the iterator/enumerator handle getting each batch? I'm not sure if iterators are used for this purpose, i don't have much experience with the standard C++ iterators.

Basically, i want to be able to do something like this:
Model::custom_iterator i;
for (i = model.items.begin(); i != model.items.end(); i++) {
    process item *i;
}


And in the Model class (or the iterator class), there would be some code that would get a batch of data and cache it, then when more data is required it gets the next batch. The iterator would then be able to access the items in the list, and request the next batch when it needs it.


I would also like to use a similar technique for iterating over a list that is expensive to generate (ancestors of a model with a parent/child relationship (read: tree)), so that i don't have to create the list before i iterate over it.
Posted
Comments
XTAL256 18-Jan-11 21:14pm    
Since some of you mentioned C# and Boost, i should mention that i am using the Qt framework.

To expand on my question, if i had a Model class which gets the batches, would i create an iterator class in a similar manner to Qt's (e.g. class iterator defined inside QList) and override the ++ operator to handle paging?

You may find this helpful The Boost.Iterator Library[^] - it will help to implement an iterator correctly.

If your data is comming from a database you can wrap the "result/cursor" handle in an iterator. Like OTL, the Oracle, Odbc and DB2-CLI Template Library[^]

One approach could be to use a vector for the "batches", wrap the iterator for the vector in a new custom iterator and get next batch each time the inner itertor reaches the end of the vector.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 18-Jan-11 17:26pm    
Thanks for the link! 5+
Nish Nishant 18-Jan-11 17:39pm    
5! Useful extra info that adds to the above response.
A simple answer to a complex question: Yes that is what iterators are here for after all. You can build an enumerator that would effectively return all of N or the fibonacci series or the faculty function. How much of that series is in fact consumed depends on the code the user runs (and the data type involved of course as you might hit a limit there).

The following site deals with an enumerator in C# but applies also to C++:http://thoughtfulcode.wordpress.com/2011/01/14/ienumerable-is-lazy-and-thats-cool/[^]

Modification:

XTAL256 wrote:
there would be some code that would get a batch of data and cache it, then when more data is required it gets the next batch

I actually missed this part in your question and want to elaborate a bit on the possibilities enumerators give you: Since you have total control over what the enumerators do internally you can also implement some kind of paging mechanism that will fetch batches of data as you put it. As long as you understand and implement all of the IEmumerable interface all of this can be transparent to the user of your code. Why did I say "can be transparent to the user"?
It means you can hide all the "behind the curtain" details from the user or else give him some control by defining some constructors for your IEnumerable implementing class that let him specify fetched batch size etc.

End Modification

Best Regards,
Manfred
 
Share this answer
 
v4
Comments
Espen Harlinn 18-Jan-11 17:12pm    
5+ Right
Nish Nishant 18-Jan-11 17:38pm    
5! Proposed as answer.
Manfred Rudolf Bihy 18-Jan-11 17:45pm    
Thanks Nishant! Is this "proposing of an answer" a privilege (and responsibility) that comes with accumulated reputation points or just an unadulterated piece of advice to OP. :)
Nish Nishant 18-Jan-11 17:47pm    
I got the habit from the MSDN forums where you have a Propose-as-answer button that can be used to advise the OP that this may be the most appropriate answer among all the responses he got. Obviously I don't really know if the OP even bothers reading such comments or if he did, whether he takes them seriously :-)
Manfred Rudolf Bihy 18-Jan-11 17:54pm    
Well thank you then for the proposal! (OP seems to have followd your advice)
I think some time ago I read something just about that in the "Site Bugs and Suggestions" forum. If I'm mistaken though it should definitely be proposed to also have this kind of button here on CP which should depend on a certain level of Authority.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900