-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtcons-set.lisp
332 lines (308 loc) · 12.8 KB
/
tcons-set.lisp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;;;; functions to implement lists-based sets
;;;; This software is derived from the SBCL system.
;;;; See the README.SBCL file for more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package :stmx.util)
(enable-#?-syntax)
#|
;;;; functions for using lists as sets
(defun member (item list &key key (test nil testp) (test-not nil notp))
"Return the tail of LIST beginning with first element satisfying EQLity,
:TEST, or :TEST-NOT with the given ITEM."
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key)))
(test (and testp (%coerce-callable-to-fun test)))
(test-not (and notp (%coerce-callable-to-fun test-not))))
(cond (test
(if key
(%member-key-test item list key test)
(%member-test item list test)))
(test-not
(if key
(%member-key-test-not item list key test-not)
(%member-test-not item list test-not)))
(t
(if key
(%member-key item list key)
(%member item l))))))
(defun member-if (test list &key key)
"Return tail of LIST beginning with first element satisfying TEST."
(let ((test (%coerce-callable-to-fun test))
(key (and key (%coerce-callable-to-fun key))))
(if key
(%member-if-key test list key)
(%member-if test l))))
(defun member-if-not (test list &key key)
"Return tail of LIST beginning with first element not satisfying TEST."
(let ((test (%coerce-callable-to-fun test))
(key (and key (%coerce-callable-to-fun key))))
(if key
(%member-if-not-key test list key)
(%member-if-not test l))))
(defun tailp (object l)
"Return true if OBJECT is the same as some tail of LIST, otherwise
returns false. LIST must be a proper list or a dotted list."
(do ((list list (cdr l)))
((atom l) (eql list object))
(if (eql object l)
(return t))))
(defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
"Add ITEM to LIST unless it is already a member"
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key)))
(test (and testp (%coerce-callable-to-fun test)))
(test-not (and notp (%coerce-callable-to-fun test-not))))
(cond (test
(if key
(%adjoin-key-test item list key test)
(%adjoin-test item list test)))
(test-not
(if key
(%adjoin-key-test-not item list key test-not)
(%adjoin-test-not item list test-not)))
(t
(if key
(%adjoin-key item list key)
(%adjoin item l))))))
(defconstant +list-based-union-limit+ 80)
(defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
"Return the union of LIST1 and LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
;; We have two possibilities here: for shortish lists we pick up the
;; shorter one as the result, and add the other one to it. For long
;; lists we use a hash-table when possible.
(let ((n1 (length list1))
(n2 (length list2))
(key (and key (%coerce-callable-to-fun key)))
(test (if notp
(let ((test-not-fun (%coerce-callable-to-fun test-not)))
(lambda (x y) (not (funcall test-not-fun x y))))
(%coerce-callable-to-fun test))))
(multiple-value-bind (short long n-short)
(if (< n1 n2)
(values list1 list2 n1)
(values list2 list1 n2))
(if (or (< n-short +list-based-union-limit+)
(not (member test (list #'eq #'eql #'equal #'equalp))))
(let ((orig short))
(dolist (elt long)
(unless (member (apply-key key elt) orig :key key :test test)
(push elt short)))
short)
(let ((table (make-hash-table :test test :size (+ n1 n2)))
(union nil))
(dolist (elt long)
(setf (gethash (apply-key key elt) table) elt))
(dolist (elt short)
(setf (gethash (apply-key key elt) table) elt))
(maphash (lambda (k v)
(declare (ignore k))
(push v union))
table)
union)))))
;;; Destination and source are SETF-able and many-evaluable. Set the
;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
;;;
;;; FIXME: needs a more mnemonic name
(defmacro steve-splice (source destination)
`(let ((temp ,source))
(setf ,source (cdr ,source)
(cdr temp) ,destination
,destination temp)))
(defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
"Destructively return the union of LIST1 and LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
;; We have two possibilities here: for shortish lists we pick up the
;; shorter one as the result, and add the other one to it. For long
;; lists we use a hash-table when possible.
(let ((n1 (length list1))
(n2 (length list2))
(key (and key (%coerce-callable-to-fun key)))
(test (if notp
(let ((test-not-fun (%coerce-callable-to-fun test-not)))
(lambda (x y) (not (funcall test-not-fun x y))))
(%coerce-callable-to-fun test))))
(multiple-value-bind (short long n-short)
(if (< n1 n2)
(values list1 list2 n1)
(values list2 list1 n2))
(if (or (< n-short +list-based-union-limit+)
(not (member test (list #'eq #'eql #'equal #'equalp))))
(let ((orig short))
(do ((elt (car long) (car long)))
((endp long))
(if (not (member (apply-key key elt) orig :key key :test test))
(steve-splice long short)
(setf long (cdr long))))
short)
(let ((table (make-hash-table :test test :size (+ n1 n2))))
(dolist (elt long)
(setf (gethash (apply-key key elt) table) elt))
(dolist (elt short)
(setf (gethash (apply-key key elt) table) elt))
(let ((union long)
(head long))
(maphash (lambda (k v)
(declare (ignore k))
(if head
(setf (car head) v
head (cdr head))
(push v union)))
table)
union))))))
(defun intersection (list1 list2
&key key (test #'eql testp) (test-not nil notp))
"Return the intersection of LIST1 and LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key))))
(let ((res nil))
(dolist (elt list1)
(if (with-set-keys (member (apply-key key elt) list2))
(push elt res)))
res)))
(defun nintersection (list1 list2
&key key (test #'eql testp) (test-not nil notp))
"Destructively return the intersection of LIST1 and LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key))))
(let ((res nil)
(list1 list1))
(do () ((endp list1))
(if (with-set-keys (member (apply-key key (car list1)) list2))
(steve-splice list1 res)
(setq list1 (cdr list1))))
res)))
(defun set-difference (list1 list2
&key key (test #'eql testp) (test-not nil notp))
"Return the elements of LIST1 which are not in LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key))))
(if (null list2)
list1
(let ((res nil))
(dolist (elt list1)
(if (not (with-set-keys (member (apply-key key elt) list2)))
(push elt res)))
res))))
(defun nset-difference (list1 list2
&key key (test #'eql testp) (test-not nil notp))
"Destructively return the elements of LIST1 which are not in LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key))))
(let ((res nil)
(list1 list1))
(do () ((endp list1))
(if (not (with-set-keys (member (apply-key key (car list1)) list2)))
(steve-splice list1 res)
(setq list1 (cdr list1))))
res)))
(defun set-exclusive-or (list1 list2
&key key (test #'eql testp) (test-not #'eql notp))
"Return new list of elements appearing exactly once in LIST1 and LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((result nil)
(key (and key (%coerce-callable-to-fun key)))
(test (if testp (%coerce-callable-to-fun test) test))
(test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
(declare (type function test test-not))
(dolist (elt list1)
(unless (with-set-keys (member (apply-key key elt) list2))
(setq result (cons elt result))))
(let ((test (if testp
(lambda (x y) (funcall test y x))
test))
(test-not (if notp
(lambda (x y) (funcall test-not y x))
test-not)))
(dolist (elt list2)
(unless (with-set-keys (member (apply-key key elt) list1))
(setq result (cons elt result)))))
result))
(defun nset-exclusive-or (list1 list2
&key key (test #'eql testp) (test-not #'eql notp))
"Destructively return a list with elements which appear but once in LIST1
and LIST2."
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key)))
(test (if testp (%coerce-callable-to-fun test) test))
(test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
(declare (type function test test-not))
;; The outer loop examines LIST1 while the inner loop examines
;; LIST2. If an element is found in LIST2 "equal" to the element
;; in LIST1, both are spliced out. When the end of LIST1 is
;; reached, what is left of LIST2 is tacked onto what is left of
;; LIST1. The splicing operation ensures that the correct
;; operation is performed depending on whether splice is at the
;; top of the list or not.
(do ((list1 list1)
(list2 list2)
(x list1 (cdr x))
(splicex ())
(deleted-y ())
;; elements of LIST2, which are "equal" to some processed
;; earlier elements of LIST1
)
((endp x)
(if (null splicex)
(setq list1 list2)
(rplacd splicex list2))
list1)
(let ((key-val-x (apply-key key (car x)))
(found-duplicate nil))
;; Move all elements from LIST2, which are "equal" to (CAR X),
;; to DELETED-Y.
(do* ((y list2 next-y)
(next-y (cdr y) (cdr y))
(splicey ()))
((endp y))
(cond ((let ((key-val-y (apply-key key (car y))))
(if notp
(not (funcall test-not key-val-x key-val-y))
(funcall test key-val-x key-val-y)))
(if (null splicey)
(setq list2 (cdr y))
(rplacd splicey (cdr y)))
(setq deleted-y (rplacd y deleted-y))
(setq found-duplicate t))
(t (setq splicey y))))
(unless found-duplicate
(setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
(if found-duplicate
(if (null splicex)
(setq list1 (cdr x))
(rplacd splicex (cdr x)))
(setq splicex x))))))
(defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
"Return T if every element in LIST1 is also in LIST2."
(declare (inline member))
(when (and testp notp)
(error ":TEST and :TEST-NOT were both supplied."))
(let ((key (and key (%coerce-callable-to-fun key))))
(dolist (elt list1)
(unless (with-set-keys (member (apply-key key elt) list2))
(return-from subsetp nil)))
t))
|#
nil