Skip to content

Commit 5a8a876

Browse files
committed
Convert Set and FrozenSet into pointer types
1 parent e1cecbb commit 5a8a876

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

marshal/marshal.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,11 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
211211
return py.NewListFromItems(tuple), nil
212212
}
213213

214-
set := make(py.Set, len(tuple))
215-
for _, obj := range tuple {
216-
set[obj] = py.SetValue{}
217-
}
218214
switch Type {
219215
case TYPE_SET:
220-
return py.Set(set), nil
216+
return py.NewSetFromItems(tuple), nil
221217
case TYPE_FROZENSET:
222-
return py.FrozenSet(set), nil
218+
return py.NewFrozenSetFromItems(tuple), nil
223219
}
224220
case TYPE_DICT:
225221
// FIXME should be py.Dict

py/set.go

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,71 @@
11
// Set and FrozenSet types
2+
//
3+
// FIXME preliminary implementation only - doesn't work properly!
24

35
package py
46

57
var SetType = NewType("set", "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements.")
68

79
type SetValue struct{}
810

9-
type Set map[Object]SetValue
11+
type Set struct {
12+
items map[Object]SetValue
13+
}
1014

1115
// Type of this Set object
12-
func (o Set) Type() *Type {
16+
func (o *Set) Type() *Type {
1317
return SetType
1418
}
1519

20+
// Make a new empty set
21+
func NewSet() *Set {
22+
return &Set{
23+
items: make(map[Object]SetValue),
24+
}
25+
}
26+
27+
// Make a new empty set with capacity for n items
28+
func NewSetWithCapacity(n int) *Set {
29+
return &Set{
30+
items: make(map[Object]SetValue, n),
31+
}
32+
}
33+
34+
// Make a new set with the items passed in
35+
func NewSetFromItems(items []Object) *Set {
36+
s := NewSetWithCapacity(len(items))
37+
for _, item := range items {
38+
s.items[item] = SetValue{}
39+
}
40+
return s
41+
}
42+
43+
// Add an item to the set
44+
func (s *Set) Add(item Object) {
45+
s.items[item] = SetValue{}
46+
}
47+
1648
var FrozenSetType = NewType("frozenset", "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements.")
1749

18-
type FrozenSet map[Object]SetValue
50+
type FrozenSet struct {
51+
Set
52+
}
1953

2054
// Type of this FrozenSet object
21-
func (o FrozenSet) Type() *Type {
55+
func (o *FrozenSet) Type() *Type {
2256
return FrozenSetType
2357
}
58+
59+
// Make a new empty frozen set
60+
func NewFrozenSet() *FrozenSet {
61+
return &FrozenSet{
62+
Set: *NewSet(),
63+
}
64+
}
65+
66+
// Make a new set with the items passed in
67+
func NewFrozenSetFromItems(items []Object) *FrozenSet {
68+
return &FrozenSet{
69+
Set: *NewSetFromItems(items),
70+
}
71+
}

0 commit comments

Comments
 (0)