From 0facc5a5596ad7a18ab2957a8f27d1e58e61d258 Mon Sep 17 00:00:00 2001 From: ka-weihe Date: Sun, 11 Apr 2021 23:36:02 +0200 Subject: [PATCH] builtin: fix map.zeros_to_end (#9689) --- vlib/builtin/map.v | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 47dbba29ba..c92ab3b1b3 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -175,14 +175,16 @@ fn (mut d DenseArray) zeros_to_end() { if d.has_index(i) { // swap (TODO: optimize) unsafe { - // Swap keys - C.memcpy(tmp_key, d.key(count), d.key_bytes) - C.memcpy(d.key(count), d.key(i), d.key_bytes) - C.memcpy(d.key(i), tmp_key, d.key_bytes) - // Swap values - C.memcpy(tmp_value, d.value(count), d.value_bytes) - C.memcpy(d.value(count), d.value(i), d.value_bytes) - C.memcpy(d.value(i), tmp_value, d.value_bytes) + if count != i { + // Swap keys + C.memcpy(tmp_key, d.key(count), d.key_bytes) + C.memcpy(d.key(count), d.key(i), d.key_bytes) + C.memcpy(d.key(i), tmp_key, d.key_bytes) + // Swap values + C.memcpy(tmp_value, d.value(count), d.value_bytes) + C.memcpy(d.value(count), d.value(i), d.value_bytes) + C.memcpy(d.value(i), tmp_value, d.value_bytes) + } } count++ }