Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
063ff667
Commit
063ff667
authored
Feb 13, 2015
by
Tim Hockin
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4422 from derekwaynecarr/set_difference
Set should have a difference function
parents
3001a1de
e7a0340a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
0 deletions
+35
-0
set.go
pkg/util/set.go
+16
-0
set_test.go
pkg/util/set_test.go
+19
-0
No files found.
pkg/util/set.go
View file @
063ff667
...
...
@@ -75,6 +75,22 @@ func (s StringSet) HasAll(items ...string) bool {
return
true
}
// Difference returns a set of objects that are not in s2
// For example:
// s1 = {1, 2, 3}
// s2 = {1, 2, 4, 5}
// s1.Difference(s2) = {3}
// s2.Difference(s1) = {4, 5}
func
(
s
StringSet
)
Difference
(
s2
StringSet
)
StringSet
{
result
:=
NewStringSet
()
for
key
:=
range
s
{
if
!
s2
.
Has
(
key
)
{
result
.
Insert
(
key
)
}
}
return
result
}
// IsSuperset returns true iff s1 is a superset of s2.
func
(
s1
StringSet
)
IsSuperset
(
s2
StringSet
)
bool
{
for
item
:=
range
s2
{
...
...
pkg/util/set_test.go
View file @
063ff667
...
...
@@ -98,3 +98,22 @@ func TestStringSetList(t *testing.T) {
t
.
Errorf
(
"List gave unexpected result: %#v"
,
s
.
List
())
}
}
func
TestStringSetDifference
(
t
*
testing
.
T
)
{
a
:=
NewStringSet
(
"1"
,
"2"
,
"3"
)
b
:=
NewStringSet
(
"1"
,
"2"
,
"4"
,
"5"
)
c
:=
a
.
Difference
(
b
)
d
:=
b
.
Difference
(
a
)
if
len
(
c
)
!=
1
{
t
.
Errorf
(
"Expected len=1: %d"
,
len
(
c
))
}
if
!
c
.
Has
(
"3"
)
{
t
.
Errorf
(
"Unexpected contents: %#v"
,
c
.
List
())
}
if
len
(
d
)
!=
2
{
t
.
Errorf
(
"Expected len=2: %d"
,
len
(
d
))
}
if
!
d
.
Has
(
"4"
)
||
!
d
.
Has
(
"5"
)
{
t
.
Errorf
(
"Unexpected contents: %#v"
,
d
.
List
())
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment