-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intersect returns items that exist in all lists. It returns slice without any duplicates. If zero slice arguments are provided, then nil is returned. Fixes #71 Updates #72
- Loading branch information
1 parent
bf85395
commit 7ff9a21
Showing
10 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package functions | ||
|
||
// Intersect returns items that exist in all lists. | ||
// | ||
// It returns slice without any duplicates. | ||
// If zero slice arguments are provided, then nil is returned. | ||
func (ss SliceType) Intersect(slices ...SliceType) (ss2 SliceType) { | ||
if slices == nil { | ||
return nil | ||
} | ||
|
||
var uniqs = make([]map[ElementType]struct{}, len(slices)) | ||
for i := 0; i < len(slices); i++ { | ||
m := make(map[ElementType]struct{}) | ||
for _, el := range slices[i] { | ||
m[el] = struct{}{} | ||
} | ||
uniqs[i] = m | ||
} | ||
|
||
var containsInAll = false | ||
for _, el := range ss.Unique() { | ||
for _, u := range uniqs { | ||
if _, exists := u[el]; !exists { | ||
containsInAll = false | ||
break | ||
} | ||
containsInAll = true | ||
} | ||
if containsInAll { | ||
ss2 = append(ss2, el) | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.