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{})
| 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. |
| 1120 | func 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 | |
| 1157 | func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) string { |
| 1158 | var msg bytes.Buffer |
searching dependent graphs…