[PATCH] Add a wrapper to iterator to return the index of the iterator

Pete Cooper peter_cooper at apple.com
Tue Apr 7 15:02:50 PDT 2015


Hi all

This patch adds a wrapper to iterators to allow us to use foreach loops, but also get the index of elements.  This allows a greater number of loops to use foreach, or avoid a foreach with a separate index.

I’ve updated a loop in AArch64A57FPLoadBalancing to show this.  The gist of the change is that the loop goes from

  unsigned Idx = 0;
  for (auto &MI : MBB)
    scanInstruction(&MI, Idx++, ActiveChains, AllChains);

to

  for (auto MI : IndexedIterator(MBB))
    scanInstruction(&*MI, MI.index(), ActiveChains, AllChains);

So accesses to the iterator need a *, and there’s a new index() method.  I considered adding a .value() to the iterator instead of *.  I’m not strongly attached to one over the other (or both).

Comments welcome.  I personally like the idea wrapping iterators, but i understand if anyone is fundamentally opposed.  I’d really like to add another wrapper later which keeps track of the next pointer for you as then the current element can be safely deleted.

Cheers,
Pete

-------------- next part --------------
A non-text attachment was scrubbed...
Name: indexed_iterator.diff
Type: application/octet-stream
Size: 10653 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150407/b7697ef7/attachment.obj>
-------------- next part --------------





More information about the llvm-commits mailing list