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