forked from candid82/joker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
108 lines (92 loc) · 1.8 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
package core
import (
"bytes"
)
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, Bool{B: true})
case *HashMap:
if m.containsKey(obj) {
return false
}
set.m = set.m.Assoc(obj, Bool{B: true}).(Map)
return true
default:
return false
}
}
func (set *MapSet) Conj(obj Object) Conjable {
return &MapSet{m: set.m.Assoc(obj, Bool{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 TYPES["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()
}