Debian Codegraph @

sets/hashset/hashset.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 hashset implements a set backed by a hash table.
6 //
7 // Structure is not thread safe.
8 //
9 // References: http://en.wikipedia.org/wiki/Set_%28abstract_data_type%29
10 package hashset
11
12 import (
13 "fmt"
14 "strings"
15
16 "github.com/emirpasic/gods/v2/sets"
17 )
18
19 // Assert Set implementation
20 var _ sets.Set[int] = (*Set[int])(nil)
21
22 // Set holds elements in go's native map
23 type Set[T comparable] struct {
24 items map[T]struct{}
25 }
26
27 var itemExists = struct{}{}
28
29 // New instantiates a new empty set and adds the passed values, if any, to the set
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 // Add adds the items (one or more) to the set.
39 func (set *Set[T]) Add(items ...T) {
40 for _, item := range items {
41 set.items[item] = itemExists
42 }
43 }
44
45 // Remove removes the items (one or more) from the set.
46 func (set *Set[T]) Remove(items ...T) {
47 for _, item := range items {
48 delete(set.items, item)
49 }
50 }
51
52 // Contains check if items (one or more) are present in the set.
53 // All items have to be present in the set for the method to return true.
54 // Returns true if no arguments are passed at all, i.e. set is always superset of empty set.
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 // Empty returns true if set does not contain any elements.
65 func (set *Set[T]) Empty() bool {
66 return set.Size() == 0
67 }
68
69 // Size returns number of elements within the set.
70 func (set *Set[T]) Size() int {
71 return len(set.items)
72 }
73
74 // Clear clears all values in the set.
75 func (set *Set[T]) Clear() {
76 set.items = make(map[T]struct{})
77 }
78
79 // Values returns all items in the set.
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 // String returns a string representation of container
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 // Intersection returns the intersection between two sets.
102 // The new set consists of all elements that are both in "set" and "another".
103 // Ref: https://en.wikipedia.org/wiki/Intersection_(set_theory)
104 func (set *Set[T]) Intersection(another *Set[T]) *Set[T] {
105 result := New[T]()
106
107 // Iterate over smaller set (optimization)
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 // Union returns the union of two sets.
126 // The new set consists of all elements that are in "set" or "another" (possibly both).
127 // Ref: https://en.wikipedia.org/wiki/Union_(set_theory)
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 // Difference returns the difference between two sets.
142 // The new set consists of all elements that are in "set" but not in "another".
143 // Ref: https://proofwiki.org/wiki/Definition:Set_Difference
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 }