Fix piecewise curve problems turned up by llvm.

LLVM is more picky than GCC about creating and using invalid iterators.
These changes stive to avoid those situations entirely.