github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/contrib/roaring/setutil_generic.go (about)

     1  //go:build !arm64 || gccgo || appengine
     2  // +build !arm64 gccgo appengine
     3  
     4  package roaring
     5  
     6  func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
     7  	pos := 0
     8  	k1 := 0
     9  	k2 := 0
    10  	if 0 == len(set2) {
    11  		buffer = buffer[:len(set1)]
    12  		copy(buffer, set1[:])
    13  		return len(set1)
    14  	}
    15  	if 0 == len(set1) {
    16  		buffer = buffer[:len(set2)]
    17  		copy(buffer, set2[:])
    18  		return len(set2)
    19  	}
    20  	s1 := set1[k1]
    21  	s2 := set2[k2]
    22  	buffer = buffer[:cap(buffer)]
    23  	for {
    24  		if s1 < s2 {
    25  			buffer[pos] = s1
    26  			pos++
    27  			k1++
    28  			if k1 >= len(set1) {
    29  				copy(buffer[pos:], set2[k2:])
    30  				pos += len(set2) - k2
    31  				break
    32  			}
    33  			s1 = set1[k1]
    34  		} else if s1 == s2 {
    35  			buffer[pos] = s1
    36  			pos++
    37  			k1++
    38  			k2++
    39  			if k1 >= len(set1) {
    40  				copy(buffer[pos:], set2[k2:])
    41  				pos += len(set2) - k2
    42  				break
    43  			}
    44  			if k2 >= len(set2) {
    45  				copy(buffer[pos:], set1[k1:])
    46  				pos += len(set1) - k1
    47  				break
    48  			}
    49  			s1 = set1[k1]
    50  			s2 = set2[k2]
    51  		} else { // if (set1[k1]>set2[k2])
    52  			buffer[pos] = s2
    53  			pos++
    54  			k2++
    55  			if k2 >= len(set2) {
    56  				copy(buffer[pos:], set1[k1:])
    57  				pos += len(set1) - k1
    58  				break
    59  			}
    60  			s2 = set2[k2]
    61  		}
    62  	}
    63  	return pos
    64  }