| 1 | ;;; substitute.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2003 Peter Graves |
|---|
| 4 | ;;; $Id: substitute.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 | (require "EXTENSIBLE-SEQUENCES-BASE") |
|---|
| 33 | |
|---|
| 34 | (in-package "COMMON-LISP") |
|---|
| 35 | |
|---|
| 36 | (export '(substitute substitute-if substitute-if-not)) |
|---|
| 37 | |
|---|
| 38 | ;;; From CMUCL. |
|---|
| 39 | |
|---|
| 40 | (defmacro real-count (count) |
|---|
| 41 | `(cond ((null ,count) most-positive-fixnum) |
|---|
| 42 | ((sys::fixnump ,count) (if (minusp ,count) 0 ,count)) |
|---|
| 43 | ((integerp ,count) (if (minusp ,count) 0 most-positive-fixnum)) |
|---|
| 44 | (t ,count))) |
|---|
| 45 | |
|---|
| 46 | (defun list-substitute* (pred new list start end count key test test-not old) |
|---|
| 47 | (let* ((result (list nil)) |
|---|
| 48 | elt |
|---|
| 49 | (splice result) |
|---|
| 50 | (list list)) ; Get a local list for a stepper. |
|---|
| 51 | (do ((index 0 (1+ index))) |
|---|
| 52 | ((= index start)) |
|---|
| 53 | (setq splice (cdr (rplacd splice (list (car list))))) |
|---|
| 54 | (setq list (cdr list))) |
|---|
| 55 | (do ((index start (1+ index))) |
|---|
| 56 | ((or (= index end) (null list) (= count 0))) |
|---|
| 57 | (setq elt (car list)) |
|---|
| 58 | (setq splice |
|---|
| 59 | (cdr (rplacd splice |
|---|
| 60 | (list |
|---|
| 61 | (cond |
|---|
| 62 | ((case pred |
|---|
| 63 | (normal |
|---|
| 64 | (if test-not |
|---|
| 65 | (not |
|---|
| 66 | (funcall test-not old (sys::apply-key key elt))) |
|---|
| 67 | (funcall test old (sys::apply-key key elt)))) |
|---|
| 68 | (if (funcall test (sys::apply-key key elt))) |
|---|
| 69 | (if-not (not (funcall test (sys::apply-key key elt))))) |
|---|
| 70 | (setq count (1- count)) |
|---|
| 71 | new) |
|---|
| 72 | (t elt)))))) |
|---|
| 73 | (setq list (cdr list))) |
|---|
| 74 | (do () |
|---|
| 75 | ((null list)) |
|---|
| 76 | (setq splice (cdr (rplacd splice (list (car list))))) |
|---|
| 77 | (setq list (cdr list))) |
|---|
| 78 | (cdr result))) |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | ;;; Replace old with new in sequence moving from left to right by incrementer |
|---|
| 82 | ;;; on each pass through the loop. Called by all three substitute functions. |
|---|
| 83 | (defun vector-substitute* (pred new sequence incrementer left right length |
|---|
| 84 | start end count key test test-not old) |
|---|
| 85 | (let ((result (sys::make-sequence-like sequence length)) |
|---|
| 86 | (index left)) |
|---|
| 87 | (do () |
|---|
| 88 | ((= index start)) |
|---|
| 89 | (setf (aref result index) (aref sequence index)) |
|---|
| 90 | (setq index (+ index incrementer))) |
|---|
| 91 | (do ((elt)) |
|---|
| 92 | ((or (= index end) (= count 0))) |
|---|
| 93 | (setq elt (aref sequence index)) |
|---|
| 94 | (setf (aref result index) |
|---|
| 95 | (cond ((case pred |
|---|
| 96 | (normal |
|---|
| 97 | (if test-not |
|---|
| 98 | (not (funcall test-not old (sys::apply-key key elt))) |
|---|
| 99 | (funcall test old (sys::apply-key key elt)))) |
|---|
| 100 | (if (funcall test (sys::apply-key key elt))) |
|---|
| 101 | (if-not (not (funcall test (sys::apply-key key elt))))) |
|---|
| 102 | (setq count (1- count)) |
|---|
| 103 | new) |
|---|
| 104 | (t elt))) |
|---|
| 105 | (setq index (+ index incrementer))) |
|---|
| 106 | (do () |
|---|
| 107 | ((= index right)) |
|---|
| 108 | (setf (aref result index) (aref sequence index)) |
|---|
| 109 | (setq index (+ index incrementer))) |
|---|
| 110 | result)) |
|---|
| 111 | |
|---|
| 112 | (defmacro subst-dispatch (pred) |
|---|
| 113 | `(sequence::seq-dispatch sequence |
|---|
| 114 | (if from-end |
|---|
| 115 | (nreverse (list-substitute* ,pred new (reverse sequence) |
|---|
| 116 | (- length end) |
|---|
| 117 | (- length start) |
|---|
| 118 | count key test test-not old)) |
|---|
| 119 | (list-substitute* ,pred new sequence start end count key test test-not |
|---|
| 120 | old)) |
|---|
| 121 | (if from-end |
|---|
| 122 | (vector-substitute* ,pred new sequence -1 (1- length) |
|---|
| 123 | -1 length (1- end) |
|---|
| 124 | (1- start) count key test test-not old) |
|---|
| 125 | (vector-substitute* ,pred new sequence 1 0 length length |
|---|
| 126 | start end count key test test-not old)) |
|---|
| 127 | ,(ecase (cadr pred) ;;pred is (quote <foo>) |
|---|
| 128 | (normal `(apply #'sequence:substitute new old sequence args)) |
|---|
| 129 | (if `(apply #'sequence:substitute-if new test sequence args)) |
|---|
| 130 | (if-not `(apply #'sequence:substitute-if-not new test sequence args))))) |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | (defun substitute (new old sequence &rest args &key from-end (test #'eql) test-not |
|---|
| 134 | (start 0) count end key) |
|---|
| 135 | (let* ((length (length sequence)) |
|---|
| 136 | (end (or end length)) |
|---|
| 137 | (count (real-count count))) |
|---|
| 138 | (subst-dispatch 'normal))) |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | (defun substitute-if (new test sequence &rest args &key from-end (start 0) end count key) |
|---|
| 142 | (let* ((length (length sequence)) |
|---|
| 143 | (end (or end length)) |
|---|
| 144 | (count (real-count count)) |
|---|
| 145 | test-not |
|---|
| 146 | old) |
|---|
| 147 | (subst-dispatch 'if))) |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | (defun substitute-if-not (new test sequence &rest args &key from-end (start 0) |
|---|
| 151 | end count key) |
|---|
| 152 | (let* ((length (length sequence)) |
|---|
| 153 | (end (or end length)) |
|---|
| 154 | (count (real-count count)) |
|---|
| 155 | test-not |
|---|
| 156 | old) |
|---|
| 157 | (subst-dispatch 'if-not))) |
|---|