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