Fix a bug in swap_idx()