-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathset.go
126 lines (106 loc) · 3.56 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
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include "utils.h"
// static inline int setCheck(PyObject *o) { return PySet_Check(o); }
// static inline int frozenSetCheck(PyObject *o) { return PyFrozenSet_Check(o); }
// static inline int frozenSetCheckE(PyObject *o) { return PyFrozenSet_CheckExact(o); }
import "C"
import "unsafe"
// *Set represents a Python set. In addition to satisfying the Object
// interface, Set pointers also have a number of methods defined - representing
// the PySet_XXX functions from the Python C API.
type Set struct {
AbstractObject
o C.PySetObject
}
type FrozenSet struct {
Set
}
// SetType is the Type object that represents the Set type.
var SetType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPySet_Type)))
// FrozenSetType is the Type object that represents the FrozenSet type.
var FrozenSetType = (*Type)(unsafe.Pointer(C.getBasePyType(C.GoPyFrozenSet_Type)))
func setCheck(obj Object) bool {
return C.setCheck(c(obj)) != 0
}
func frozenSetCheck(obj Object) bool {
return C.frozenSetCheck(c(obj)) != 0
}
func newSet(obj *C.PyObject) *Set {
return (*Set)(unsafe.Pointer(obj))
}
func newFrozenSet(obj *C.PyObject) *FrozenSet {
return (*FrozenSet)(unsafe.Pointer(obj))
}
// NewSet create a new Python set instance. The set contains the values from
// the passed Object if it is iterable (a TypeError is returned if "o" is not
// iterable). An empty set is returned if o is nil.
//
// Return value: New Reference.
func NewSet(o Object) (*Set, error) {
ret := C.PySet_New(c(o))
if ret == nil {
return nil, exception()
}
return newSet(ret), nil
}
// NewFrozenSet create a new Python frozenset instance. The set contains the
// values from the passed Object if it is iterable (a TypeError is returned if
// "o" is not iterable). An empty set is returned if o is nil.
//
// Return value: New Reference.
func NewFrozenSet(o Object) (*FrozenSet, error) {
ret := C.PyFrozenSet_New(c(o))
if ret == nil {
return nil, exception()
}
return newFrozenSet(ret), nil
}
func (f *FrozenSet) CheckExact() bool {
ret := C.frozenSetCheckE(c(f))
return ret != 0
}
// Size returns the number of elements in the set "s". This is equivalent to
// the Python "len(s)".
func (s *Set) Size() int64 {
ret := C.PySet_Size(c(s))
if ret < 0 {
panic(exception())
}
return int64(ret)
}
// Contains returns true if the set "s" contains the Object "key". "key" must
// be hashable, otherwise a TypeError is returned. This is equivalent to the
// Python "key in s".
func (s *Set) Contains(key Object) (bool, error) {
ret := C.PySet_Contains(c(s), c(key))
return int2BoolErr(ret)
}
// Add adds "key" to the set "s". "key" must be hashable, otherwise a TypeError
// is returned.
func (s *Set) Add(key Object) error {
ret := C.PySet_Add(c(s), c(key))
return int2Err(ret)
}
// Discard removes the specified "key" from the set "s". It returns true if the
// Object was found and removed, false if it was not found. "key" must be
// hashable, otherwise a TypeError is returned.
func (s *Set) Discard(key Object) (bool, error) {
ret := C.PySet_Discard(c(s), c(key))
return int2BoolErr(ret)
}
// Pop returns a new refereence to an arbitrary Object from the set, and removes
// it. If the set is empty a KeyError is returned.
//
// Return value: New Reference.
func (s *Set) Pop() (Object, error) {
ret := C.PySet_Pop(c(s))
return obj2ObjErr(ret)
}
// Clear empties the set "s".
func (s *Set) Clear() error {
ret := C.PySet_Clear(c(s))
return int2Err(ret)
}