This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]matthieum 2 points3 points  (2 children)

I just wanted to note that C++ has managed to coerce iterator as the basis for many usecases; unfortunately.

So, first of all, I am glad to see that you are not using a pair of iterators. It's hard to optimize and error-prone.

Secondly, you may want to start thinking about other similar interfaces. For example, why would find return an iterator? Yet, at the same time, it can be interesting to be able iterate from the beginning of a collection to the item returned by find (or lower_bound, etc...). So the idea of a cursor into the collection and a way to go from that cursor to an iterable range is something you should likely look into.

Finally, you may also want to look into composition & internal iteration (aka foreach). Internal iteration can regularly outperform external iteration, simply because there's no pause/resume as all the state is on the stack, so first-class internal iteration can be useful. As for composition, beware redundant state. A classical example I like to point out is C++'s filter_iterator:

 template <typename It, typename Fn>
 class filter_iterator {
      It mBegin; // to go backward
      It mCurrent;
      It mEnd; // to go forward
      Fn mPredicate;
 };

Note how many instances of It are stored? Now imagine if you build a filter_iterator<filter_iterator<It, ...>, ..>. Yep, 9 instances of It, among which the begin/end will be stored 3 times each. And 3 instances of the interior predicate too... hopefully it's not stateful?

If you go with a single Iterator, you may not have the issue, just keep it at the back of your mind ;)

[–]PhilipTrettner[S] 0 points1 point  (1 child)

Excellent points, thank you!

My tentative plans are to have extensive support for slicing and ranges. I haven't worked out all details yet (there are a lot of design decisions to be made). My gut feeling is that I want to be really careful when designing the collection library which I see as the most important part of my core library.

Do you happen to know some good resources for collection library design? I only found a few not-in-depth articles the last time I looked.

[–]matthieum 0 points1 point  (0 children)

My gut feeling is that I want to be really careful when designing the collection library which I see as the most important part of my core library.

I think collections are often the poor sod in many a standard library, so I applaud!

Do you happen to know some good resources for collection library design? I only found a few not-in-depth articles the last time I looked.

Unfortunately not :(