Debian Codegraph @

sets/treeset/treeset.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package treeset implements a tree backed by a red-black tree.
6 //
7 // Structure is not thread safe.
8 //
9 // Reference: http://en.wikipedia.org/wiki/Set_%28abstract_data_type%29
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 // Assert Set implementation
24 var _ sets.Set[int] = (*Set[int])(nil)
25
26 // Set holds elements in a red-black tree
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 // NewWith instantiates a new empty set with the custom comparator.
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 // Add adds the items (one or more) to the set.
47 func (set *Set[T]) Add(items ...T) {
48 for _, item := range items {
49 set.tree.Put(item, itemExists)
50 }
51 }
52
53 // Remove removes the items (one or more) from the set.
54 func (set *Set[T]) Remove(items ...T) {
55 for _, item := range items {
56 set.tree.Remove(item)
57 }
58 }
59
60 // Contains checks weather items (one or more) are present in the set.
61 // All items have to be present in the set for the method to return true.
62 // Returns true if no arguments are passed at all, i.e. set is always superset of empty set.
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 // Empty returns true if set does not contain any elements.
73 func (set *Set[T]) Empty() bool {
74 return set.tree.Size() == 0
75 }
76
77 // Size returns number of elements within the set.
78 func (set *Set[T]) Size() int {
79 return set.tree.Size()
80 }
81
82 // Clear clears all values in the set.
83 func (set *Set[T]) Clear() {
84 set.tree.Clear()
85 }
86
87 // Values returns all items in the set.
88 func (set *Set[T]) Values() []T {
89 return set.tree.Keys()
90 }
91
92 // String returns a string representation of container
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 // Intersection returns the intersection between two sets.
104 // The new set consists of all elements that are both in "set" and "another".
105 // The two sets should have the same comparators, otherwise the result is empty set.
106 // Ref: https://en.wikipedia.org/wiki/Intersection_(set_theory)
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 // Iterate over smaller set (optimization)
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 // Union returns the union of two sets.
135 // The new set consists of all elements that are in "set" or "another" (possibly both).
136 // The two sets should have the same comparators, otherwise the result is empty set.
137 // Ref: https://en.wikipedia.org/wiki/Union_(set_theory)
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 // Difference returns the difference between two sets.
158 // The two sets should have the same comparators, otherwise the result is empty set.
159 // The new set consists of all elements that are in "set" but not in "another".
160 // Ref: https://proofwiki.org/wiki/Definition:Set_Difference
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 }