source: trunk/j/src/org/armedbear/lisp/remove.lisp @ 11391

Last change on this file since 11391 was 11391, checked in by vvoutilainen, 12 years ago

ABCL license is GPL + Classpath exception. This was intended
by Peter Graves, the original author. For reference, see
http://sourceforge.net/mailarchive/forum.php?thread_name=20040721115302.839%40prufrock&forum_name=armedbear-j-announce

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1;;; remove.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: remove.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $
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(resolve 'delete) ; MUMBLE-DELETE-FROM-END
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-remove-macro (bump left begin finish right pred)
45  `(do ((index ,begin (,bump index))
46        (result
47         (do ((index ,left (,bump index))
48              (result (make-sequence-like sequence length)))
49           ((= index ,begin) result)
50           (aset result index (aref sequence index))))
51        (new-index ,begin)
52        (number-zapped 0)
53        (this-element))
54     ((or (= index ,finish) (= number-zapped count))
55      (do ((index index (,bump index))
56           (new-index new-index (,bump new-index)))
57        ((= index ,right) (shrink-vector result new-index))
58        (aset result new-index (aref sequence index))))
59     (setq this-element (aref sequence index))
60     (cond (,pred (setq number-zapped (1+ number-zapped)))
61           (t (aset result new-index this-element)
62              (setq new-index (,bump new-index))))))
63
64(defmacro mumble-remove (pred)
65  `(mumble-remove-macro 1+ 0 start end length ,pred))
66
67(defmacro mumble-remove-from-end (pred)
68  `(let ((sequence (copy-seq sequence)))
69     (mumble-delete-from-end ,pred)))
70
71(defmacro normal-mumble-remove ()
72  `(mumble-remove
73    (if test-not
74        (not (funcall test-not item (apply-key key this-element)))
75        (funcall test item (apply-key key this-element)))))
76
77(defmacro normal-mumble-remove-from-end ()
78  `(mumble-remove-from-end
79    (if test-not
80        (not (funcall test-not item (apply-key key this-element)))
81        (funcall test item (apply-key key this-element)))))
82
83(defmacro if-mumble-remove ()
84  `(mumble-remove (funcall predicate (apply-key key this-element))))
85
86(defmacro if-mumble-remove-from-end ()
87  `(mumble-remove-from-end (funcall predicate (apply-key key this-element))))
88
89(defmacro if-not-mumble-remove ()
90  `(mumble-remove (not (funcall predicate (apply-key key this-element)))))
91
92(defmacro if-not-mumble-remove-from-end ()
93  `(mumble-remove-from-end
94    (not (funcall predicate (apply-key key this-element)))))
95
96(defmacro list-remove-macro (pred reverse-p)
97  `(let* ((sequence ,(if reverse-p
98                         '(reverse sequence)
99                         'sequence))
100          (%start ,(if reverse-p '(- length end) 'start))
101          (%end ,(if reverse-p '(- length start) 'end))
102          (splice (list nil))
103          (results (do ((index 0 (1+ index))
104                        (before-start splice))
105                     ((= index %start) before-start)
106                     (setq splice
107                           (cdr (rplacd splice (list (pop sequence))))))))
108     (do ((index %start (1+ index))
109          (this-element)
110          (number-zapped 0))
111       ((or (= index %end) (= number-zapped count))
112        (do ((index index (1+ index)))
113          ((null sequence)
114           ,(if reverse-p
115                '(nreverse (cdr results))
116                '(cdr results)))
117          (setq splice (cdr (rplacd splice (list (pop sequence)))))))
118       (setq this-element (pop sequence))
119       (if ,pred
120           (setq number-zapped (1+ number-zapped))
121           (setq splice (cdr (rplacd splice (list this-element))))))))
122
123
124(defmacro list-remove (pred)
125  `(list-remove-macro ,pred nil))
126
127(defmacro list-remove-from-end (pred)
128  `(list-remove-macro ,pred t))
129
130(defmacro normal-list-remove ()
131  `(list-remove
132    (if test-not
133        (not (funcall test-not item (apply-key key this-element)))
134        (funcall test item (apply-key key this-element)))))
135
136(defmacro normal-list-remove-from-end ()
137  `(list-remove-from-end
138    (if test-not
139        (not (funcall test-not item (apply-key key this-element)))
140        (funcall test item (apply-key key this-element)))))
141
142(defmacro if-list-remove ()
143  `(list-remove
144    (funcall predicate (apply-key key this-element))))
145
146(defmacro if-list-remove-from-end ()
147  `(list-remove-from-end
148    (funcall predicate (apply-key key this-element))))
149
150(defmacro if-not-list-remove ()
151  `(list-remove
152    (not (funcall predicate (apply-key key this-element)))))
153
154(defmacro if-not-list-remove-from-end ()
155  `(list-remove-from-end
156    (not (funcall predicate (apply-key key this-element)))))
157
158(defun remove (item sequence &key from-end (test #'eql) test-not (start 0)
159                    end count key)
160  (let* ((length (length sequence))
161   (end (or end length))
162   (count (real-count count)))
163    (if (listp sequence)
164        (if from-end
165            (normal-list-remove-from-end)
166            (normal-list-remove))
167        (if from-end
168            (normal-mumble-remove-from-end)
169            (normal-mumble-remove)))))
170
171(defun remove-if (predicate sequence &key from-end (start 0) end count key)
172  (let* ((length (length sequence))
173   (end (or end length))
174   (count (real-count count)))
175    (if (listp sequence)
176        (if from-end
177            (if-list-remove-from-end)
178            (if-list-remove))
179        (if from-end
180            (if-mumble-remove-from-end)
181            (if-mumble-remove)))))
182
183(defun remove-if-not (predicate sequence &key from-end (start 0) end count key)
184  (let* ((length (length sequence))
185   (end (or end length))
186   (count (real-count count)))
187    (if (listp sequence)
188        (if from-end
189            (if-not-list-remove-from-end)
190            (if-not-list-remove))
191        (if from-end
192            (if-not-mumble-remove-from-end)
193            (if-not-mumble-remove)))))
Note: See TracBrowser for help on using the repository browser.