MCPcopy
hub / github.com/expr-lang/expr / diffLists

Function diffLists

internal/testify/assert/assertions.go:1120–1155  ·  view source on GitHub ↗

diffLists diffs two arrays/slices and returns slices of elements that are only in A and only in B. If some element is present multiple times, each instance is counted separately (e.g. if something is 2x in A and 5x in B, it will be 0x in extraA and 3x in extraB). The order of items in both lists is

(listA, listB interface{})

Source from the content-addressed store, hash-verified

1118// If some element is present multiple times, each instance is counted separately (e.g. if something is 2x in A and
1119// 5x in B, it will be 0x in extraA and 3x in extraB). The order of items in both lists is ignored.
1120func diffLists(listA, listB interface{}) (extraA, extraB []interface{}) {
1121 aValue := reflect.ValueOf(listA)
1122 bValue := reflect.ValueOf(listB)
1123
1124 aLen := aValue.Len()
1125 bLen := bValue.Len()
1126
1127 // Mark indexes in bValue that we already used
1128 visited := make([]bool, bLen)
1129 for i := 0; i < aLen; i++ {
1130 element := aValue.Index(i).Interface()
1131 found := false
1132 for j := 0; j < bLen; j++ {
1133 if visited[j] {
1134 continue
1135 }
1136 if ObjectsAreEqual(bValue.Index(j).Interface(), element) {
1137 visited[j] = true
1138 found = true
1139 break
1140 }
1141 }
1142 if !found {
1143 extraA = append(extraA, element)
1144 }
1145 }
1146
1147 for j := 0; j < bLen; j++ {
1148 if visited[j] {
1149 continue
1150 }
1151 extraB = append(extraB, bValue.Index(j).Interface())
1152 }
1153
1154 return
1155}
1156
1157func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) string {
1158 var msg bytes.Buffer

Callers 2

TestDiffListsFunction · 0.85
ElementsMatchFunction · 0.85

Calls 2

ObjectsAreEqualFunction · 0.85
LenMethod · 0.45

Tested by 1

TestDiffListsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…