get rid of implicit vec copies in treemap iterator

Each call to next() was doing a copy rather than a move. There's
currently no way for this to be a method that uses &mut self, so it has
to be a free function. Closes #4763.