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
|
// Copyright 2015 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package mapset provides a reasonable map-based set implementation for
// use in comparative benchmarks, and to check arbitrary fuzz outputs.
// mapset is not intended for reuse.
package mapset
import "sort"
func New(s []int) Set {
t := make(Set, len(s))
for _, v := range s {
t[v] = struct{}{}
}
return t
}
type Set map[int]struct{}
func (s Set) Union(t Set) Set {
for v := range t {
s[v] = struct{}{}
}
return s
}
func (s Set) Inter(t Set) Set {
for v := range s {
_, ok := t[v]
if !ok {
delete(s, v)
}
}
return s
}
func (s Set) Diff(t Set) Set {
for v := range t {
delete(s, v)
}
return s
}
func (s Set) SymDiff(t Set) Set {
for v := range t {
_, ok := s[v]
if ok {
delete(s, v)
} else {
s[v] = struct{}{}
}
}
return s
}
func (s Set) IsSub(t Set) bool {
if len(s) > len(t) {
return false
}
for k := range s {
_, ok := t[k]
if !ok {
return false
}
}
return true
}
func (s Set) IsSuper(t Set) bool {
return t.IsSub(s)
}
func (s Set) IsInter(t Set) bool {
for k := range s {
_, ok := t[k]
if ok {
return true
}
}
return false
}
func (s Set) IsEqual(t Set) bool {
if len(s) != len(t) {
return false
}
return s.IsSub(t)
}
func (s Set) Elems() []int {
t := make([]int, 0, len(s))
for v := range s {
t = append(t, v)
}
sort.Ints(t)
return t
}
func (s Set) Copy() Set {
t := make(Set, len(s))
t.Union(s)
return t
}
|