From d5e443e8e3609fe38586aed942a3dae3343dbe47 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Mon, 4 Nov 2019 19:47:25 +1100 Subject: Support building and moving patches WIP --- pkg/utils/utils.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'pkg/utils') diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index f184c0bbb..bf69fd30e 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -261,3 +261,41 @@ func AsJson(i interface{}) string { bytes, _ := json.MarshalIndent(i, "", " ") return string(bytes) } + +// UnionInt returns the union of two int arrays +func UnionInt(a, b []int) []int { + m := make(map[int]bool) + + for _, item := range a { + m[item] = true + } + + for _, item := range b { + if _, ok := m[item]; !ok { + // this does not mutate the original a slice + // though it does mutate the backing array I believe + // but that doesn't matter because if you later want to append to the + // original a it must see that the backing array has been changed + // and create a new one + a = append(a, item) + } + } + return a +} + +// DifferenceInt returns the difference of two int arrays +func DifferenceInt(a, b []int) []int { + result := []int{} + m := make(map[int]bool) + + for _, item := range b { + m[item] = true + } + + for _, item := range a { + if _, ok := m[item]; !ok { + result = append(result, item) + } + } + return result +} -- cgit v1.2.3