| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
package hashset |
| 11 |
|
| 12 |
import ( |
| 13 |
"fmt" |
| 14 |
"strings" |
| 15 |
|
| 16 |
"github.com/emirpasic/gods/v2/sets" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
var _ sets.Set[int] = (*Set[int])(nil) |
| 21 |
|
| 22 |
|
| 23 |
type Set[T comparable] struct { |
| 24 |
items map[T]struct{} |
| 25 |
} |
| 26 |
|
| 27 |
var itemExists = struct{}{} |
| 28 |
|
| 29 |
|
| 30 |
func New[T comparable](values ...T) *Set[T] { |
| 31 |
set := &Set[T]{items: make(map[T]struct{})} |
| 32 |
if len(values) > 0 { |
| 33 |
set.Add(values...) |
| 34 |
} |
| 35 |
return set |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (set *Set[T]) Add(items ...T) { |
| 40 |
for _, item := range items { |
| 41 |
set.items[item] = itemExists |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (set *Set[T]) Remove(items ...T) { |
| 47 |
for _, item := range items { |
| 48 |
delete(set.items, item) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func (set *Set[T]) Contains(items ...T) bool { |
| 56 |
for _, item := range items { |
| 57 |
if _, contains := set.items[item]; !contains { |
| 58 |
return false |
| 59 |
} |
| 60 |
} |
| 61 |
return true |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func (set *Set[T]) Empty() bool { |
| 66 |
return set.Size() == 0 |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (set *Set[T]) Size() int { |
| 71 |
return len(set.items) |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
func (set *Set[T]) Clear() { |
| 76 |
set.items = make(map[T]struct{}) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func (set *Set[T]) Values() []T { |
| 81 |
values := make([]T, set.Size()) |
| 82 |
count := 0 |
| 83 |
for item := range set.items { |
| 84 |
values[count] = item |
| 85 |
count++ |
| 86 |
} |
| 87 |
return values |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (set *Set[T]) String() string { |
| 92 |
str := "HashSet\n" |
| 93 |
items := []string{} |
| 94 |
for k := range set.items { |
| 95 |
items = append(items, fmt.Sprintf("%v", k)) |
| 96 |
} |
| 97 |
str += strings.Join(items, ", ") |
| 98 |
return str |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
func (set *Set[T]) Intersection(another *Set[T]) *Set[T] { |
| 105 |
result := New[T]() |
| 106 |
|
| 107 |
|
| 108 |
if set.Size() <= another.Size() { |
| 109 |
for item := range set.items { |
| 110 |
if _, contains := another.items[item]; contains { |
| 111 |
result.Add(item) |
| 112 |
} |
| 113 |
} |
| 114 |
} else { |
| 115 |
for item := range another.items { |
| 116 |
if _, contains := set.items[item]; contains { |
| 117 |
result.Add(item) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return result |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func (set *Set[T]) Union(another *Set[T]) *Set[T] { |
| 129 |
result := New[T]() |
| 130 |
|
| 131 |
for item := range set.items { |
| 132 |
result.Add(item) |
| 133 |
} |
| 134 |
for item := range another.items { |
| 135 |
result.Add(item) |
| 136 |
} |
| 137 |
|
| 138 |
return result |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
func (set *Set[T]) Difference(another *Set[T]) *Set[T] { |
| 145 |
result := New[T]() |
| 146 |
|
| 147 |
for item := range set.items { |
| 148 |
if _, contains := another.items[item]; !contains { |
| 149 |
result.Add(item) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return result |
| 154 |
} |