-
Notifications
You must be signed in to change notification settings - Fork 67
/
set.go
156 lines (137 loc) · 2.71 KB
/
set.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package core
import (
"bytes"
"fmt"
"io"
)
type (
Set interface {
Conjable
Gettable
Disjoin(key Object) Set
}
MapSet struct {
InfoHolder
MetaHolder
m Map
}
)
func (v *MapSet) WithMeta(meta Map) Object {
res := *v
res.meta = SafeMerge(res.meta, meta)
return &res
}
func (set *MapSet) Disjoin(key Object) Set {
return &MapSet{m: set.m.Without(key)}
}
func (set *MapSet) Add(obj Object) bool {
switch m := set.m.(type) {
case *ArrayMap:
return m.Add(obj, Boolean{B: true})
case *HashMap:
if m.containsKey(obj) {
return false
}
set.m = set.m.Assoc(obj, Boolean{B: true}).(Map)
return true
default:
return false
}
}
func (set *MapSet) Conj(obj Object) Conjable {
return &MapSet{m: set.m.Assoc(obj, Boolean{B: true}).(Map)}
}
func EmptySet() *MapSet {
return &MapSet{m: EmptyArrayMap()}
}
func (set *MapSet) ToString(escape bool) string {
var b bytes.Buffer
b.WriteString("#{")
for iter := iter(set.m.Keys()); iter.HasNext(); {
b.WriteString(iter.Next().ToString(escape))
if iter.HasNext() {
b.WriteRune(' ')
}
}
b.WriteRune('}')
return b.String()
}
func (set *MapSet) Equals(other interface{}) bool {
switch otherSet := other.(type) {
case *MapSet:
return set.m.Equals(otherSet.m)
default:
return false
}
}
func (set *MapSet) Get(key Object) (bool, Object) {
if ok, _ := set.m.Get(key); ok {
return true, key
}
return false, nil
}
func (seq *MapSet) GetType() *Type {
return TYPE.MapSet
}
func (set *MapSet) Hash() uint32 {
return hashUnordered(set.Seq(), 2)
}
func (set *MapSet) Seq() Seq {
return set.m.Keys()
}
func (set *MapSet) Count() int {
return set.m.Count()
}
func (set *MapSet) Call(args []Object) Object {
CheckArity(args, 1, 1)
if ok, _ := set.Get(args[0]); ok {
return args[0]
}
return NIL
}
func (set *MapSet) Empty() Collection {
return EmptySet()
}
func NewSetFromSeq(s Seq) *MapSet {
res := EmptySet()
for !s.IsEmpty() {
res.Add(s.First())
s = s.Rest()
}
return res
}
func (set *MapSet) Pprint(w io.Writer, indent int) int {
i := indent + 1
fmt.Fprint(w, "#{")
for iter := iter(set.m.Keys()); iter.HasNext(); {
i = pprintObject(iter.Next(), indent+2, w)
if iter.HasNext() {
fmt.Fprint(w, "\n")
writeIndent(w, indent+2)
}
}
fmt.Fprint(w, "}")
return i + 1
}
func (set *MapSet) Format(w io.Writer, indent int) int {
i := indent + 2
fmt.Fprint(w, "#{")
var prevObj Object
for iter := iter(set.m.Keys()); iter.HasNext(); {
obj := iter.Next()
if prevObj != nil {
i = maybeNewLine(w, prevObj, obj, indent+2, i)
}
i = formatObject(obj, i, w)
prevObj = obj
}
if prevObj != nil {
if isComment(prevObj) {
fmt.Fprint(w, "\n")
writeIndent(w, indent+2)
i = indent + 2
}
}
fmt.Fprint(w, "}")
return i + 1
}