int rle_canonize( struct rle_item *begin, int length ) { const struct rle_item *read = begin; struct rle_item *write = begin; while ( length > 0 ) { struct rle_item collapsed = *read; while ( --length > 0 ) { ++ read; if ( read->value == collapsed.value ) collapsed.count += read->count; else if ( read->count ) break; } if ( collapsed.count ) *write++ = collapsed; } return write - begin; }