Generalize repetition optimization for the flow

Instead of just reusing the iterations directly when plus.length ===
 minus.length, apply the same strategy whenever we need to add and remove
iterations. We are always able to do this with min(plus.length, 
minus.length) iterations.
Merge pull request #1390 from @aadsm

Generalize Repetition optimizations for Flow.