From 9fcf8e29214210612d545bed50d7f889800ac639 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sun, 1 May 2022 21:50:12 +0300 Subject: Initial --- collections/doc.go | 2 ++ collections/merge.go | 41 +++++++++++++++++++++ collections/merge_test.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 collections/doc.go create mode 100644 collections/merge.go create mode 100644 collections/merge_test.go (limited to 'collections') diff --git a/collections/doc.go b/collections/doc.go new file mode 100644 index 0000000..ebb2322 --- /dev/null +++ b/collections/doc.go @@ -0,0 +1,2 @@ +// Функции работы над обобщенными коллекциями +package collections diff --git a/collections/merge.go b/collections/merge.go new file mode 100644 index 0000000..6fcf875 --- /dev/null +++ b/collections/merge.go @@ -0,0 +1,41 @@ +package collections + +import "go.neonxp.dev/extra/utils" + +// MergeScalar стабильное слияние двух слайсов скаляров +func MergeScalar[T utils.Scalar](s1, s2 []T) []T { + i, j := 0, 0 + len1, len2 := len(s1), len(s2) + result := make([]T, 0, len1+len2) + for i < len1 && j < len2 { + if s1[i] < s2[j] { + result = append(result, s1[i]) + i++ + continue + } + result = append(result, s2[j]) + j++ + } + result = append(result, s1[i:]...) + result = append(result, s2[j:]...) + return result +} + +// Merge стабильное слияние двух слайсов +func Merge[T utils.Sortable[T]](s1, s2 []T) []T { + i, j := 0, 0 + len1, len2 := len(s1), len(s2) + result := make([]T, 0, len1+len2) + for i < len1 && j < len2 { + if s1[i].Less(s2[j]) { + result = append(result, s1[i]) + i++ + continue + } + result = append(result, s2[j]) + j++ + } + result = append(result, s1[i:]...) + result = append(result, s2[j:]...) + return result +} diff --git a/collections/merge_test.go b/collections/merge_test.go new file mode 100644 index 0000000..27f5ba0 --- /dev/null +++ b/collections/merge_test.go @@ -0,0 +1,92 @@ +package collections + +import ( + "reflect" + "testing" +) + +func TestMergeScalar(t *testing.T) { + type args struct { + s1 []int + s2 []int + } + tests := []struct { + name string + args args + want []int + }{ + { + name: "test 1", + args: args{ + s1: []int{1, 2, 4, 5, 6}, + s2: []int{3, 3}, + }, + want: []int{1, 2, 3, 3, 4, 5, 6}, + }, + { + name: "test 2", + args: args{ + s1: []int{1, 3, 5, 7}, + s2: []int{0, 2, 4, 6, 8}, + }, + want: []int{0, 1, 2, 3, 4, 5, 6, 7, 8}, + }, + { + name: "test 3", + args: args{ + s1: []int{8, 6, 4, 2, 0}, + s2: []int{1, 3, 5, 7}, + }, + want: []int{1, 3, 5, 7, 8, 6, 4, 2, 0}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MergeScalar(tt.args.s1, tt.args.s2); !reflect.DeepEqual(got, tt.want) { + t.Errorf("MergeScalar() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestMerge(t *testing.T) { + type args struct { + s1 []*myType + s2 []*myType + } + tests := []struct { + name string + args args + want []*myType + }{ + { + name: "test1", + args: args{ + s1: []*myType{ + {400}, {200}, {100}, + }, + s2: []*myType{ + {300}, + }, + }, + want: []*myType{ + {400}, {300}, {200}, {100}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Merge(tt.args.s1, tt.args.s2); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Merge() = %v, want %v", got, tt.want) + } + }) + } +} + +type myType struct { + Weight int +} + +func (t *myType) Less(t2 *myType) bool { + return t2.Weight < t.Weight +} -- cgit v1.2.3