| 1 | ;;; delete.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2003 Peter Graves |
|---|
| 4 | ;;; $Id: delete.lisp 12516 2010-03-03 21:05:41Z astalla $ |
|---|
| 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 | ;;; |
|---|
| 20 | ;;; As a special exception, the copyright holders of this library give you |
|---|
| 21 | ;;; permission to link this library with independent modules to produce an |
|---|
| 22 | ;;; executable, regardless of the license terms of these independent |
|---|
| 23 | ;;; modules, and to copy and distribute the resulting executable under |
|---|
| 24 | ;;; terms of your choice, provided that you also meet, for each linked |
|---|
| 25 | ;;; independent module, the terms and conditions of the license of that |
|---|
| 26 | ;;; module. An independent module is a module which is not derived from |
|---|
| 27 | ;;; or based on this library. If you modify this library, you may extend |
|---|
| 28 | ;;; this exception to your version of the library, but you are not |
|---|
| 29 | ;;; obligated to do so. If you do not wish to do so, delete this |
|---|
| 30 | ;;; exception statement from your version. |
|---|
| 31 | |
|---|
| 32 | (in-package "SYSTEM") |
|---|
| 33 | |
|---|
| 34 | (require "EXTENSIBLE-SEQUENCES-BASE") |
|---|
| 35 | |
|---|
| 36 | ;;; From CMUCL. |
|---|
| 37 | |
|---|
| 38 | (defmacro real-count (count) |
|---|
| 39 | `(cond ((null ,count) most-positive-fixnum) |
|---|
| 40 | ((fixnump ,count) (if (minusp ,count) 0 ,count)) |
|---|
| 41 | ((integerp ,count) (if (minusp ,count) 0 most-positive-fixnum)) |
|---|
| 42 | (t ,count))) |
|---|
| 43 | |
|---|
| 44 | (defmacro mumble-delete (pred) |
|---|
| 45 | `(do ((index start (1+ index)) |
|---|
| 46 | (jndex start) |
|---|
| 47 | (number-zapped 0)) |
|---|
| 48 | ((or (= index end) (= number-zapped count)) |
|---|
| 49 | (do ((index index (1+ index)) ; copy the rest of the vector |
|---|
| 50 | (jndex jndex (1+ jndex))) |
|---|
| 51 | ((= index length) |
|---|
| 52 | (shrink-vector sequence jndex)) |
|---|
| 53 | (aset sequence jndex (aref sequence index)))) |
|---|
| 54 | (aset sequence jndex (aref sequence index)) |
|---|
| 55 | (if ,pred |
|---|
| 56 | (setq number-zapped (1+ number-zapped)) |
|---|
| 57 | (setq jndex (1+ jndex))))) |
|---|
| 58 | |
|---|
| 59 | (defmacro mumble-delete-from-end (pred) |
|---|
| 60 | `(do ((index (1- end) (1- index)) ; find the losers |
|---|
| 61 | (number-zapped 0) |
|---|
| 62 | (losers ()) |
|---|
| 63 | this-element |
|---|
| 64 | (terminus (1- start))) |
|---|
| 65 | ((or (= index terminus) (= number-zapped count)) |
|---|
| 66 | (do ((losers losers) ; delete the losers |
|---|
| 67 | (index start (1+ index)) |
|---|
| 68 | (jndex start)) |
|---|
| 69 | ((or (null losers) (= index end)) |
|---|
| 70 | (do ((index index (1+ index)) ; copy the rest of the vector |
|---|
| 71 | (jndex jndex (1+ jndex))) |
|---|
| 72 | ((= index length) |
|---|
| 73 | (shrink-vector sequence jndex)) |
|---|
| 74 | (aset sequence jndex (aref sequence index)))) |
|---|
| 75 | (aset sequence jndex (aref sequence index)) |
|---|
| 76 | (if (= index (car losers)) |
|---|
| 77 | (pop losers) |
|---|
| 78 | (setq jndex (1+ jndex))))) |
|---|
| 79 | (setq this-element (aref sequence index)) |
|---|
| 80 | (when ,pred |
|---|
| 81 | (setq number-zapped (1+ number-zapped)) |
|---|
| 82 | (push index losers)))) |
|---|
| 83 | |
|---|
| 84 | (defmacro normal-mumble-delete () |
|---|
| 85 | `(mumble-delete |
|---|
| 86 | (if test-not |
|---|
| 87 | (not (funcall test-not item (funcall-key key (aref sequence index)))) |
|---|
| 88 | (funcall test item (funcall-key key (aref sequence index)))))) |
|---|
| 89 | |
|---|
| 90 | (defmacro normal-mumble-delete-from-end () |
|---|
| 91 | `(mumble-delete-from-end |
|---|
| 92 | (if test-not |
|---|
| 93 | (not (funcall test-not item (funcall-key key this-element))) |
|---|
| 94 | (funcall test item (funcall-key key this-element))))) |
|---|
| 95 | |
|---|
| 96 | (defmacro list-delete (pred) |
|---|
| 97 | `(let ((handle (cons nil sequence))) |
|---|
| 98 | (do ((current (nthcdr start sequence) (cdr current)) |
|---|
| 99 | (previous (nthcdr start handle)) |
|---|
| 100 | (index start (1+ index)) |
|---|
| 101 | (number-zapped 0)) |
|---|
| 102 | ((or (= index end) (= number-zapped count)) |
|---|
| 103 | (cdr handle)) |
|---|
| 104 | (cond (,pred |
|---|
| 105 | (rplacd previous (cdr current)) |
|---|
| 106 | (setq number-zapped (1+ number-zapped))) |
|---|
| 107 | (t |
|---|
| 108 | (setq previous (cdr previous))))))) |
|---|
| 109 | |
|---|
| 110 | (defmacro list-delete-from-end (pred) |
|---|
| 111 | `(let* ((reverse (nreverse sequence)) |
|---|
| 112 | (handle (cons nil reverse))) |
|---|
| 113 | (do ((current (nthcdr (- length end) reverse) |
|---|
| 114 | (cdr current)) |
|---|
| 115 | (previous (nthcdr (- length end) handle)) |
|---|
| 116 | (index start (1+ index)) |
|---|
| 117 | (number-zapped 0)) |
|---|
| 118 | ((or (= index end) (= number-zapped count)) |
|---|
| 119 | (nreverse (cdr handle))) |
|---|
| 120 | (cond (,pred |
|---|
| 121 | (rplacd previous (cdr current)) |
|---|
| 122 | (setq number-zapped (1+ number-zapped))) |
|---|
| 123 | (t |
|---|
| 124 | (setq previous (cdr previous))))))) |
|---|
| 125 | |
|---|
| 126 | (defmacro normal-list-delete () |
|---|
| 127 | '(list-delete |
|---|
| 128 | (if test-not |
|---|
| 129 | (not (funcall test-not item (funcall-key key (car current)))) |
|---|
| 130 | (funcall test item (funcall-key key (car current)))))) |
|---|
| 131 | |
|---|
| 132 | (defmacro normal-list-delete-from-end () |
|---|
| 133 | '(list-delete-from-end |
|---|
| 134 | (if test-not |
|---|
| 135 | (not (funcall test-not item (funcall-key key (car current)))) |
|---|
| 136 | (funcall test item (funcall-key key (car current)))))) |
|---|
| 137 | |
|---|
| 138 | (defun delete (item sequence &rest args &key from-end (test #'eql) test-not |
|---|
| 139 | (start 0) end count key) |
|---|
| 140 | (when key |
|---|
| 141 | (setq key (coerce-to-function key))) |
|---|
| 142 | (let* ((length (length sequence)) |
|---|
| 143 | (end (or end length)) |
|---|
| 144 | (count (real-count count))) |
|---|
| 145 | (sequence::seq-dispatch sequence |
|---|
| 146 | (if from-end |
|---|
| 147 | (normal-list-delete-from-end) |
|---|
| 148 | (normal-list-delete)) |
|---|
| 149 | (if from-end |
|---|
| 150 | (normal-mumble-delete-from-end) |
|---|
| 151 | (normal-mumble-delete)) |
|---|
| 152 | (apply #'sequence:delete item sequence args)))) |
|---|
| 153 | |
|---|
| 154 | (defmacro if-mumble-delete () |
|---|
| 155 | `(mumble-delete |
|---|
| 156 | (funcall predicate (funcall-key key (aref sequence index))))) |
|---|
| 157 | |
|---|
| 158 | (defmacro if-mumble-delete-from-end () |
|---|
| 159 | `(mumble-delete-from-end |
|---|
| 160 | (funcall predicate (funcall-key key this-element)))) |
|---|
| 161 | |
|---|
| 162 | (defmacro if-list-delete () |
|---|
| 163 | '(list-delete |
|---|
| 164 | (funcall predicate (funcall-key key (car current))))) |
|---|
| 165 | |
|---|
| 166 | (defmacro if-list-delete-from-end () |
|---|
| 167 | '(list-delete-from-end |
|---|
| 168 | (funcall predicate (funcall-key key (car current))))) |
|---|
| 169 | |
|---|
| 170 | (defun delete-if (predicate sequence &rest args &key from-end (start 0) |
|---|
| 171 | key end count) |
|---|
| 172 | (when key |
|---|
| 173 | (setq key (coerce-to-function key))) |
|---|
| 174 | (let* ((length (length sequence)) |
|---|
| 175 | (end (or end length)) |
|---|
| 176 | (count (real-count count))) |
|---|
| 177 | (sequence::seq-dispatch sequence |
|---|
| 178 | (if from-end |
|---|
| 179 | (if-list-delete-from-end) |
|---|
| 180 | (if-list-delete)) |
|---|
| 181 | (if from-end |
|---|
| 182 | (if-mumble-delete-from-end) |
|---|
| 183 | (if-mumble-delete)) |
|---|
| 184 | (apply #'sequence:delete-if predicate sequence args)))) |
|---|
| 185 | |
|---|
| 186 | (defmacro if-not-mumble-delete () |
|---|
| 187 | `(mumble-delete |
|---|
| 188 | (not (funcall predicate (funcall-key key (aref sequence index)))))) |
|---|
| 189 | |
|---|
| 190 | (defmacro if-not-mumble-delete-from-end () |
|---|
| 191 | `(mumble-delete-from-end |
|---|
| 192 | (not (funcall predicate (funcall-key key this-element))))) |
|---|
| 193 | |
|---|
| 194 | (defmacro if-not-list-delete () |
|---|
| 195 | '(list-delete |
|---|
| 196 | (not (funcall predicate (funcall-key key (car current)))))) |
|---|
| 197 | |
|---|
| 198 | (defmacro if-not-list-delete-from-end () |
|---|
| 199 | '(list-delete-from-end |
|---|
| 200 | (not (funcall predicate (funcall-key key (car current)))))) |
|---|
| 201 | |
|---|
| 202 | (defun delete-if-not (predicate sequence &rest args &key from-end (start 0) |
|---|
| 203 | end key count) |
|---|
| 204 | (when key |
|---|
| 205 | (setq key (coerce-to-function key))) |
|---|
| 206 | (let* ((length (length sequence)) |
|---|
| 207 | (end (or end length)) |
|---|
| 208 | (count (real-count count))) |
|---|
| 209 | (sequence::seq-dispatch sequence |
|---|
| 210 | (if from-end |
|---|
| 211 | (if-not-list-delete-from-end) |
|---|
| 212 | (if-not-list-delete)) |
|---|
| 213 | (if from-end |
|---|
| 214 | (if-not-mumble-delete-from-end) |
|---|
| 215 | (if-not-mumble-delete)) |
|---|
| 216 | (apply #'sequence:delete-if-not predicate sequence args)))) |
|---|