[2258] | 1 | ;;; assoc.lisp |
---|
| 2 | ;;; |
---|
| 3 | ;;; Copyright (C) 2003 Peter Graves |
---|
[4371] | 4 | ;;; $Id: assoc.lisp,v 1.4 2003-10-14 16:04:29 piso Exp $ |
---|
[2258] | 5 | ;;; |
---|
| 6 | ;;; This program is free software; you can redistribute it and/or |
---|
| 7 | ;;; modify it under the terms of the GNU General Public License |
---|
| 8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
| 9 | ;;; of the License, or (at your option) any later version. |
---|
| 10 | ;;; |
---|
| 11 | ;;; This program is distributed in the hope that it will be useful, |
---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | ;;; GNU General Public License for more details. |
---|
| 15 | ;;; |
---|
| 16 | ;;; You should have received a copy of the GNU General Public License |
---|
| 17 | ;;; along with this program; if not, write to the Free Software |
---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 19 | |
---|
[4371] | 20 | ;;; From CMUCL. |
---|
[2258] | 21 | |
---|
[4371] | 22 | (in-package "SYSTEM") |
---|
[2258] | 23 | |
---|
| 24 | (defmacro assoc-guts (test-guy) |
---|
| 25 | `(do ((alist alist (cdr alist))) |
---|
| 26 | ((endp alist)) |
---|
| 27 | (if (car alist) |
---|
| 28 | (if ,test-guy (return (car alist)))))) |
---|
| 29 | |
---|
| 30 | (defun assoc (item alist &key key test test-not) |
---|
| 31 | (cond (test |
---|
| 32 | (if key |
---|
| 33 | (assoc-guts (funcall test item (funcall key (caar alist)))) |
---|
| 34 | (assoc-guts (funcall test item (caar alist))))) |
---|
| 35 | (test-not |
---|
| 36 | (if key |
---|
| 37 | (assoc-guts (not (funcall test-not item |
---|
| 38 | (funcall key (caar alist))))) |
---|
| 39 | (assoc-guts (not (funcall test-not item (caar alist)))))) |
---|
| 40 | (t |
---|
| 41 | (if key |
---|
| 42 | (assoc-guts (eql item (funcall key (caar alist)))) |
---|
| 43 | (assoc-guts (eql item (caar alist))))))) |
---|
| 44 | |
---|
| 45 | (defun assoc-if (predicate alist &key key) |
---|
| 46 | (if key |
---|
| 47 | (assoc-guts (funcall predicate (funcall key (caar alist)))) |
---|
| 48 | (assoc-guts (funcall predicate (caar alist))))) |
---|
| 49 | |
---|
| 50 | (defun assoc-if-not (predicate alist &key key) |
---|
| 51 | (if key |
---|
| 52 | (assoc-guts (not (funcall predicate (funcall key (caar alist))))) |
---|
| 53 | (assoc-guts (not (funcall predicate (caar alist)))))) |
---|
| 54 | |
---|
| 55 | (defun rassoc (item alist &key key test test-not) |
---|
| 56 | (cond (test |
---|
| 57 | (if key |
---|
| 58 | (assoc-guts (funcall test item (funcall key (cdar alist)))) |
---|
| 59 | (assoc-guts (funcall test item (cdar alist))))) |
---|
| 60 | (test-not |
---|
| 61 | (if key |
---|
| 62 | (assoc-guts (not (funcall test-not item |
---|
| 63 | (funcall key (cdar alist))))) |
---|
| 64 | (assoc-guts (not (funcall test-not item (cdar alist)))))) |
---|
| 65 | (t |
---|
| 66 | (if key |
---|
| 67 | (assoc-guts (eql item (funcall key (cdar alist)))) |
---|
| 68 | (assoc-guts (eql item (cdar alist))))))) |
---|
| 69 | |
---|
| 70 | (defun rassoc-if (predicate alist &key key) |
---|
| 71 | (if key |
---|
| 72 | (assoc-guts (funcall predicate (funcall key (cdar alist)))) |
---|
| 73 | (assoc-guts (funcall predicate (cdar alist))))) |
---|
| 74 | |
---|
| 75 | (defun rassoc-if-not (predicate alist &key key) |
---|
| 76 | (if key |
---|
| 77 | (assoc-guts (not (funcall predicate (funcall key (cdar alist))))) |
---|
| 78 | (assoc-guts (not (funcall predicate (cdar alist)))))) |
---|
[2276] | 79 | |
---|
| 80 | (defun acons (key datum alist) |
---|
| 81 | (cons (cons key datum) alist)) |
---|
| 82 | |
---|
| 83 | (defun pairlis (keys data &optional (alist '())) |
---|
| 84 | (do ((x keys (cdr x)) |
---|
| 85 | (y data (cdr y))) |
---|
| 86 | ((and (endp x) (endp y)) alist) |
---|
| 87 | (if (or (endp x) (endp y)) |
---|
| 88 | (error "the lists of keys and data are of unequal length")) |
---|
| 89 | (setq alist (acons (car x) (car y) alist)))) |
---|
[2279] | 90 | |
---|
| 91 | (defun copy-alist (alist) |
---|
| 92 | (if (atom alist) |
---|
| 93 | alist |
---|
| 94 | (let ((result |
---|
| 95 | (cons (if (atom (car alist)) |
---|
| 96 | (car alist) |
---|
| 97 | (cons (caar alist) (cdar alist)) ) |
---|
| 98 | nil))) |
---|
| 99 | (do ((x (cdr alist) (cdr x)) |
---|
| 100 | (splice result |
---|
| 101 | (cdr (rplacd splice |
---|
| 102 | (cons |
---|
| 103 | (if (atom (car x)) |
---|
| 104 | (car x) |
---|
| 105 | (cons (caar x) (cdar x))) |
---|
| 106 | nil))))) |
---|
| 107 | ;; Non-null terminated alist done here. |
---|
| 108 | ((atom x) |
---|
| 109 | (unless (null x) |
---|
| 110 | (rplacd splice x)))) |
---|
| 111 | result))) |
---|