improved implementation of rle & inverse_rle