From bf7a4e729f13a21cb69b940fccf038f75f7c3f45 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 9 Feb 2022 11:40:08 +0800 Subject: [PATCH] feat: add func Contain, Union, Intersection --- datastructure/list.go | 44 ++++++++++++++++++++++++++++++++++---- datastructure/list_test.go | 30 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/datastructure/list.go b/datastructure/list.go index ffaa9ef..86bf843 100644 --- a/datastructure/list.go +++ b/datastructure/list.go @@ -44,6 +44,17 @@ func (l *List[T]) IndexOf(value T) int { return index } +// Contain checks if the value in the list or not +func (l *List[T]) Contain(value T) bool { + data := l.data + for _, v := range data { + if reflect.DeepEqual(v, value) { + return true + } + } + return false +} + // Push append value to the list data func (l *List[T]) Push(value T) { l.data = append(l.data, value) @@ -141,14 +152,14 @@ func (l *List[T]) IsEmpty() bool { return len(l.data) == 0 } -// Clone return a copy of list +// Clear the data of list func (l *List[T]) Clear() { - l.data = make([]T, 0) + l.data = make([]T, 0, 0) } // Clone return a copy of list func (l *List[T]) Clone() *List[T] { - cl := &List[T]{data: make([]T, len(l.data))} + cl := NewList(make([]T, len(l.data))) copy(cl.data, l.data) return cl @@ -157,7 +168,8 @@ func (l *List[T]) Clone() *List[T] { // Merge two list, return new list, don't change original list func (l *List[T]) Merge(other *List[T]) *List[T] { l1, l2 := len(l.data), len(other.data) - ml := &List[T]{data: make([]T, l1+l2, l1+l2)} + ml := NewList(make([]T, l1+l2, l1+l2)) + data := append([]T{}, append(l.data, other.data...)...) ml.data = data @@ -207,3 +219,27 @@ func (l *List[T]) Unique() { l.data = uniqueData } + +// Union creates a new list contain all element in list l and other, remove duplicate element. +func (l *List[T]) Union(other *List[T]) *List[T] { + res := NewList([]T{}) + + res.data = append(res.data, l.data...) + res.data = append(res.data, other.data...) + res.Unique() + + return res +} + +// Intersection creates a new list whose element both be contained in list l and other +func (l *List[T]) Intersection(other *List[T]) *List[T] { + res := NewList(make([]T, 0, 0)) + + for _, v := range l.data { + if other.Contain(v) { + res.data = append(res.data, v) + } + } + + return res +} diff --git a/datastructure/list_test.go b/datastructure/list_test.go index 3000f67..457b535 100644 --- a/datastructure/list_test.go +++ b/datastructure/list_test.go @@ -36,6 +36,14 @@ func TestIndexOf(t *testing.T) { assert.Equal(-1, i) } +func TestContain(t *testing.T) { + assert := internal.NewAssert(t, "TestContain") + + list := NewList([]int{1, 2, 3}) + assert.Equal(true, list.Contain(1)) + assert.Equal(false, list.Contain(0)) +} + func TestPush(t *testing.T) { assert := internal.NewAssert(t, "TestPush") @@ -240,3 +248,25 @@ func TestUnique(t *testing.T) { assert.Equal(true, expected.EqutalTo(list)) } + +func TestUnion(t *testing.T) { + assert := internal.NewAssert(t, "TestUnion") + + list1 := NewList([]int{1, 2, 3, 4}) + list2 := NewList([]int{4, 5, 6}) + expected := NewList([]int{1, 2, 3, 4, 5, 6}) + + list3 := list1.Union(list2) + assert.Equal(true, expected.EqutalTo(list3)) +} + +func TestIntersection(t *testing.T) { + assert := internal.NewAssert(t, "TestIntersection") + + list1 := NewList([]int{1, 2, 3, 4}) + list2 := NewList([]int{4, 5, 6}) + expected := NewList([]int{4}) + + list3 := list1.Intersection(list2) + assert.Equal(true, expected.EqutalTo(list3)) +}