diff --git a/setutil_generic.go b/setutil_generic.go index 9edcc902..9fcdd0b6 100644 --- a/setutil_generic.go +++ b/setutil_generic.go @@ -2,10 +2,13 @@ package roaring + func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int { - pos := 0 - k1 := 0 - k2 := 0 + pos := uint(0) + k1 := uint(0) + k2 := uint(0) + len1 := uint(len(set1)) + len2 := uint(len(set2)) if 0 == len(set2) { buffer = buffer[:len(set1)] copy(buffer, set1[:]) @@ -18,16 +21,16 @@ func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int { } s1 := set1[k1] s2 := set2[k2] - buffer = buffer[:cap(buffer)] - for { + buffer = buffer[:len1 + len2] + for pos < uint(len(buffer)) { if s1 < s2 { buffer[pos] = s1 pos++ k1++ - if k1 >= len(set1) { + if k1 >= len1 { copy(buffer[pos:], set2[k2:]) - pos += len(set2) - k2 - break + pos += len2 - k2 + return int(pos) } s1 = set1[k1] } else if s1 == s2 { @@ -35,14 +38,14 @@ func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int { pos++ k1++ k2++ - if k1 >= len(set1) { + if k1 >= len1 { copy(buffer[pos:], set2[k2:]) - pos += len(set2) - k2 + pos += len2 - k2 break } - if k2 >= len(set2) { + if k2 >= len2 { copy(buffer[pos:], set1[k1:]) - pos += len(set1) - k1 + pos += len1 - k1 break } s1 = set1[k1] @@ -51,13 +54,13 @@ func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int { buffer[pos] = s2 pos++ k2++ - if k2 >= len(set2) { + if k2 >= len2 { copy(buffer[pos:], set1[k1:]) - pos += len(set1) - k1 + pos += len1 - k1 break } s2 = set2[k2] } } - return pos + return int(pos) }