2018-12-13 16:35:50 +00:00
|
|
|
package slices
|
|
|
|
|
|
|
|
// Intersection of two uint32 slices with time
|
|
|
|
// complexity of approximately O(n) leveraging a map to
|
|
|
|
// check for element existence off by a constant factor
|
|
|
|
// of underlying map efficiency.
|
|
|
|
func Intersection(a []uint32, b []uint32) []uint32 {
|
|
|
|
set := make([]uint32, 0)
|
|
|
|
m := make(map[uint32]bool)
|
|
|
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
m[a[i]] = true
|
|
|
|
}
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
if _, found := m[b[i]]; found {
|
|
|
|
set = append(set, b[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
2018-12-19 04:55:36 +00:00
|
|
|
|
|
|
|
// Union of two uint32 slices with time
|
|
|
|
// complexity of approximately O(n) leveraging a map to
|
|
|
|
// check for element existence off by a constant factor
|
|
|
|
// of underlying map efficiency.
|
|
|
|
func Union(a []uint32, b []uint32) []uint32 {
|
|
|
|
set := make([]uint32, 0)
|
|
|
|
m := make(map[uint32]bool)
|
|
|
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
m[a[i]] = true
|
|
|
|
set = append(set, a[i])
|
|
|
|
}
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
if _, found := m[b[i]]; !found {
|
|
|
|
set = append(set, b[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
2018-12-26 15:46:06 +00:00
|
|
|
|
|
|
|
// Not returns the uint32 in slice a that are
|
|
|
|
// not in slice b with time complexity of approximately
|
|
|
|
// O(n) leveraging a map to check for element existence
|
|
|
|
// off by a constant factor of underlying map efficiency.
|
|
|
|
func Not(a []uint32, b []uint32) []uint32 {
|
|
|
|
set := make([]uint32, 0)
|
|
|
|
m := make(map[uint32]bool)
|
|
|
|
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
m[a[i]] = true
|
|
|
|
}
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
if _, found := m[b[i]]; !found {
|
|
|
|
set = append(set, b[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
2018-12-27 05:27:35 +00:00
|
|
|
|
|
|
|
// IsIn returns true if a is in b and False otherwise.
|
|
|
|
func IsIn(a uint32, b []uint32) bool {
|
|
|
|
for _, v := range b {
|
|
|
|
if a == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|