Debian Codegraph @

comlib/misc.lisp

raw
1 ;;;-*- Mode:Lisp; Syntax:Common-Lisp; Package:CHAOS -*-
2 ;;;
3 ;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :CHAOS)
30 #|==============================================================================
31 System: Chaos
32 Module: comlib
33 File: misc.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 ;;;== DESCTIPTION
41 ;;;============================================================== A
42 ;;;collection of misc utility functions and macros.
43
44 ;;; ** NOTE ********************************************************************
45 ;;; Many of the codes in this file are cllected from articles posted to
46 ;;; some news groups (comp.lang.lisp, comp.lang.mcl, etc) or some mailing list
47 ;;; long ago. And some were from my personal libraries.
48 ;;; I have no infos codes other than the codes themselves.
49 ;;; Please let me know if you find codes which are copyrighted, or you know the
50 ;;; author. Email: [email protected].
51 ;;; ****************************************************************************
52
53 ;;; *******
54 ;;; KEYWORD_____________________________________________________________________
55 ;;; *******
56 (defvar *keyword-package* (find-package :keyword))
57
58 (defun make-keyword (name)
59 (declare (type (or symbol simple-string) name)
60 (values symbol))
61 (if (stringp name)
62 (intern name *keyword-package*)
63 ;; name must be a symbol
64 (intern (symbol-name name) *keyword-package*)))
65
66 ;;; *****************
67 ;;; OBJECT ALLOCATION____________________________________________________________
68 ;;; *****************
69
70 ;;; Allocating simple vector
71 ;
72 (defmacro alloc-svec (size)
73 `(the simple-vector (make-array ,size :initial-element nil)))
74
75 (defmacro alloc-svec-fixnum (size)
76 `(the simple-vector (make-array ,size :initial-element 0)))
77
78 ;;; GCL doesn't hadle simple-vector properly!
79
80 #-(or GCL EXCL)
81 (defmacro %svref (vector index)
82 `(svref ,vector (the fixnum ,index)))
83
84 #+EXCL
85 (defmacro %svref (vector index)
86 `(aref (the vector ,vector) (the fixnum ,index)))
87
88 #+GCL
89 (defmacro %svref (vector index)
90 `(aref (the vector ,vector) (the fixnum ,index)))
91
92 ;;; AT-TOP-LEVEL : -> Bool
93 ;;; determins whether we are at top level or not.
94 ;;; *NOTE* : the top-level must maintain the following two
95 ;;; variables properly:
96 ;;; *chaos-input-source* : buind file (or stream?) during input
97 ;;; from files.
98 ;;; *chaos-input-level* : buind fixnum indicating nested file
99 ;;; inputs levels.
100 ;;;
101 (defun at-top-level ()
102 (and (null *chaos-input-source*)
103 (<= *chaos-input-level* 0)))
104
105 ;;; ********************
106 ;;; ENVIRONMENT-VARIABLE________________________________________________________
107 ;;; ********************
108 ;;; ** TO DO for other platforms.
109
110 (defun get-environment-variable (x)
111 #+(or :CCL CMU) (declare (ignore x))
112 #+(or KCL GCL) (si:getenv x)
113 #+LUCID (environment-variable x)
114 #+CMU nil
115 #+:CCL nil
116 #+:Allegro (sys:getenv x)
117 #+:CLISP (ext:getenv x)
118 #+:SBCL (sb-ext:posix-getenv x)
119 )
120
121 ;;; *****
122 ;;; DEBUG_______________________________________________________________________
123 ;;; *****
124
125 (defvar *on-debug* nil)
126 (defvar *debug-level* 0)
127 (defmacro debug-msg ((msg &key (level 0)) &rest args)
128 `(if (and *on-debug* (<= ,level *debug-level*))
129 (funcall #'format *debug-io* ,msg ,@args)))
130 (defmacro debug-form (level &body body)
131 `(if (and *on-debug* (<= ,level *debug-level*))
132 (progn ,@body)))
133
134 (defun on-debug (&optional (level 1))
135 (setf *on-debug* t)
136 (setf *debug-level* level))
137
138 (defun off-debug ()
139 (setf *on-debug* nil)
140 (setf *debug-level* 0))
141
142 ;;; ***************
143 ;;; OBJECT ORDERING_____________________________________________________________
144 ;;; ***************
145 ;;; ordering some Common Lisp object
146 ;;; symbol < cons < number < character < string < sequence
147 ;;; integer < symbol < cons < othernumber
148
149 (defun ob< (x y)
150 (declare (type t x y))
151 (eq :lt (ob-compare x y)))
152
153 (defun ob-compare (x y)
154 (declare (type t x y))
155 (typecase x
156 (integer (typecase y
157 (integer (if (< (the integer x) (the integer y))
158 :lt
159 (if (< (the integer y) (the integer x))
160 :gt
161 :eq)))
162 (otherwise :lt)))
163 (symbol (typecase y
164 (symbol (if (eq x y)
165 :eq
166 (if (string-lessp (string (the symbol x))
167 (string (the symbol y)))
168 :lt
169 :gt)))
170 (integer :gt)
171 (otherwise :lt)))
172 (cons (typecase y
173 (cons (let ((comp-car (ob-compare (car x ) (car y))))
174 (if (eq :eq comp-car)
175 (ob-compare (cdr x) (cdr y))
176 comp-car)))
177 ((or symbol integer) :gt)
178 (otherwise :lt)))
179 (number (typecase y
180 (number (if (< x y)
181 :lt
182 (if (< y x)
183 :gt
184 :eq)))
185 ((or symbol integer cons) :gt)
186 (otherwise :lt)))
187 (character (typecase y
188 (character (if (char< (the character x)
189 (the character y))
190 :lt
191 (if (char< (the character y)
192 (the character x))
193 :gt
194 :eq)))
195 ((or number cons symbol) :gt)
196 (otherwise :lt)))
197 (string (typecase y
198 (string (if (string-lessp x y)
199 :lt
200 (if (string-lessp y x)
201 :gt
202 :eq)))
203 ((or character number cons symbol) :gt)
204 (otherwise :lt)))
205 (sequence (typecase y
206 (sequence (let ((lenx (length x))
207 (leny (length y)))
208 (declare (type fixnum lenx leny))
209 (dotimes (i (min lenx leny) (ob-compare lenx leny))
210 (declare (type fixnum i))
211 (let ((xi (elt x i))
212 (yi (elt y i)))
213 (let ((cmp (ob-compare xi yi)))
214 (unless (eq :eq cmp)
215 (return cmp)))))
216 :eq))
217 (otherwise :gt)))
218 (otherwise :lt)
219 ;; (structure :lt)
220 ;;
221 ;; how about structure ...
222 ;; (otherwise (break "not yet type ~s" (type-of x)))
223 ))
224
225 ;;; *********
226 ;;; TOPO-SORT___________________________________________________________________
227 ;;; *********
228 ;;; will only apply to sequences of distinct items
229 ;;; very simple method based on selection sort
230 ;;; -- select minimal element of those remaining, swap with next and continue
231 ;;; this is specialized to lists.
232 ;;;
233 (defun topo-sort (lst pred)
234 (declare (type list lst)
235 (type function pred))
236 (let ((res lst)) ; save original list as final value
237 ;; run through the positions of lst successively filling them in
238 (loop
239 (when (null lst) (return))
240 ;; pos is location of val which is current minimal value
241 (let ((pos lst) (val (car lst)) (rest (cdr lst)))
242 ;; scan through remainder of list rest updating pos and val
243 (loop ; -- select minimal
244 (when (null rest) (return))
245 (let ((valr (car rest)))
246 (when (funcall pred valr val)
247 (setq pos rest val valr))) ; have found new minimal value
248 (setq rest (cdr rest))) ; loop -- select minimal
249 ;; swap values at front of lst and at pos
250 (rplaca pos (car lst))
251 (rplaca lst val))
252 (setq lst (cdr lst)))
253 res))
254
255 ;;; *******
256 ;;; ADDRESS______________________________________________________________________
257 ;;; *******
258
259 ;;; address of objects.
260 ;;; The intention is to only use this for printing.
261 ;;;
262 #+GCL
263 (Clines "static object addr_of(x) object x; {return(make_fixnum((int)x));}")
264 ;;(defCfun "object addr_of(x) object x;" 0
265 ;;" Creturn(make_fixnum((int)x));"
266 ;;)
267 #+GCL
268 (defentry addr-of (object) (object addr_of))
269
270 (defconstant .32bit. #xffffffff)
271
272 #-GCL
273 (declaim (inline addr-of))
274
275 #+LUCID
276 (defun addr-of (x) (logand .32bit. (sys:%pointer x)))
277
278 #+CMU
279 (defun addr-of (x)
280 (logand .32.bit. (kernel:get-lisp-obj-address x)))
281
282 #+Excl
283 (defun addr-of (x) (logand .32bit. (excl::pointer-to-positive-fixnum x)))
284
285 #+CCL
286 (defun addr-of (x) (logand .32bit. (ccl::%address-of x)))
287
288 #+CLISP
289 (defun addr-of (x) (logand .32bit. (sys::address-of x)))
290
291 #+SBCL
292 (defun addr-of (x) (logand .32bit. (sb-kernel:get-lisp-obj-address x)))
293
294 (defun print-addr (x)
295 (format t "0x~8,'0x" (addr-of x)))
296
297 ;;; ************
298 ;;; QUERY-INPUT_________________________________________________________________
299 ;;; ************
300 (defun query-input (&optional (default #\y) (timeout 20)
301 format-string &rest args)
302 (clear-input *query-io*)
303 (when format-string
304 (fresh-line *query-io*)
305 (apply #'format *query-io* format-string args)
306 ;; FINISH-OUTPUT needed for CMU and other places which don't handle
307 ;; output streams nicely. This prevents it from continuing and
308 ;; reading the query until the prompt has been printed.
309 (finish-output *query-io*))
310 (let ((read-char (read-char-wait timeout *query-io*)))
311 (cond ((null read-char) (return-from query-input default))
312 (t (unread-char read-char *query-io*)
313 (read *query-io*)))))
314
315 ;;; *************
316 ;;; Y-OR-N-P-WAIT________________________________________________________________
317 ;;; *************
318 ;;; y-or-n-p-wait is like y-or-n-p, but will timeout
319 ;;; after a specified number of seconds
320 (defun internal-real-time-in-seconds ()
321 (/ (get-internal-real-time)
322 internal-time-units-per-second))
323
324 (defun read-char-wait (&optional (timeout 20) input-stream &aux char)
325 (do ((start (internal-real-time-in-seconds)))
326 ((or (setq char (read-char-no-hang input-stream nil)) ;(listen *query-io*)
327 (< (+ start timeout) (internal-real-time-in-seconds)))
328 char)))
329
330 (defvar *use-timeouts* t
331 "If T, timeouts in Y-OR-N-P-WAIT are enabled. Otherwise it behaves
332 like Y-OR-N-P. This is provided for users whose lisps don't handle
333 read-char-no-hang properly.")
334
335 (defvar *clear-input-before-query* t
336 "If T, y-or-n-p-wait will clear the input before printing the prompt
337 and asking the user for input.")
338
339 ;;; y-or-n-p-wait
340 ;;; Y-OR-N-P-WAIT prints the message, if any, and reads characters from
341 ;;; *QUERY-IO* until the user enters y, Y or space as an affirmative, or either
342 ;;; n or N as a negative answer, or the timeout occurs. It asks again if
343 ;;; you enter any other characters.
344
345 (defun y-or-n-p-wait (&optional (default #\y) (timeout 20)
346 format-string &rest args)
347 (when *clear-input-before-query* (clear-input *query-io*))
348 (when format-string
349 (fresh-line *query-io*)
350 (apply #'format *query-io* format-string args)
351 ;; FINISH-OUTPUT needed for CMU and other places which don't handle
352 ;; output streams nicely. This prevents it from continuing and
353 ;; reading the query until the prompt has been printed.
354 (finish-output *query-io*))
355 (loop
356 (let* ((read-char (if *use-timeouts*
357 (read-char-wait timeout *query-io*)
358 (read-char *query-io*)))
359 (char (or read-char default)))
360 (declare (type character char))
361 ;; We need to ignore #\newline because otherwise the bugs in
362 ;; clear-input will cause y-or-n-p-wait to print the "Type ..."
363 ;; message every time... *sigh*
364 ;; Anyway, we might want to use this to ignore whitespace once
365 ;; clear-input is fixed.
366 (unless (find char '(#\tab #\newline #\return))
367 (when (null read-char)
368 (format *query-io* "~@[~A~]" default)
369 (finish-output *query-io*))
370 (cond ((null char) (return t))
371 ((find char '(#\y #\Y #\space) :test #'char=) (return t))
372 ((find char '(#\n #\N) :test #'char=) (return nil))
373 (t
374 (when *clear-input-before-query* (clear-input *query-io*))
375 (format *query-io* "~%Type \"y\" for yes or \"n\" for no. ")
376 (when format-string
377 (fresh-line *query-io*)
378 (apply #'format *query-io* format-string args))
379 (finish-output *query-io*)))))))
380
381 ;;; ********
382 ;;; MULTISET____________________________________________________________________
383 ;;; ********
384
385 ;;; A multiset is a collection of elements "{e1, ..., en}" of some type, where
386 ;;; the elements need not be distinct. The operations "multiset.equal" is used
387 ;;; to determine if two elements are the same. Two elements are said to be
388 ;;; `distinct' in a multiset if they are not "multiset.equal" to one another.
389 ;;;
390 ;;; IMPLEMENTATION:
391 ;;; an element is represented as a pair of the form `(object . cout)',
392 ;;; where, `object' is the element and `count' is the number of times it
393 ;;; occurs in the multiset (if it occurs at least once in the multiset).
394 ;;; a multiset itself is represented as a list of this pairs.
395
396 (defstruct (multiset (:conc-name "MULTISET-")
397 (:constructor multiset-create (equal-fun elements))
398 (:copier nil))
399 (equal-fun #'eq :type function) ; predicate which determines the equality
400 ; of the objects.
401 (elements nil :type list)) ; list of pair (object . count).
402
403 ;;; MULTISET-NEW
404 ;;; creates the new empty multiset-
405 ;;;
406 (defmacro multiset-new (&optional (equal-fun #'eq))
407 `(multiset-create ,equal-fun nil))
408
409 ;;; MULTISET-IS-EMPTY m
410 ;;;
411 (defmacro multiset-is-empty (m)
412 `(null (multiset-elements ,m)))
413
414 ;;; MULTISET-INSERT ms e
415 ;;; insert e in ms.
416 (defmacro multiset-insert (ms e)
417 (once-only (ms)
418 `(let* ((elems (multiset-elements ,ms))
419 (pair (assoc ,e elems :test (multiset-equal-fun ,ms))))
420 (if pair
421 (incf (the fixnum (cdr pair)))
422 (setf (multiset-elements ,ms)
423 (push (cons e 1) elems))))))
424
425 ;;; LIST-TO-MULTISET list
426 ;;; returns a new multiset consisting of the elements in list.
427 ;;;
428 (defmacro list-to-multiset (list &optional (equal-fun #'eq))
429 ` (let ((ms (multiset-new ,equal-fun)))
430 (declare (type multiset ms))
431 (dolist (e ,list)
432 (multiset-insert ms e))
433 ms))
434
435 ;;; MULTISET-TO-SET ms
436 ;;; returns a set contains element in ms.
437 (defmacro multiset-to-set (ms)
438 `(mapcar #'car (multiset-elements ,ms)))
439
440 ;;; MULTISET-DELETE ms e
441 ;;; removes one occurrence of e in ms.
442 ;;;
443 (defmacro multiset-delete (ms e)
444 (once-only (ms)
445 `(let* ((elems (multiset-elements ,ms))
446 (pair (assoc ,e elems :test (multiset-equal-fun ,ms))))
447 (when pair
448 (when (zerop (decf (the fixnum (cdr pair))))
449 (setf (multiset-elements ,ms)
450 (delete e elems :test (multiset-equal-fun ,ms) :key #'car)))))))
451
452 ;;; MULTISET-MERGE m1 m2
453 ;;; inserts each elements of m2 into m1. leaves m2 unchanged.
454 ;;; equality is determined with respect to m1.
455 ;;;
456 (defmacro multiset-merge (m1 m2)
457 (once-only (m1)
458 `(let ((m1-elems (multiset-elements ,m1))
459 (equal-fun (multiset-equal-fun ,m1)))
460 (dolist (e2 (multiset-elements ,m2))
461 (let ((pair (assoc (car e2) m1-elems :test equal-fun)))
462 (if pair
463 (incf (the fixnum (cdr m1-elems)) (the fixnum (cdr pair)))
464 (push e2 m1-elems)))))))
465
466 ;;; MULTISET-INTERSECTION m1 m2
467 ;;; returns a new multiset with all elements that occur in both m1 and m2,
468 ;;; with the number of occurences being the smaller of the two.
469 ;;; leaves m1 and m2 unchanged.
470 ;;;
471 (defmacro multiset-intersectin (m1 m2)
472 (once-only (m1)
473 `(let ((m1-elems (multiset-elements ,m1))
474 (equal-fun (multiset-equal-fun ,m1))
475 (new-elems nil))
476 (dolist (e2 (multiset-elements ,m2))
477 (let ((pair (assoc (car e2) m1-elems :test equal-fun)))
478 (when pair
479 (push (cons (car pair) (min (cdr pair) (cdr e2)))
480 new-elems))))
481 (multiset-create equal-fun new-elems))))
482
483 ;;; MULTISET-DIFF m1 m2
484 ;;; returns the new multiset formed by removing from m1 each elements that
485 ;;; occurs in m2, the number of times it occurs. Thus, the relationship
486 ;;; m1 + m2 == (m1 - m2) + (m1 ^ m2) + (m2 - m1) will hold.
487 ;;;
488 (defmacro multiset-diff (m1 m2)
489 (once-only (m1)
490 `(let ((m1-elems (multiset-elements ,m1))
491 (equal-fun (multiset-equal-fun ,m1))
492 (new-elems nil))
493 (dolist (e2 (multiset-elements ,m2))
494 (let ((pair (assoc (car e2) m1-elems :test equal-fun)))
495 (if pair
496 (let ((count (- (cdr pair) (cdr e2))))
497 (when (< 0 count)
498 (push (cons (car pair) count) new-elems)))
499 (push (cons (car par) (cdr pair)) new-elems))))
500 (multiset-create equal-fun new-elems))))
501
502 ;;; MULTISET-COUNT m e
503 ;;; returns the nubmer of occurences of e in m.
504 ;;;
505 (defmacro multiset-count (m e)
506 (once-only (m)
507 `(let ((pair (assoc ,e (multiset-elements ,m) :test (multiset-equal-fun ,m))))
508 (if pair
509 (cdr pair)
510 0))))
511
512 ;;; ****
513 ;;; TIME________________________________________________________________________
514 ;;; ****
515
516 (defun get-time-string (&optional universal-time)
517 (unless universal-time (setf universal-time (get-universal-time)))
518 (multiple-value-bind (secs min hour date month year dow)
519 (decode-universal-time universal-time 0) ; GMT time
520 (declare (type fixnum dow month))
521 (format nil "~d ~a ~d ~a ~d:~2,'0d:~2,'0d GMT"
522 year
523 (%svref '#(0 "Jan" "Feb" "Mar" "Apr" "May"
524 "Jun" "Jul" "Aug" "Sep" "Oct"
525 "Nov" "Dec")
526 month)
527 date
528 (%svref '#("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun") dow)
529 hour min secs)
530 ))
531
532 ;;; elapsed-time-in-seconds
533 ;;;
534 ;;; Returns the time in seconds that has elapsed between Base and Now.
535 ;;; Just subtracts Base from Now to get elapsed time in internal time units,
536 ;;; then divides by the number of internal units per second to get seconds.
537
538 (defun elapsed-time-in-seconds (base now)
539 (declare (type integer base now))
540 (coerce (/ (- now base) internal-time-units-per-second) 'float))
541
542 (defun time-in-seconds (sum)
543 (declare (type integer sum))
544 (coerce (/ sum internal-time-units-per-second) 'float))
545
546 ;;; ****
547 ;;; MISC________________________________________________________________________
548 ;;; ****
549
550 (defmacro every2len (fn l1 l2)
551 (let* ((lmbd (cadr fn))
552 (args (cadr lmbd))
553 (bdy (cddr lmbd)))
554 ` (let ((lst1 ,l1) (lst2 ,l2) ,@args)
555 (loop
556 (when (null lst1) (return (null lst2)))
557 (when (null lst2) (return (null lst1)))
558 (setq ,(car args) (car lst1))
559 (setq ,(cadr args) (car lst2))
560 (unless (progn ,@bdy) (return nil))
561 (setq lst1 (cdr lst1))
562 (setq lst2 (cdr lst2))
563 )
564 )))
565
566 (defun list2array (list)
567 (declare (type list list)
568 #-GCL (values simple-vector)
569 )
570 #-GCL
571 (make-array (length list) :initial-contents list)
572 #+GCL
573 (let ((len (length list)))
574 (let ((arr (si:make-vector t len nil nil nil 0 nil)) (i 0))
575 (declare (fixnum i))
576 (dolist (e list) (si:aset arr i e) (setq i (1+ i)))
577 arr)))
578
579 (defun make-list-1-n (n)
580 (declare (type fixnum n)
581 (values list))
582 (let ((result nil))
583 (dotimes-fixnum (x n)
584 (push (+ x 1) result))
585 (reverse result)))
586
587 (defun make-list-1-n-0 (n)
588 (declare (type fixnum n)
589 (values list))
590 (let ((result nil))
591 (dotimes-fixnum (x n)
592 (push (+ x 1) result))
593 (push 0 result)
594 (reverse result)))
595
596 ;;;
597 ;;; REMOVEABLE ASSOCIATION TABLE
598 ;;;
599 (defmacro find-in-assoc-table (table key &optional (test '#'equal))
600 `(cdr (assoc ,key ,table :test ,test)))
601
602 (defmacro get-entry-in-assoc-table (table key &optional (test '#'equal))
603 `(assoc ,key ,table :test ,test))
604
605 (defmacro delete-entry-from-assoc-table (table key &optional (test '#'equal))
606 ` (let ((entry (assoc ,key ,table :test ,test)))
607 (when entry
608 (setq ,table (delete entry ,table :test #'eq)))))
609
610 (defmacro delete-object-from-assoc-table (table object &optional (test '#'eq))
611 ` (let ((entry (rassoc ,object ,table :test ,test)))
612 (when entry
613 (setq ,table (delete entry ,table :test #'eq)))))
614
615 (defmacro add-to-assoc-table (table key value &optional (test '#'equal))
616 (once-only (table key value)
617 ` (let ((entry (get-entry-in-assoc-table ,table ,key ,test)))
618 (if entry
619 (setf (cdr entry) ,value)
620 (prog1
621 ,value
622 (push (cons ,key ,value) ,table))))))
623
624 (defmacro object-is-in-assoc-table? (table object &optional (test '#'eq))
625 `(rassoc ,object ,table :test ,test))
626
627 ;;; *******************
628 ;;; FIXNUM COMPUTATIONS
629 ;;; *******************
630
631 (defmacro test-and (a b)
632 `(not (zerop (logand ,a ,b))))
633
634 (defmacro make-and (*a *b)
635 `(logand ,*a ,*b))
636
637 (defmacro make-or (*a *b)
638 `(logior ,*a ,*b))
639
640 (defmacro make-xor (*a *b)
641 `(logxor ,*a ,*b))
642
643 (defmacro expt2 (x)
644 `(expt 2 (the fixnum ,x)))
645
646 ;;; EOF