mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 21:38:05 -05:00
Fix Slice Union Helpers for Variadic Arguments (#3228)
* union changes * slice util fixes
This commit is contained in:
@@ -62,16 +62,17 @@ func UnionUint64(s ...[]uint64) []uint64 {
|
||||
if len(s) == 1 {
|
||||
return s[0]
|
||||
}
|
||||
set := make([]uint64, 0)
|
||||
set := s[0]
|
||||
m := make(map[uint64]bool)
|
||||
for i := 1; i < len(s); i++ {
|
||||
for j := 0; j < len(s[i-1]); j++ {
|
||||
m[s[i-1][j]] = true
|
||||
set = append(set, s[i-1][j])
|
||||
a := s[i-1]
|
||||
b := s[i]
|
||||
for j := 0; j < len(a); j++ {
|
||||
m[a[j]] = true
|
||||
}
|
||||
for j := 0; j < len(s[i]); j++ {
|
||||
if _, found := m[s[i][j]]; !found {
|
||||
set = append(set, s[i][j])
|
||||
for j := 0; j < len(b); j++ {
|
||||
if _, found := m[b[j]]; !found {
|
||||
set = append(set, b[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,16 +158,17 @@ func UnionInt64(s ...[]int64) []int64 {
|
||||
if len(s) == 1 {
|
||||
return s[0]
|
||||
}
|
||||
set := make([]int64, 0)
|
||||
set := s[0]
|
||||
m := make(map[int64]bool)
|
||||
for i := 1; i < len(s); i++ {
|
||||
for j := 0; j < len(s[i-1]); j++ {
|
||||
m[s[i-1][j]] = true
|
||||
set = append(set, s[i-1][j])
|
||||
a := s[i-1]
|
||||
b := s[i]
|
||||
for j := 0; j < len(a); j++ {
|
||||
m[a[j]] = true
|
||||
}
|
||||
for j := 0; j < len(s[i]); j++ {
|
||||
if _, found := m[s[i][j]]; !found {
|
||||
set = append(set, s[i][j])
|
||||
for j := 0; j < len(b); j++ {
|
||||
if _, found := m[b[j]]; !found {
|
||||
set = append(set, b[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,6 +204,29 @@ func IsInInt64(a int64, b []int64) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// UnionByteSlices returns the common elements between sets of byte slices.
|
||||
func UnionByteSlices(s ...[][]byte) [][]byte {
|
||||
if len(s) == 0 {
|
||||
return [][]byte{}
|
||||
}
|
||||
if len(s) == 1 {
|
||||
return s[0]
|
||||
}
|
||||
set := s[0]
|
||||
m := make(map[string]bool)
|
||||
for i := 1; i < len(s); i++ {
|
||||
for j := 0; j < len(s[i-1]); j++ {
|
||||
m[string(s[i-1][j])] = true
|
||||
}
|
||||
for j := 0; j < len(s[i]); j++ {
|
||||
if _, found := m[string(s[i][j])]; !found {
|
||||
set = append(set, s[i][j])
|
||||
}
|
||||
}
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// IntersectionByteSlices returns the common elements between sets of byte slices.
|
||||
func IntersectionByteSlices(s ...[][]byte) [][]byte {
|
||||
if len(s) == 0 {
|
||||
|
||||
@@ -23,7 +23,6 @@ func TestSubsetUint64(t *testing.T) {
|
||||
if result != tt.out {
|
||||
t.Errorf("%v, got %v, want %v", tt.setA, result, tt.out)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +46,6 @@ func TestIntersectionUint64(t *testing.T) {
|
||||
if !reflect.DeepEqual(result, tt.out) {
|
||||
t.Errorf("got %d, want %d", result, tt.out)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +64,6 @@ func TestIsSortedUint64(t *testing.T) {
|
||||
if result != tt.out {
|
||||
t.Errorf("got %v, want %v", result, tt.out)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,9 +87,7 @@ func TestIntersectionInt64(t *testing.T) {
|
||||
if !reflect.DeepEqual(result, tt.out) {
|
||||
t.Errorf("got %d, want %d", result, tt.out)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUnionUint64(t *testing.T) {
|
||||
@@ -116,6 +111,16 @@ func TestUnionUint64(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
items := [][]uint64{
|
||||
{3, 4, 5},
|
||||
{6, 7, 8},
|
||||
{9, 10, 11},
|
||||
}
|
||||
variadicResult := UnionUint64(items...)
|
||||
want := []uint64{3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
if !reflect.DeepEqual(want, variadicResult) {
|
||||
t.Errorf("Received %v, wanted %v", variadicResult, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnionInt64(t *testing.T) {
|
||||
@@ -137,9 +142,17 @@ func TestUnionInt64(t *testing.T) {
|
||||
if !reflect.DeepEqual(result, tt.out) {
|
||||
t.Errorf("got %d, want %d", result, tt.out)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
items := [][]int64{
|
||||
{3, 4, 5},
|
||||
{6, 7, 8},
|
||||
{9, 10, 11},
|
||||
}
|
||||
variadicResult := UnionInt64(items...)
|
||||
want := []int64{3, 4, 5, 6, 7, 8, 9, 10, 11}
|
||||
if !reflect.DeepEqual(want, variadicResult) {
|
||||
t.Errorf("Received %v, wanted %v", variadicResult, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotUint64(t *testing.T) {
|
||||
@@ -226,6 +239,51 @@ func TestIsInInt64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnionByteSlices(t *testing.T) {
|
||||
testCases := []struct {
|
||||
setA [][]byte
|
||||
setB [][]byte
|
||||
out [][]byte
|
||||
}{
|
||||
{
|
||||
[][]byte{[]byte("hello"), []byte("world")},
|
||||
[][]byte{[]byte("world"), {}},
|
||||
[][]byte{[]byte("hello"), []byte("world"), {}},
|
||||
},
|
||||
{
|
||||
[][]byte{[]byte("hello")},
|
||||
[][]byte{[]byte("hello")},
|
||||
[][]byte{[]byte("hello")},
|
||||
},
|
||||
}
|
||||
for _, tt := range testCases {
|
||||
result := UnionByteSlices(tt.setA, tt.setB)
|
||||
if !reflect.DeepEqual(result, tt.out) {
|
||||
t.Errorf("got %d, want %d", result, tt.out)
|
||||
}
|
||||
}
|
||||
items := [][][]byte{
|
||||
{
|
||||
{1, 2, 3},
|
||||
},
|
||||
{
|
||||
{4, 5, 6},
|
||||
},
|
||||
{
|
||||
{7, 8, 9},
|
||||
},
|
||||
}
|
||||
variadicResult := UnionByteSlices(items...)
|
||||
want := [][]byte{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9},
|
||||
}
|
||||
if !reflect.DeepEqual(want, variadicResult) {
|
||||
t.Errorf("Received %v, wanted %v", variadicResult, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntersectionByteSlices(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input [][][]byte
|
||||
|
||||
Reference in New Issue
Block a user