/
utils.lisp
353 lines (302 loc) · 10.3 KB
/
utils.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
(in-package :com.search)
(defmacro with-gensyms (syms &body body)
"Create gensyms, useful for creating macros." ; LMH
`(let ,(mapcar #'(lambda (s)
`(,s (gensym)))
syms)
,@body))
(defun mkstr (&rest args)
"Make a string out of the printed representations of the arguments." ; LMH
(with-output-to-string (s)
(dolist (a args) (princ a s))))
(defun symb (&rest args)
"Make a symbol out of the printed representations of the arguments." ; LMH
(values (intern (apply #'mkstr args))))
(defun make-logic-symbol (string)
"Convert string to symbol, preserving case, except for AND/OR/NOT/FORALL/EXISTS."
(cond ((find string '(and or not forall exists) :test #'string-equal))
((lower-case-p (char string 0))
(symb (string-upcase string)))
((equal string "Nil") '|Nil|)
(t (intern (string-upcase string)))))
(defun combinations (bag)
(if (null bag)
'(())
(mapcan #'(lambda (e)
(mapcar #'(lambda (c) (cons e c))
(combinations (cdr bag))))
(car bag))))
(defun permutations (bag)
(mapcan #'(lambda (y)
(mapcar #'(lambda (x) (list x y))
(remove y bag :test #'equal)))
bag))
(defmacro with-collect ((&rest collectors) &body forms)
"Evaluate forms, collecting objects into lists.
Within the FORMS, you can use local macros listed among collectors,
they are returned as multiple values.
E.g., (with-collect (c1 c2) (dotimes (i 10) (if (oddp i) (c1 i) (c2 i))))
==> (1 3 5 7 9); (0 2 4 6 8) [2 values]
In CLISP, push/nreverse is about 1.25 times as fast as pushing into the
tail, so this macro uses push/nreverse on CLISP and push into the tail
on other lisps (which is 1.5-2 times as fast as push/nreverse there)."
#+clisp
(let ((ret (mapcar (lambda (cc) (gensym (format nil "~s-RET-" cc)))
collectors)))
`(let (,@ret)
(declare (list ,@ret))
(macrolet ,(mapcar (lambda (co re) `(,co (form) `(push ,form ,',re)))
collectors ret)
,@forms
(values ,@(mapcar (lambda (re) `(sys::list-nreverse ,re)) ret)))))
#-clisp
(let ((ret (mapcar (lambda (cc) (gensym (format nil "~s-RET-" cc)))
collectors))
(tail (mapcar (lambda (cc) (gensym (format nil "~s-TAIL-" cc)))
collectors))
(tmp (mapcar (lambda (cc) (gensym (format nil "~s-TMP-" cc)))
collectors)))
`(let (,@ret ,@tail)
(declare (list ,@ret ,@tail))
(macrolet ,(mapcar (lambda (co re ta tm)
`(,co (form)
`(let ((,',tm (list ,form)))
(if ,',re (setf (cdr ,',ta) (setf ,',ta ,',tm))
(setf ,',re (setf ,',ta ,',tm))))))
collectors ret tail tmp)
,@forms
(values ,@ret)))))
(defmacro awhen (test-form &body body)
`(let ((it ,test-form))
(declare (ignorable it))
(when it
,@body)))
(defun mapa-b (fn a b &optional (step 1))
; (declare (function fn) (fixnum a b step)) ; LMH
"Apply the fn to the list of numbers a...b, stepping with step." ; LMH
(do ((i a (+ i step))
(result nil))
((> i b) (nreverse result))
; (declare (fixnum i)) ; LMH
(push (funcall fn i) result)))
(defun map0-n (fn n)
(declare (function fn) (fixnum n)) ; LMH
"Apply the fn to the list of numbers 0...n." ; LMH
(mapa-b fn 0 n))
(defun map1-n (fn n)
; (declare (function fn) (fixnum n)) ; LMH
"Apply the fn to the list of numbers 1...n." ; LMH
(mapa-b fn 1 n))
(defun firstn (lst n)
(if (or (null lst) (<= n 0))
nil
(cons (car lst)
(firstn (cdr lst) (- n 1)))))
(defun s+ (&rest args)
(declare (optimize (speed 3) (safety 0) (compilation-speed 0) (debug 0)))
(apply #'concatenate 'string args))
(defun mappend (fn &rest lsts)
(declare (function fn)) ; LMH
"Nondestructive form of mapcan." ; LMH
(apply #'append (apply #'mapcar fn lsts)))
(defun map-adj (fn this adj rest list &optional (acc '()))
(cond ((null list) (nreverse acc))
((null (funcall rest (funcall rest list)))
(nreverse (cons (funcall fn (funcall this list) (funcall adj list))
acc)))
(t (map-adj fn this adj rest (funcall rest list)
(cons (funcall fn (funcall this list) (funcall adj list)) acc)))))
(defmacro when-bind ((var expr) &body body)
`(let ((,var ,expr))
(when ,var
,@body)))
(defmacro while (test &body body)
`(do ()
((not ,test))
,@body))
(defmacro aif (test-form then-form &optional else-form)
"Anaphoric if: use `it' in then-form, else-form to
refer to result of the test-form." ; LMH
`(let ((it ,test-form))
(declare (ignorable it)) ; LMH
(if it ,then-form ,else-form)))
(defun hash-keys (ht)
(let ((acc nil))
(maphash #'(lambda (k v)
(declare (ignore v))
(push k acc))
ht)
(nreverse acc)))
(defun hash-vals (ht)
(let ((acc nil))
(maphash #'(lambda (k v)
(declare (ignore k))
(push v acc))
ht)
acc))
(defun hash-pairs (ht)
(let ((acc nil))
(maphash #'(lambda (k v)
(push (cons k v) acc))
ht)
acc))
(defun somehash (fn ht)
(maphash #'(lambda (k v)
(declare (ignore k))
(when (funcall fn v)
(return-from somehash v)))
ht)
nil)
(defun hash-nth (n ht)
(gethash (nth n (hash-keys ht)) ht))
(defun hash-first (ht)
(hash-nth 0 ht))
(defun hash-second (ht)
(hash-nth 1 ht))
(defun hash-last (ht)
(hash-nth (1- (hash-table-count ht)) ht))
(defun hash-table->alist (ht)
"Return the alist with the same data as the hash-table.
Actually, the first element is the test: '(eql (key0 . val0) (key1 . val1)).
The inverse is `alist->hash-table'."
(declare (hash-table ht))
(cons (hash-table-test ht)
(with-collect (co)
(with-hash-table-iterator (iter ht)
(loop (multiple-value-bind (re kk vv) (iter)
(unless re (return))
(co (cons kk vv))))))))
(defun alist->hash-table (alist &optional (value-fn #'identity))
"Return the new hash-table based on this alist.
The inverse is `hash-table->alist'."
(declare (list alist))
(let ((ht (make-hash-table :test (car alist))))
(dolist (co (cdr alist) ht)
(setf (gethash (car co) ht) (funcall value-fn (cdr co))))))
(defmethod print-object ((ht hash-table) (out stream))
(if *print-readably*
(format out "~s" (hash-table->alist ht))
(call-next-method)))
; Hash Tables
(defun nonempty-ht (ht)
(maphash #'(lambda (k v) (declare (ignore k v)) (return-from nonempty-ht t))
ht)
nil)
(defun ht-car (ht)
(maphash #'(lambda (k v) (declare (ignore k)) (return-from ht-car v))
ht))
(defun key-match (ht1 ht2)
(maphash #'(lambda (k v)
(declare (ignore v))
(when (gethash k ht2)
(return-from key-match k)))
ht1)
nil)
(defun write-hashtable-stream (stream ht)
(when ht
(maphash #'(lambda (key value)
(print (cons key value) stream))
ht)))
(defun write-hashtable (file ht)
(with-open-file (stream file :direction :output :if-exists :supersede)
(write-hashtable-stream stream ht)))
(defun read-hashtable-stream (stream)
(let* ((ht (make-hash-table :test #'equal)))
(loop for line = (read stream nil nil)
until (null line)
do
(setf (gethash (car line) ht) (cdr line)))
ht))
(defun read-hashtable (file)
(with-open-file (stream file :direction :input)
(read-hashtable-stream stream)))
(defmacro hash (&key (test nil) (rehash-size 10) (keyvals nil) (hash nil))
;--------------------------
; Return a new hash table.
;--------------------------
`(progn
(let ((h (make-hash-table ,@(when test (list :test test)) ,@(list :rehash-size rehash-size))))
(when ,keyvals
(hash-populate h ,keyvals))
(when ,hash
(do-hash (k v ,hash) (hash-put h k v)))
h)))
(defmacro hash-populate (hash kvlist)
;-----------------------------
; Insert KEY, VALUE elements
; from 2-element KVLIST
; Return Hash
; ----------------------------
`(progn
(dolist (kv ,kvlist)
(hash-put ,hash (first kv) (second kv)))
,hash))
(defmacro hash-put (hash key value)
;-----------------------------
; Insert KEY-VALUE into HASH.
; Return HASH.
;-----------------------------
`(progn
(setf (gethash ,key ,hash) ,value)
,hash))
(defmacro hash-get (hash key)
;-----------------------------
; Get KEY-VALUE into HASH.
; Return HASH.
;-----------------------------
`(progn
(gethash ,key ,hash)
))
(defmacro do-hash ((k v hash) &body body)
;-------------------------------------------------------
; Iterate over elements of HASH. On each iteration
; evaluate BODY with ELEM bound to the current element.
;-------------------------------------------------------
`(maphash
(lambda (,k ,v)
,@body)
,hash))
;; Returns a new hashset that is a union of the two
(defmacro hashset-union (hset1 hset2)
`(let ((h (hash :test (hash-table-test ,hset1) :hash ,hset1)))
(maphash #'(lambda (k v)
(hash-put h k v))
,hset2)
h))
;; Returns a new hashset that is a union of the two (destructive version)
(defmacro nhashset-union (hset1 hset2)
`(progn
(maphash #'(lambda (k v)
(hash-put ,hset1 k v))
,hset2)
,hset1))
;; Returns a new hashset that is the intersection
(defmacro hashset-intersection (hset1 hset2)
`(let* ((h (hash :test (hash-table-test ,hset1)))
(h1 (if (> (hash-table-size ,hset1) (hash-table-size ,hset2)) ,hset1 ,hset2))
(h2 (if (eq ,hset1 h1) ,hset2 ,hset1)))
(maphash #'(lambda (k v)
(if (hash-get h1 k)
(hash-put h k v)))
h2)
h))
(defmacro hashset-difference (hset1 hset2)
`(let ((h (hash :test (hash-table-test ,hset1))))
(maphash #'(lambda (k v)
(unless (hash-get ,hset2 k)
(hash-put h k v)))
,hset1)
h))
(defun hash-empty? (hash)
;-------------------------
; True iff HASH is empty.
;-------------------------
(null (hash-keys hash)))
(defun make-counter ()
(hash :test 'equal))
(defun incf-counter (h key)
(if (gethash key h)
(incf (gethash key h))
(setf (gethash key h) 1)))
(defun make-hash ()
(make-hash-table :test 'equal))