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
|
;; Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
;; This file is part of GNU Common Lisp, herein referred to as GCL
;;
;; GCL is free software; you can redistribute it and/or modify it under
;; the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; GCL is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
;; License for more details.
;;
;; You should have received a copy of the GNU Library General Public License
;; along with GCL; see the file COPYING. If not, write to the Free Software
;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;;; listlib.lsp
;;;;
;;;; list manipulating routines
; Rewritten 11 Feb 1993 by William Schelter and Gordon Novak to use iteration
; rather than recursion, as needed for large data sets.
(in-package 'lisp)
(export '(union nunion intersection nintersection
set-difference nset-difference set-exclusive-or nset-exclusive-or
subsetp nth nth-value nthcdr first second third fourth fifth sixth seventh eighth ninth tenth))
(in-package 'system)
(eval-when (compile)
(proclaim '(optimize (safety 0) (space 3)))
)
(defun key-list (key test test-not &aux (tem nil))
(when key (push :key tem) (push key tem))
(when test (push :test tem) (push test tem))
(when test-not (push :test-not tem) (push test-not tem))
(nreverse tem))
;(defun union (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (cond ((null list1) list2)
; ((apply #'member1 (car list1) list2 rest)
; (apply #'union (cdr list1) list2 rest))
; (t
; (cons (car list1)
; (apply #'union (cdr list1) list2 rest)))))
(defun union (list1 list2 &key test test-not key &aux first last)
(do ((x list1 (cdr x)))
((null x) (if last (rplacd last list2)) (return (or first list2)))
(or (consp x) (error "UNION not passed a list"))
(if (not (apply #'member1 (car x) list2 (key-list key test test-not)))
(if last (progn (rplacd last (cons (car x) nil))
(setq last (cdr last)))
(progn (setq first (cons (car x) nil))
(setq last first)))) ) )
;(defun nunion (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (cond ((null list1) list2)
; ((apply #'member1 (car list1) list2 rest)
; (apply #'nunion (cdr list1) list2 rest))
; (t
; (rplacd list1
; (apply #'nunion (cdr list1) list2 rest)))))
(defun nunion (list1 list2 &key test test-not key &aux first last)
(do ((x list1 (cdr x)))
((null x) (if last (rplacd last list2)) (return (or first list2)))
(or (consp x) (error "NUNION not passed a list"))
(if (not (apply #'member1 (car x) list2 (key-list key test test-not)))
(progn (if last (rplacd last x)
(setq first x))
(setq last x))) ) )
;(defun intersection (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (cond ((null list1) nil)
; ((apply #'member1 (car list1) list2 rest)
; (cons (car list1)
; (apply #'intersection (cdr list1) list2 rest)))
; (t (apply #'intersection (cdr list1) list2 rest))))
;; all functions in this file should be written as follows:
;; Besides being non recursive, it allows compilation on safety 0
(defun intersection (list1 list2 &key test test-not key &aux ans)
(do ((x list1 (cdr x)))
((null x) (return ans))
(or (consp x) (error "INTERSECTION not passed a list"))
(if (apply #'member1 (car x) list2 (key-list key test test-not))
(setq ans (cons (car x) ans))))
)
;(defun nintersection (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (cond ((null list1) nil)
; ((apply #'member1 (car list1) list2 rest)
; (rplacd list1
; (apply #'nintersection (cdr list1) list2 rest)))
; (t (apply #'nintersection (cdr list1) list2 rest))))
(defun nintersection (list1 list2 &key test test-not key &aux first last)
(do ((x list1 (cdr x)))
((null x) (if last (rplacd last nil)) (return first))
(or (consp x) (error "NINTERSECTION not passed a list"))
(if (apply #'member1 (car x) list2 (key-list key test test-not))
(progn (if last (rplacd last x)
(setq first x))
(setq last x))) ) )
;(defun set-difference (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (cond ((null list1) nil)
; ((not (apply #'member1 (car list1) list2 rest))
; (cons (car list1)
; (apply #'set-difference (cdr list1) list2 rest)))
; (t (apply #'set-difference (cdr list1) list2 rest))))
(defun set-difference (list1 list2 &key test test-not key &aux ans)
(do ((x list1 (cdr x)))
((null x) (return ans))
(or (consp x) (error "SET-DIFFERENCE not passed a list"))
(if (not (apply #'member1 (car x) list2 (key-list key test test-not)))
(setq ans (cons (car x) ans)))) )
(defun set-difference-rev (list1 list2 &key test test-not key &aux ans)
(do ((x list1 (cdr x)))
((null x) (return ans))
(or (consp x) (error "SET-DIFFERENCE not passed a list"))
(if (not (apply #'member1 (car x) list2 :rev t (key-list key test test-not)))
(setq ans (cons (car x) ans)))) )
;(defun nset-difference (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (cond ((null list1) nil)
; ((not (apply #'member1 (car list1) list2 rest))
; (rplacd list1
; (apply #'nset-difference (cdr list1) list2 rest)))
; (t (apply #'nset-difference (cdr list1) list2 rest))))
(defun nset-difference (list1 list2 &key test test-not key &aux first last)
(do ((x list1 (cdr x)))
((null x) (if last (rplacd last nil)) (return first))
(or (consp x) (error "NSET-DIFFERENCE not passed a list"))
(if (not (apply #'member1 (car x) list2 (key-list key test test-not)))
(progn (if last (rplacd last x)
(setq first x))
(setq last x))) ) )
(defun nset-difference-rev (list1 list2 &key test test-not key &aux first last)
(do ((x list1 (cdr x)))
((null x) (if last (rplacd last nil)) (return first))
(or (consp x) (error "NSET-DIFFERENCE not passed a list"))
(if (not (apply #'member1 (car x) list2 :rev t (key-list key test test-not)))
(progn (if last (rplacd last x)
(setq first x))
(setq last x))) ) )
;(defun set-exclusive-or (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (append (apply #'set-difference list1 list2 rest)
; (apply #'set-difference list2 list1 rest)))
(defun set-exclusive-or (list1 list2 &key test test-not key)
(nconc (apply #'set-difference list1 list2 (key-list key test test-not))
(apply #'set-difference-rev list2 list1 (key-list key test test-not))))
;(defun nset-exclusive-or (list1 list2 &rest rest &key test test-not key)
; (declare (ignore test test-not key))
; (nconc (apply #'set-difference list1 list2 rest)
; (apply #'nset-difference list2 list1 rest)))
(defun nset-exclusive-or (list1 list2 &key test test-not key &aux first last fint lint)
(do ((x list1 (cdr x)))
((null x) (if lint (rplacd lint nil))
(if last
(progn (rplacd last
(apply #'nset-difference-rev list2 fint (key-list key test test-not)))
(return first))
(return (apply #'nset-difference-rev list2 fint (key-list key test test-not)))))
(or (consp x) (error "NSET-EXCLUSIVE-OR not passed a list"))
(if (apply #'member1 (car x) list2 (key-list key test test-not))
(progn (if lint (rplacd lint x)
(setq fint x))
(setq lint x))
(progn (if last (rplacd last x)
(setq first x))
(setq last x))) ) )
(defun subsetp (list1 list2 &key test test-not key)
(do ((l list1 (cdr l)))
((null l) t)
(or (consp l) (error "SUBSETP not passed a list"))
(if (not (apply #'member1 (car l) list2 (key-list key test test-not))) (return nil))))
(defmacro tp-error (x y)
`(specific-error :wrong-type-argument "~S is not of type ~S." ,x ',y))
(defun smallnthcdr (n x)
(declare (fixnum n))
(cond ((atom x) (when x (tp-error x proper-list)))
((= n 0) x)
((smallnthcdr (1- n) (cdr x)))))
(defun bignthcdr (n i s f)
(declare (fixnum i))
(cond ((atom f) (when f (tp-error f proper-list)))
((atom (cdr f)) (when (cdr f) (tp-error (cdr f) proper-list)))
((eq s f) (smallnthcdr (mod n i) s))
((bignthcdr n (1+ i) (cdr s) (cddr f)))))
(defun nthcdr (n x)
(declare (optimize (safety 1)))
(cond ((or (not (integerp n)) (minusp n)) (tp-error n (integer 0)))
((< n array-dimension-limit) (smallnthcdr n x))
((atom x) (when x (tp-error x proper-list)))
((atom (cdr x)) (when (cdr x) (tp-error (cdr x) proper-list)))
((bignthcdr n 1 (cdr x) (cddr x)))))
(defun nth (n x)
(declare (optimize (safety 2)))
(car (nthcdr n x)))
(defun first (x)
(declare (optimize (safety 2)))
(car x))
(defun second (x)
(declare (optimize (safety 2)))
(cadr x))
(defun third (x)
(declare (optimize (safety 2)))
(caddr x))
(defun fourth (x)
(declare (optimize (safety 2)))
(cadddr x))
(defun fifth (x)
(declare (optimize (safety 2)))
(car (cddddr x)))
(defun sixth (x)
(declare (optimize (safety 2)))
(cadr (cddddr x)))
(defun seventh (x)
(declare (optimize (safety 2)))
(caddr (cddddr x)))
(defun eighth (x)
(declare (optimize (safety 2)))
(cadddr (cddddr x)))
(defun ninth (x)
(declare (optimize (safety 2)))
(car (cddddr (cddddr x))))
(defun tenth (x)
(declare (optimize (safety 2)))
(cadr (cddddr (cddddr x))))
; Courtesy Paul Dietz
(defmacro nth-value (n expr)
(declare (optimize (safety 1)))
`(nth ,n (multiple-value-list ,expr)))
|