File: ordered_set.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (217 lines) | stat: -rw-r--r-- 5,349 bytes parent folder | download | duplicates (4)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package tools

// OrderedSet is a unique set of strings that maintains insertion order.
type OrderedSet struct {
	// s is the set of strings that we're keeping track of.
	s []string
	// m is a mapping of string value "s" into the index "i" that that
	// string is present in in the given "s".
	m map[string]int
}

// NewOrderedSet creates an ordered set with no values.
func NewOrderedSet() *OrderedSet {
	return NewOrderedSetWithCapacity(0)
}

// NewOrderedSetWithCapacity creates a new ordered set with no values. The
// returned ordered set can be appended to "capacity" number of times before it
// grows internally.
func NewOrderedSetWithCapacity(capacity int) *OrderedSet {
	return &OrderedSet{
		s: make([]string, 0, capacity),
		m: make(map[string]int, capacity),
	}
}

// NewOrderedSetFromSlice returns a new ordered set with the elements given in
// the slice "s".
func NewOrderedSetFromSlice(s []string) *OrderedSet {
	set := NewOrderedSetWithCapacity(len(s))
	for _, e := range s {
		set.Add(e)
	}

	return set
}

// Add adds the given element "i" to the ordered set, unless the element is
// already present. It returns whether or not the element was added.
func (s *OrderedSet) Add(i string) bool {
	if _, ok := s.m[i]; ok {
		return false
	}

	s.s = append(s.s, i)
	s.m[i] = len(s.s) - 1

	return true
}

// Contains returns whether or not the given "i" is contained in this ordered
// set. It is a constant-time operation.
func (s *OrderedSet) Contains(i string) bool {
	if _, ok := s.m[i]; ok {
		return true
	}
	return false
}

// ContainsAll returns whether or not all of the given items in "i" are present
// in the ordered set.
func (s *OrderedSet) ContainsAll(i ...string) bool {
	for _, item := range i {
		if !s.Contains(item) {
			return false
		}
	}
	return true
}

// IsSubset returns whether other is a subset of this ordered set. In other
// words, it returns whether or not all of the elements in "other" are also
// present in this set.
func (s *OrderedSet) IsSubset(other *OrderedSet) bool {
	for _, i := range other.s {
		if !s.Contains(i) {
			return false
		}
	}
	return true
}

// IsSuperset returns whether or not this set is a superset of "other". In other
// words, it returns whether or not all of the elements in this set are also in
// the set "other".
func (s *OrderedSet) IsSuperset(other *OrderedSet) bool {
	return other.IsSubset(s)
}

// Union returns a union of this set with the given set "other". It returns the
// items that are in either set while maintaining uniqueness constraints. It
// preserves ordered within each set, and orders the elements in this set before
// the elements in "other".
//
// It is an O(n+m) operation.
func (s *OrderedSet) Union(other *OrderedSet) *OrderedSet {
	union := NewOrderedSetWithCapacity(other.Cardinality() + s.Cardinality())

	for _, e := range s.s {
		union.Add(e)
	}
	for _, e := range other.s {
		union.Add(e)
	}

	return union
}

// Intersect returns the elements that are in both this set and then given
// "ordered" set. It is an O(min(n, m)) (in other words, O(n)) operation.
func (s *OrderedSet) Intersect(other *OrderedSet) *OrderedSet {
	intersection := NewOrderedSetWithCapacity(MinInt(
		s.Cardinality(), other.Cardinality()))

	if s.Cardinality() < other.Cardinality() {
		for _, elem := range s.s {
			if other.Contains(elem) {
				intersection.Add(elem)
			}
		}
	} else {
		for _, elem := range other.s {
			if s.Contains(elem) {
				intersection.Add(elem)
			}
		}
	}

	return intersection
}

// Difference returns the elements that are in this set, but not included in
// other.
func (s *OrderedSet) Difference(other *OrderedSet) *OrderedSet {
	diff := NewOrderedSetWithCapacity(s.Cardinality())
	for _, e := range s.s {
		if !other.Contains(e) {
			diff.Add(e)
		}
	}

	return diff
}

// SymmetricDifference returns the elements that are not present in both sets.
func (s *OrderedSet) SymmetricDifference(other *OrderedSet) *OrderedSet {
	left := s.Difference(other)
	right := other.Difference(s)

	return left.Union(right)
}

// Clear removes all elements from this set.
func (s *OrderedSet) Clear() {
	s.s = make([]string, 0)
	s.m = make(map[string]int, 0)
}

// Remove removes the given element "i" from this set.
func (s *OrderedSet) Remove(i string) {
	idx, ok := s.m[i]
	if !ok {
		return
	}

	rest := MinInt(idx+1, len(s.s)-1)

	s.s = append(s.s[:idx], s.s[rest:]...)
	for _, e := range s.s[rest:] {
		s.m[e] = s.m[e] - 1
	}
	delete(s.m, i)
}

// Cardinality returns the cardinality of this set.
func (s *OrderedSet) Cardinality() int {
	return len(s.s)
}

// Iter returns a channel which yields the elements in this set in insertion
// order.
func (s *OrderedSet) Iter() <-chan string {
	c := make(chan string)
	go func() {
		for _, i := range s.s {
			c <- i
		}
		close(c)
	}()

	return c
}

// Equal returns whether this element has the same number, identity and ordering
// elements as given in "other".
func (s *OrderedSet) Equal(other *OrderedSet) bool {
	if s.Cardinality() != other.Cardinality() {
		return false
	}

	for e, i := range s.m {
		if ci, ok := other.m[e]; !ok || ci != i {
			return false
		}
	}

	return true
}

// Clone returns a deep copy of this set.
func (s *OrderedSet) Clone() *OrderedSet {
	clone := NewOrderedSetWithCapacity(s.Cardinality())
	for _, i := range s.s {
		clone.Add(i)
	}
	return clone
}