We build too many walls and not enough bridges
This commit is contained in:
19
pkg/algo/algo.go
Normal file
19
pkg/algo/algo.go
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
// Package algo provides some algorithm utils.
|
||||
package algo
|
||||
47
pkg/algo/sets.go
Normal file
47
pkg/algo/sets.go
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package algo
|
||||
|
||||
import "reflect"
|
||||
|
||||
// SetIntersection complexity: O(n^2), could be better but this is simple enough
|
||||
func SetIntersection(a, b interface{}, eq func(a, b interface{}) bool) []interface{} {
|
||||
set := make([]interface{}, 0)
|
||||
av := reflect.ValueOf(a)
|
||||
|
||||
for i := 0; i < av.Len(); i++ {
|
||||
el := av.Index(i).Interface()
|
||||
if contains(b, el, eq) {
|
||||
set = append(set, el)
|
||||
}
|
||||
}
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func contains(a, e interface{}, eq func(a, b interface{}) bool) bool {
|
||||
v := reflect.ValueOf(a)
|
||||
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
if eq(v.Index(i).Interface(), e) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
71
pkg/algo/sets_test.go
Normal file
71
pkg/algo/sets_test.go
Normal file
@ -0,0 +1,71 @@
|
||||
// Copyright (c) 2020 Proton Technologies AG
|
||||
//
|
||||
// This file is part of ProtonMail Bridge.
|
||||
//
|
||||
// ProtonMail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// ProtonMail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package algo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
k, v int
|
||||
}
|
||||
|
||||
func TestSetIntersection(t *testing.T) {
|
||||
keysAreEqual := func(a, b interface{}) bool {
|
||||
return a.(T).k == b.(T).k
|
||||
}
|
||||
|
||||
type args struct {
|
||||
a interface{}
|
||||
b interface{}
|
||||
eq func(a, b interface{}) bool
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want interface{}
|
||||
}{
|
||||
{
|
||||
name: "integer sets",
|
||||
args: args{a: []int{1, 2, 3}, b: []int{3, 4, 5}, eq: func(a, b interface{}) bool { return a == b }},
|
||||
want: []int{3},
|
||||
},
|
||||
{
|
||||
name: "string sets",
|
||||
args: args{a: []string{"1", "2", "3"}, b: []string{"3", "4", "5"}, eq: func(a, b interface{}) bool { return a == b }},
|
||||
want: []string{"3"},
|
||||
},
|
||||
{
|
||||
name: "custom comp, only compare on keys, prefer first set if keys are the same",
|
||||
args: args{a: []T{{k: 1, v: 1}, {k: 2, v: 2}}, b: []T{{k: 2, v: 1234}, {k: 3, v: 3}}, eq: keysAreEqual},
|
||||
want: []T{{k: 2, v: 2}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// using cmp.Equal because it handles the interfaces correctly; testify/assert doesn't
|
||||
// treat these as equal because their types are different ([]interface vs []int)
|
||||
if got := SetIntersection(tt.args.a, tt.args.b, tt.args.eq); cmp.Equal(got, tt.want) {
|
||||
t.Errorf("SetIntersection() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user