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
5de212ed
Commit
5de212ed
authored
Sep 18, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14134 from eparis/string-set-intersection
Auto commit by PR queue bot
parents
3eabb81e
fc9d3c9d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
0 deletions
+108
-0
set.go
pkg/util/sets/set.go
+23
-0
set_test.go
pkg/util/sets/set_test.go
+85
-0
No files found.
pkg/util/sets/set.go
View file @
5de212ed
...
...
@@ -121,6 +121,29 @@ func (s1 String) Union(s2 String) String {
return
result
}
// Intersection returns a new set which includes the item in BOTH s1 and s2
// For example:
// s1 = {1, 2}
// s2 = {2, 3}
// s1.Intersection(s2) = {2}
func
(
s1
String
)
Intersection
(
s2
String
)
String
{
var
walk
,
other
String
result
:=
NewString
()
if
s1
.
Len
()
<
s2
.
Len
()
{
walk
=
s1
other
=
s2
}
else
{
walk
=
s2
other
=
s1
}
for
key
:=
range
walk
{
if
other
.
Has
(
key
)
{
result
.
Insert
(
key
)
}
}
return
result
}
// IsSuperset returns true if and only if s1 is a superset of s2.
func
(
s1
String
)
IsSuperset
(
s2
String
)
bool
{
for
item
:=
range
s2
{
...
...
pkg/util/sets/set_test.go
View file @
5de212ed
...
...
@@ -183,3 +183,88 @@ func TestStringSetEquals(t *testing.T) {
t
.
Errorf
(
"Expected to be not-equal: %v vs %v"
,
a
,
b
)
}
}
func
TestStringUnion
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
s1
String
s2
String
expected
String
}{
{
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(
"3"
,
"4"
,
"5"
,
"6"
),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
),
},
{
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
},
{
NewString
(),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
},
{
NewString
(),
NewString
(),
NewString
(),
},
}
for
_
,
test
:=
range
tests
{
union
:=
test
.
s1
.
Union
(
test
.
s2
)
if
union
.
Len
()
!=
test
.
expected
.
Len
()
{
t
.
Errorf
(
"Expected union.Len()=%d but got %d"
,
test
.
expected
.
Len
(),
union
.
Len
())
}
if
!
union
.
Equal
(
test
.
expected
)
{
t
.
Errorf
(
"Expected union.Equal(expected) but not true. union:%v expected:%v"
,
union
.
List
(),
test
.
expected
.
List
())
}
}
}
func
TestStringIntersection
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
s1
String
s2
String
expected
String
}{
{
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(
"3"
,
"4"
,
"5"
,
"6"
),
NewString
(
"3"
,
"4"
),
},
{
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
},
{
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(),
NewString
(),
},
{
NewString
(),
NewString
(
"1"
,
"2"
,
"3"
,
"4"
),
NewString
(),
},
{
NewString
(),
NewString
(),
NewString
(),
},
}
for
_
,
test
:=
range
tests
{
intersection
:=
test
.
s1
.
Intersection
(
test
.
s2
)
if
intersection
.
Len
()
!=
test
.
expected
.
Len
()
{
t
.
Errorf
(
"Expected intersection.Len()=%d but got %d"
,
test
.
expected
.
Len
(),
intersection
.
Len
())
}
if
!
intersection
.
Equal
(
test
.
expected
)
{
t
.
Errorf
(
"Expected intersection.Equal(expected) but not true. intersection:%v expected:%v"
,
intersection
.
List
(),
test
.
expected
.
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