| 1 | ;;; replace.lisp | 
|---|
| 2 | ;;; | 
|---|
| 3 | ;;; Copyright (C) 2003-2005 Peter Graves | 
|---|
| 4 | ;;; $Id: replace.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 | ;;; Adapted from CMUCL. | 
|---|
| 33 |  | 
|---|
| 34 | (in-package #:system) | 
|---|
| 35 |  | 
|---|
| 36 | (require "EXTENSIBLE-SEQUENCES-BASE") | 
|---|
| 37 |  | 
|---|
| 38 | #|(eval-when (:compile-toplevel :load-toplevel :execute) | 
|---|
| 39 | (defmacro seq-dispatch (sequence list-form array-form) | 
|---|
| 40 | `(if (listp ,sequence) | 
|---|
| 41 | ,list-form | 
|---|
| 42 | ,array-form)))|# | 
|---|
| 43 |  | 
|---|
| 44 | (eval-when (:compile-toplevel :execute) | 
|---|
| 45 |  | 
|---|
| 46 | ;;; If we are copying around in the same vector, be careful not to copy the | 
|---|
| 47 | ;;; same elements over repeatedly.  We do this by copying backwards. | 
|---|
| 48 | (defmacro mumble-replace-from-mumble () | 
|---|
| 49 | `(if (and (eq target-sequence source-sequence) (> target-start source-start)) | 
|---|
| 50 | (let ((nelts (min (- target-end target-start) (- source-end source-start)))) | 
|---|
| 51 | (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1) | 
|---|
| 52 | (1- target-index)) | 
|---|
| 53 | (source-index (+ (the fixnum source-start) (the fixnum nelts) -1) | 
|---|
| 54 | (1- source-index))) | 
|---|
| 55 | ((= target-index (the fixnum (1- target-start))) target-sequence) | 
|---|
| 56 | (declare (fixnum target-index source-index)) | 
|---|
| 57 | (setf (aref target-sequence target-index) | 
|---|
| 58 | (aref source-sequence source-index)))) | 
|---|
| 59 | (do ((target-index target-start (1+ target-index)) | 
|---|
| 60 | (source-index source-start (1+ source-index))) | 
|---|
| 61 | ((or (= target-index (the fixnum target-end)) | 
|---|
| 62 | (= source-index (the fixnum source-end))) | 
|---|
| 63 | target-sequence) | 
|---|
| 64 | (declare (fixnum target-index source-index)) | 
|---|
| 65 | (setf (aref target-sequence target-index) | 
|---|
| 66 | (aref source-sequence source-index))))) | 
|---|
| 67 |  | 
|---|
| 68 | (defmacro list-replace-from-list () | 
|---|
| 69 | `(if (and (eq target-sequence source-sequence) (> target-start source-start)) | 
|---|
| 70 | (let ((new-elts (subseq source-sequence source-start | 
|---|
| 71 | (+ (the fixnum source-start) | 
|---|
| 72 | (the fixnum | 
|---|
| 73 | (min (- (the fixnum target-end) | 
|---|
| 74 | (the fixnum target-start)) | 
|---|
| 75 | (- (the fixnum source-end) | 
|---|
| 76 | (the fixnum source-start)))))))) | 
|---|
| 77 | (do ((n new-elts (cdr n)) | 
|---|
| 78 | (o (nthcdr target-start target-sequence) (cdr o))) | 
|---|
| 79 | ((null n) target-sequence) | 
|---|
| 80 | (rplaca o (car n)))) | 
|---|
| 81 | (do ((target-index target-start (1+ target-index)) | 
|---|
| 82 | (source-index source-start (1+ source-index)) | 
|---|
| 83 | (target-sequence-ref (nthcdr target-start target-sequence) | 
|---|
| 84 | (cdr target-sequence-ref)) | 
|---|
| 85 | (source-sequence-ref (nthcdr source-start source-sequence) | 
|---|
| 86 | (cdr source-sequence-ref))) | 
|---|
| 87 | ((or (= target-index (the fixnum target-end)) | 
|---|
| 88 | (= source-index (the fixnum source-end)) | 
|---|
| 89 | (null target-sequence-ref) (null source-sequence-ref)) | 
|---|
| 90 | target-sequence) | 
|---|
| 91 | (declare (fixnum target-index source-index)) | 
|---|
| 92 | (rplaca target-sequence-ref (car source-sequence-ref))))) | 
|---|
| 93 |  | 
|---|
| 94 | (defmacro list-replace-from-mumble () | 
|---|
| 95 | `(do ((target-index target-start (1+ target-index)) | 
|---|
| 96 | (source-index source-start (1+ source-index)) | 
|---|
| 97 | (target-sequence-ref (nthcdr target-start target-sequence) | 
|---|
| 98 | (cdr target-sequence-ref))) | 
|---|
| 99 | ((or (= target-index (the fixnum target-end)) | 
|---|
| 100 | (= source-index (the fixnum source-end)) | 
|---|
| 101 | (null target-sequence-ref)) | 
|---|
| 102 | target-sequence) | 
|---|
| 103 | (declare (fixnum source-index target-index)) | 
|---|
| 104 | (rplaca target-sequence-ref (aref source-sequence source-index)))) | 
|---|
| 105 |  | 
|---|
| 106 | (defmacro mumble-replace-from-list () | 
|---|
| 107 | `(do ((target-index target-start (1+ target-index)) | 
|---|
| 108 | (source-index source-start (1+ source-index)) | 
|---|
| 109 | (source-sequence (nthcdr source-start source-sequence) | 
|---|
| 110 | (cdr source-sequence))) | 
|---|
| 111 | ((or (= target-index (the fixnum target-end)) | 
|---|
| 112 | (= source-index (the fixnum source-end)) | 
|---|
| 113 | (null source-sequence)) | 
|---|
| 114 | target-sequence) | 
|---|
| 115 | (declare (fixnum target-index source-index)) | 
|---|
| 116 | (setf (aref target-sequence target-index) (car source-sequence)))) | 
|---|
| 117 |  | 
|---|
| 118 | ) ; eval-when | 
|---|
| 119 |  | 
|---|
| 120 | ;;; The support routines for REPLACE are used by compiler transforms, so we | 
|---|
| 121 | ;;; worry about dealing with end being supplied as or defaulting to nil | 
|---|
| 122 | ;;; at this level. | 
|---|
| 123 |  | 
|---|
| 124 | (defun list-replace-from-list* (target-sequence source-sequence target-start | 
|---|
| 125 | target-end source-start source-end) | 
|---|
| 126 | (when (null target-end) (setq target-end (length target-sequence))) | 
|---|
| 127 | (when (null source-end) (setq source-end (length source-sequence))) | 
|---|
| 128 | (list-replace-from-list)) | 
|---|
| 129 |  | 
|---|
| 130 | (defun list-replace-from-vector* (target-sequence source-sequence target-start | 
|---|
| 131 | target-end source-start source-end) | 
|---|
| 132 | (when (null target-end) (setq target-end (length target-sequence))) | 
|---|
| 133 | (when (null source-end) (setq source-end (length source-sequence))) | 
|---|
| 134 | (list-replace-from-mumble)) | 
|---|
| 135 |  | 
|---|
| 136 | (defun vector-replace-from-list* (target-sequence source-sequence target-start | 
|---|
| 137 | target-end source-start source-end) | 
|---|
| 138 | (when (null target-end) (setq target-end (length target-sequence))) | 
|---|
| 139 | (when (null source-end) (setq source-end (length source-sequence))) | 
|---|
| 140 | (mumble-replace-from-list)) | 
|---|
| 141 |  | 
|---|
| 142 | (defun vector-replace-from-vector* (target-sequence source-sequence | 
|---|
| 143 | target-start target-end source-start | 
|---|
| 144 | source-end) | 
|---|
| 145 | (when (null target-end) (setq target-end (length target-sequence))) | 
|---|
| 146 | (when (null source-end) (setq source-end (length source-sequence))) | 
|---|
| 147 | (mumble-replace-from-mumble)) | 
|---|
| 148 |  | 
|---|
| 149 | ;;; REPLACE cannot default end arguments to the length of sequence since it | 
|---|
| 150 | ;;; is not an error to supply nil for their values.  We must test for ends | 
|---|
| 151 | ;;; being nil in the body of the function. | 
|---|
| 152 | (defun replace (target-sequence source-sequence &rest args &key | 
|---|
| 153 | ((:start1 target-start) 0) | 
|---|
| 154 | ((:end1 target-end)) | 
|---|
| 155 | ((:start2 source-start) 0) | 
|---|
| 156 | ((:end2 source-end))) | 
|---|
| 157 | "The target sequence is destructively modified by copying successive | 
|---|
| 158 | elements into it from the source sequence." | 
|---|
| 159 | (let ((target-end (or target-end (length target-sequence))) | 
|---|
| 160 | (source-end (or source-end (length source-sequence)))) | 
|---|
| 161 | (declare (type (integer 0 #.most-positive-fixnum) target-start target-end source-start source-end)) | 
|---|
| 162 | (sequence::seq-dispatch target-sequence | 
|---|
| 163 | (sequence::seq-dispatch source-sequence | 
|---|
| 164 | (list-replace-from-list) | 
|---|
| 165 | (list-replace-from-mumble) | 
|---|
| 166 | (apply #'sequence:replace target-sequence source-sequence args)) | 
|---|
| 167 | (sequence::seq-dispatch source-sequence | 
|---|
| 168 | (mumble-replace-from-list) | 
|---|
| 169 | (mumble-replace-from-mumble) | 
|---|
| 170 | (apply #'sequence:replace target-sequence source-sequence args)) | 
|---|
| 171 | (apply #'sequence:replace target-sequence source-sequence args)))) | 
|---|