|
1 | 1 | // Set and FrozenSet types
|
| 2 | +// |
| 3 | +// FIXME preliminary implementation only - doesn't work properly! |
2 | 4 |
|
3 | 5 | package py
|
4 | 6 |
|
5 | 7 | var SetType = NewType("set", "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements.")
|
6 | 8 |
|
7 | 9 | type SetValue struct{}
|
8 | 10 |
|
9 |
| -type Set map[Object]SetValue |
| 11 | +type Set struct { |
| 12 | + items map[Object]SetValue |
| 13 | +} |
10 | 14 |
|
11 | 15 | // Type of this Set object
|
12 |
| -func (o Set) Type() *Type { |
| 16 | +func (o *Set) Type() *Type { |
13 | 17 | return SetType
|
14 | 18 | }
|
15 | 19 |
|
| 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 | + |
16 | 48 | var FrozenSetType = NewType("frozenset", "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements.")
|
17 | 49 |
|
18 |
| -type FrozenSet map[Object]SetValue |
| 50 | +type FrozenSet struct { |
| 51 | + Set |
| 52 | +} |
19 | 53 |
|
20 | 54 | // Type of this FrozenSet object
|
21 |
| -func (o FrozenSet) Type() *Type { |
| 55 | +func (o *FrozenSet) Type() *Type { |
22 | 56 | return FrozenSetType
|
23 | 57 | }
|
| 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