summaryrefslogtreecommitdiff
path: root/modules/language/python/set.scm
blob: 23fb552a9488464ed39da58b561835a48038484d (about) (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
(define-module (language python set)
  #:use-module (oop pf-objects)
  #:use-module (oop goops)
  #:use-module (language python exceptions)
  #:use-module (language python dict)
  #:use-module (language python for)
  #:use-module (language python try)
  #:use-module (language python list)
  #:use-module (language python yield)
  #:use-module (language python persist)
  #:use-module (language python bool)
  #:export (py-set frozenset weak-set))

(define-class <set> () dict)
(name-object <set>)

(cpit <set>
      (o (lambda (o a)
	   (slot-set! o 'dict
		      (let ((h (make-py-hashtable)))
			(let lp ((a a))
			  (if (pair? a)
			      (begin
				(py-hash-set! h (caar a) (cdar a))
				(lp (cdr a))))))))
	 (list
	  (hash-fold (lambda (k v s) (cons (cons k v) s))
		     '()
		     (slot-ref o 'dict)))))


(define miss (list 'miss))

(define-method (< (o1 <set>) ( o2 <set>))
  (and (not (equal? o1 o2))
       (for ((k : o1)) ()
            (if (in k o2)
                (values)
                (break #f))
            #:final #t)))

(define-method (> (o1 <set>) ( o2 <set>))
  (and (not (equal? o1 o2))
       (for ((k : o2)) ()
            (if (in k o1)
                (values)
                (break #f))
            #:final #t)))

(define-method (<= (o1 <set>) ( o2 <set>))
  (for ((k : o1)) ()
       (if (in k o2)
           (values)
           (break #f))
       #:final #t))

(define-method (>= (o1 <set>) ( o2 <set>))
  (for ((k : o2)) ()
       (if (in k o1)
           (values)
           (break #f))
       #:final #t))

(define-method (in k (o <set>))
  (in k (slot-ref o 'dict)))

(define-syntax-rule (mk set make-py-hashtable)
(define-python-class set (<set>)
  (define __init__
    (case-lambda
      ((self)       
       (slot-set! self 'dict (make-py-hashtable)))
      ((self x)
       (let ((d (make-py-hashtable)))
         (slot-set! self 'dict d)
         (if (eq? x '())
             (values)
	     (for ((y : x)) ()
	       (pylist-set! d y #t)))))))

  (define __bool__
    (lambda (self)
      (bool (slot-ref self 'dict))))
      
  (define pop
    (lambda (self)
      (call-with-values (lambda () (pylist-pop! (slot-ref self 'dict)))
        (lambda (k v) k))))
  
  (define  add
    (lambda (self k)
      (pylist-set! (slot-ref self 'dict) k #t)))

  (define copy
    (lambda (self)
      (let ((dict (py-copy (slot-ref self 'dict))))
        (set dict))))

  (define difference
    (lambda (self . l)
      (let* ((d (slot-ref self 'dict))
             (r (py-copy d)))
        (let lp ((l l))
          (if (pair? l)
              (begin
                (for ((x : (car l))) ()
                     (when (in x d)
                       (pylist-delete! r x)))
                (lp (cdr l)))))
        (set r))))

  (define difference_update
    (lambda (self . l)
      (let* ((r (slot-ref self 'dict)))
        (let lp ((l l))
          (if (pair? l)
              (begin
                (for ((x : (car l))) ()
                     (when (in x r)
                       (pylist-delete! r x)))
                (lp (cdr l)))))
        (values))))
  
  (define discard
    (lambda (self . l)
      (let* ((r (slot-ref self 'dict)))
        (let lp ((l l))
          (if (pair? l)
              (begin
                (pylist-delete! r (car l))
                (lp (cdr l))))))))
  
  (define intersection
    (lambda (self . l)
      (let* ((d (slot-ref self 'dict))
             (r (py-copy d)))
        (let lp ((l l))
          (if (pair? l)
              (let ((y (car l)))
                (for ((k v : r)) ((dels '()))
                     (if (not (__contains__ y k))
                         (cons k dels)
                         dels)
                  #:final
                  (let lp ((dels dels))
                    (if (pair? dels)
                        (begin
                          (pylist-delete! r (car dels))
                          (lp (cdr dels))))))
                (lp (cdr l)))))
        (set r))))

  (define intersection_update
    (lambda (self . l)
      (let* ((r (slot-ref self 'dict)))
        (let lp ((l l))
          (if (pair? l)
              (let ((y (car l)))
                (for ((k v : r)) ((dels '()))
                  (if (not (__contains__ y k))
                      (cons k dels)
                      dels)
                  #:final
                  (let lp ((dels dels))
                    (if (pair? dels)
                        (begin
                          (pylist-delete! r (car dels))
                          (lp (cdr dels))))))
                (lp (cdr l))))))))

  (define isdisjoint
    (lambda (self x)
      (let* ((r  (slot-ref self 'dict))
             (n1 (len r))
             (n2 (len x)))
        (if (< n2 n1)
            (let ((xx x))
              (set! x r)
              (set! r xx)))
        (for ((k v : r)) ()
             (if (in k x)
                 (break #f))
             #:final
             #t))))
  
  (define issubset
    (lambda (self x)
      (let* ((r  (slot-ref self 'dict)))
        (for ((k v : r)) ()
          (if (not (__contains__ x k))
              (break #f))
          #:final
          #t))))

  (define issuperset
    (lambda (self x)
      (let* ((r (slot-ref self 'dict)))
        (for ((x v : r)) ()
          (if (not (in x r))
              (break #f))
          #:final
          #t))))

  (define remove
    (lambda (self x)
      (let* ((r (slot-ref self 'dict)))
        (if (not (in x r))
            (raise KeyError "missing key in set at remove")
            (pylist-delete! r x)))))

  (define symmetric_difference
    (lambda (self x)
      (union (difference self x) (difference x self))))
  
  (define symmetric_difference_update
    (lambda (self x)
      (difference_update self x)
      (update self (difference x self))))

  (define union
    (lambda (self . l)
      (let* ((d (slot-ref self 'dict))
             (r (py-copy d)))
        (let lp ((l l))
          (if (pair? l)
              (begin
                (for ((k : (car l))) ()
                     (pylist-set! r k #t))
                (lp (cdr l)))
              (set r))))))
  
  (define update
    (lambda (self . l)
      (let* ((r (slot-ref self 'dict)))             
        (let lp ((l l))
          (if (pair? l)
              (begin
                (for ((k v : (car l))) ()
                     (pylist-set! r k #t))
                (lp (cdr l)))
              (values))))))
  
  (define __repr__
    (lambda (self)
      (let* ((r (py-keys (slot-ref self 'dict)))
             (n (len r))
             (l (to-list r)))
        (cond
         ((= n 0)
          (format #f "set([])"))
         (else
          (format #f "set([~a~{, ~a~}])" (car l) (cdr l)))))))

  (define __contains__
    (lambda (self x)
      (let* ((d (slot-ref self 'dict))
             (t (slot-ref d    't)))
        (not (eq? miss (py-hash-ref t x miss))))))

  (define __and__
    (lambda (self op)
      (intersection self op)))

  (define __or__
    (lambda (self op)
      (union self op)))

  (define __sub__
    (lambda (self op)
      (difference self op)))

  (define __xor__
    (lambda (self op)
      (symmetric_difference self op)))
  
  (define __eq__
    (lambda (self x)
      (and
       (is-a? x <p>)
       (eq? (ref self '__class__ 1) (ref x '__class__ 2))
       (equal? (ref self 'd 1)      (ref x 'd 2)))))
                 
  (define __iter__
    (lambda (self)
      ((make-generator ()
	  (lambda (yield)
	    (for ((k v : (slot-ref self 'dict))) ()
		 (yield k)
		 (values)))))))))

(mk set      make-py-hashtable)
(mk weak-set make-py-weak-key-hashtable)

(define py-set set)
(define-python-class frozenset (set))