Skip to content

Commit

Permalink
Added Hashset methods with HS. ToList. Minor List update.
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
vladimir2217 committed Jul 22, 2024
1 parent 051cc22 commit a96c249
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
32 changes: 32 additions & 0 deletions Collections/Hashset.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ func (this *Hashset[K]) UnionWith(other *[]K) {
}
}

func (this *Hashset[K]) UnionWithHS(other *Hashset[K]) {
for k, _ := range *other._imap {
this.Add(k.(K))
}
}

func (this *Hashset[K]) ExceptWithHS(other *Hashset[K]) {
for k, _ := range *other._imap {
this.Remove(k.(K))
}
}

func (this *Hashset[K]) IntersectWithHS(other *Hashset[K]) {
res := map[interface{}]bool{}

for k, _ := range *other._imap {
if this.Contains(k.(K)) {
res[k] = false
}
}

this._imap = &res
}

func (this *Hashset[K]) ToList() List[K] {
r := NewList[K]()
for k := range *this._imap {
r.Add(k.(K))
}
return r
}

func (this *Hashset[K]) Clone() *Hashset[K] {
res := NewHashset[K]()
res._imap = CopyMap(this._imap)
Expand Down
4 changes: 2 additions & 2 deletions Collections/List.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func (list *List[T]) Clone() *List[T] {
return &newList
}

// GetSlice returns a copy of the slice.
func (list List[T]) GetSlice() []T {
// ToSlice returns a copy of the slice.
func (list List[T]) ToSlice() []T {
res := make([]T, len(list._ilist._arr))
copy(res, list._ilist._arr)
return res
Expand Down
65 changes: 65 additions & 0 deletions Tests/Collections/Hashset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ func Test_ExceptWith(t *testing.T) {

}

func Test_ExceptWithHS(t *testing.T) {
hs := Collections.NewHashset[int]()
hs.Add(100)
hs.Add(20)
hs.Add(3)

hs2 := Collections.NewHashset[int]()
hs2.Add(100)
hs2.Add(3)

hs.ExceptWithHS(hs2)
assert.Equal(t, 1, hs.Size())
assert.True(t, hs.Contains(20))

}

func Test_IntersectWith(t *testing.T) {
hs := Collections.NewHashset[int]()
hs.Add(100)
Expand All @@ -88,6 +104,23 @@ func Test_IntersectWith(t *testing.T) {

}

func Test_IntersectWithHS(t *testing.T) {
hs := Collections.NewHashset[int]()
hs.Add(100)
hs.Add(20)
hs.Add(3)

hs2 := Collections.NewHashset[int]()
hs2.Add(100)
hs2.Add(3)

hs.IntersectWithHS(hs2)
assert.Equal(t, 2, hs.Size())
assert.True(t, hs.Contains(100))
assert.True(t, hs.Contains(3))

}

func Test_UnionWith(t *testing.T) {
hs := Collections.NewHashset[int]()
hs.Add(100)
Expand All @@ -101,6 +134,38 @@ func Test_UnionWith(t *testing.T) {

}

func Test_UnionWithHS(t *testing.T) {
hs := Collections.NewHashset[int]()
hs.Add(100)
hs.Add(20)
hs.Add(3)

hs2 := Collections.NewHashset[int]()
hs2.Add(33)
hs2.Add(44)

hs.UnionWithHS(hs2)
assert.Equal(t, 5, hs.Size())
assert.True(t, hs.Contains(33))
assert.True(t, hs.Contains(44))

}

func Test_ToList(t *testing.T) {
hs1 := Collections.NewHashset[int]()
hs1.Add(100)
hs1.Add(20)
hs1.Add(3)

li := hs1.ToList()

assert.Equal(t, 3, li.Size())
assert.Equal(t, 100, li.Get(0))
assert.Equal(t, 20, li.Get(1))
assert.Equal(t, 3, li.Get(2))

}

func Test_Clone(t *testing.T) {
hs1 := Collections.NewHashset[int]()
hs1.Add(100)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Collections/List_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func Test_GetClone_no_impact_to_original(t *testing.T) {
func Test_GetSlice_no_impact_to_original(t *testing.T) {
list := Collections.NewList[int]()
list.AddRange([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
res := list.GetSlice()
res := list.ToSlice()
assert.Equal(t, 10, len(res))
assert.Equal(t, 4, res[3])
assert.Equal(t, 5, res[4])
Expand Down

0 comments on commit a96c249

Please sign in to comment.