~james/core

core/sets/set.go -rw-r--r-- 2.7 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package sets

import (
	"iter"
	"maps"
)

type Set[T comparable] struct {
	data map[T]struct{}
}

func New[T comparable]() Set[T] {
	return Set[T]{
		data: make(map[T]struct{}),
	}
}

func (s *Set[T]) Insert(item T) bool {
	_, exists := s.data[item]
	s.data[item] = struct{}{}
	return !exists
}

func Singleton[T comparable](item T) Set[T] {
	n := New[T]()
	_ = n.Insert(item)
	return n
}

func (s *Set[T]) Remove(item T) bool {
	_, exists := s.data[item]
	if exists {
		delete(s.data, item)
	}
	return exists
}

func (s Set[T]) Contains(item T) bool {
	_, exists := s.data[item]
	return exists
}

func (s Set[T]) Len() int {
	return len(s.data)
}

func (s Set[T]) IsEmpty() bool {
	return len(s.data) == 0
}

func (s *Set[T]) Clear() {
	s.data = make(map[T]struct{})
}

func (s Set[T]) All() iter.Seq[T] {
	return func(yield func(T) bool) {
		for item := range s.data {
			if !yield(item) {
				return
			}
		}
	}
}

func (s Set[T]) Clone() Set[T] {
	return Set[T]{
		data: maps.Clone(s.data),
	}
}

func (s Set[T]) Union(other Set[T]) iter.Seq[T] {
	if s.Len() >= other.Len() {
		return chain(s.All(), other.Difference(s))
	} else {
		return chain(other.All(), s.Difference(other))
	}
}

func chain[T any](seqs ...iter.Seq[T]) iter.Seq[T] {
	return func(yield func(T) bool) {
		for _, seq := range seqs {
			for item := range seq {
				if !yield(item) {
					return
				}
			}
		}
	}
}

func (s Set[T]) Intersection(other Set[T]) iter.Seq[T] {
	return func(yield func(T) bool) {
		for item := range s.data {
			if other.Contains(item) {
				if !yield(item) {
					return
				}
			}
		}
	}
}

func (s Set[T]) Difference(other Set[T]) iter.Seq[T] {
	return func(yield func(T) bool) {
		for item := range s.data {
			if !other.Contains(item) {
				if !yield(item) {
					return
				}
			}
		}
	}
}

func (s Set[T]) SymmetricDifference(other Set[T]) iter.Seq[T] {
	return func(yield func(T) bool) {
		for item := range s.data {
			if !other.Contains(item) {
				if !yield(item) {
					return
				}
			}
		}
		for item := range other.data {
			if !s.Contains(item) {
				if !yield(item) {
					return
				}
			}
		}
	}
}

func (s Set[T]) IsSubset(other Set[T]) bool {
	for item := range s.data {
		if !other.Contains(item) {
			return false
		}
	}
	return true
}

func (s Set[T]) IsSuperset(other Set[T]) bool {
	return other.IsSubset(s)
}

func (s Set[T]) IsDisjoint(other Set[T]) bool {
	for item := range s.data {
		if other.Contains(item) {
			return false
		}
	}
	return true
}

func (s Set[T]) Equal(other Set[T]) bool {
	if s.Len() != other.Len() {
		return false
	}
	for item := range s.data {
		if !other.Contains(item) {
			return false
		}
	}
	return true
}

func Collect[T comparable](seq iter.Seq[T]) Set[T] {
	result := New[T]()
	for item := range seq {
		result.Insert(item)
	}
	return result
}