/
setmap.go
96 lines (78 loc) · 2.03 KB
/
setmap.go
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
package set
import (
"fmt"
"strings"
"github.com/timtadh/data-structures/errors"
"github.com/timtadh/data-structures/types"
)
type SetMap struct {
types.Map
}
func NewSetMap(m types.Map) *SetMap {
return &SetMap{m}
}
func (s *SetMap) String() string {
if s.Size() <= 0 {
return "{}"
}
items := make([]string, 0, s.Size())
for item, next := s.Items()(); next != nil; item, next = next() {
items = append(items, fmt.Sprintf("%v", item))
}
return "{" + strings.Join(items, ", ") + "}"
}
func (s *SetMap) Items() types.KIterator {
return s.Keys()
}
// unimplemented
func (s *SetMap) Item(item types.Hashable) (types.Hashable, error) {
return nil, errors.Errorf("un-implemented")
}
// unimplemented
func (s *SetMap) Equals(o types.Equatable) bool {
panic(errors.Errorf("un-implemented"))
}
func (s *SetMap) Add(item types.Hashable) (err error) {
return s.Put(item, nil)
}
func (s *SetMap) Delete(item types.Hashable) (err error) {
_, err = s.Remove(item)
return err
}
func (s *SetMap) Extend(items types.KIterator) (err error) {
for item, next := items(); next != nil; item, next = next() {
err := s.Add(item)
if err != nil {
return err
}
}
return nil
}
// Unions s with o and returns a new SetMap (with a LinearHash)
func (s *SetMap) Union(other types.Set) (types.Set, error) {
return Union(s, other)
}
// Unions s with o and returns a new SetMap (with a LinearHash)
func (s *SetMap) Intersect(other types.Set) (types.Set, error) {
return Intersect(s, other)
}
// Unions s with o and returns a new SetMap (with a LinearHash)
func (s *SetMap) Subtract(other types.Set) (types.Set, error) {
return Subtract(s, other)
}
// Is s a subset of o?
func (s *SetMap) Subset(o types.Set) bool {
return Subset(s, o)
}
// Is s a proper subset of o?
func (s *SetMap) ProperSubset(o types.Set) bool {
return ProperSubset(s, o)
}
// Is s a superset of o?
func (s *SetMap) Superset(o types.Set) bool {
return Superset(s, o)
}
// Is s a proper superset of o?
func (s *SetMap) ProperSuperset(o types.Set) bool {
return ProperSuperset(s, o)
}