[llvm-commits] PATCH: Add an indexed iterator utility to the ADT library, and expose it for SmallVector

Chandler Carruth chandlerc at gmail.com
Fri Aug 3 14:13:32 PDT 2012


On Fri, Aug 3, 2012 at 2:04 PM, Jakob Stoklund Olesen <stoklund at 2pi.dk>wrote:

>
> On Aug 3, 2012, at 1:11 PM, Chandler Carruth <chandlerc at gmail.com> wrote:
>
> > The gist of it is this:
> >
> > Imagine you have a sorted vector, and are processing each element. One
> step of the processing has the option of producing new values that should
> be inserted into the vector. Because they are already processed, they don't
> need to be visited again, and so you simple push_back everything new, and
> sort the vector again at the end.
>
> But how is this iterator abstraction an improvement over 'unsigned'?
>

Well, in a sense, it isn't really improving the abstraction, it's mostly
improving the syntax:

I can write "I->foo()" to access a member rather than
"MyContainerVariable[i].foo()".

I don't have to pass the container to a static helper function.

I can call 'sort' on it and any other standard algorithms.

I can change code from the normal iterator to the indexed iterator without
rewriting if/when the invalidation problem arises. Only a type change is
required.


So is it necessary or more powerful than 'unsigned'? Certainly not. But I
find it extremely convenient, and it made my code significantly more
readable to use it. It allows me to write more code uniformly with
iterators rather than mixing iterator patterns and index patterns.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120803/3e1bbade/attachment.html>


More information about the llvm-commits mailing list