Debian Codegraph @

src/lsp/listlib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1995, Giuseppe Attardi.
6 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 ;;;; list manipulating routines
11
12 (in-package "SYSTEM")
13
14 (defun union (list1 list2 &key test test-not key)
15 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
16 Returns, as a list, the union of elements in LIST1 and in LIST2."
17 (do ((x list1 (cdr x))
18 (first) (last))
19 ((null x)
20 (when last (rplacd last list2))
21 (or first list2))
22 (unless (member1 (car x) list2 test test-not key)
23 (if last
24 (progn (rplacd last (cons (car x) nil))
25 (setq last (cdr last)))
26 (progn (setq first (cons (car x) nil))
27 (setq last first))))))
28
29 (defun nunion (list1 list2 &key test test-not key)
30 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
31 Destructive UNION. Both LIST1 and LIST2 may be destroyed."
32 (do ((x list1 (cdr x))
33 (first) (last))
34 ((null x)
35 (when last (rplacd last list2))
36 (or first list2))
37 (unless (member1 (car x) list2 test test-not key)
38 (if last
39 (rplacd last x)
40 (setq first x))
41 (setq last x))))
42
43 (defun intersection (list1 list2 &key test test-not key)
44 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
45 Returns a list consisting of those objects that are elements of both LIST1 and
46 LIST2."
47 (do ((x list1 (cdr x))
48 (ans))
49 ((null x)
50 (nreverse ans)) ; optional nreverse: not required by CLtL
51 (when (member1 (car x) list2 test test-not key)
52 (push (car x) ans))))
53
54 (defun nintersection (list1 list2 &key test test-not key)
55 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
56 Destructive INTERSECTION. Only LIST1 may be destroyed."
57 (do ((x list1 (cdr x))
58 (first) (last))
59 ((null x)
60 (when last (rplacd last nil))
61 first)
62 (when (member1 (car x) list2 test test-not key)
63 (if last
64 (rplacd last x)
65 (setq first x))
66 (setq last x))))
67
68 (defun set-difference (list1 list2 &key test test-not key)
69 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
70 Returns, as a list, those elements of LIST1 that are not elements of LIST2."
71 (do ((x list1 (cdr x))
72 (ans))
73 ((null x) (nreverse ans))
74 (unless (member1 (car x) list2 test test-not key)
75 (push (car x) ans))))
76
77 (defun nset-difference (list1 list2 &key test test-not key)
78 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
79 Destructive SET-DIFFERENCE. Only LIST1 may be destroyed."
80 (do ((x list1 (cdr x))
81 (first) (last))
82 ((null x)
83 (when last (rplacd last nil))
84 first)
85 (unless (member1 (car x) list2 test test-not key)
86 (if last
87 (rplacd last x)
88 (setq first x))
89 (setq last x))))
90
91 (defun swap-args (f)
92 (declare (si::c-local))
93 (and f #'(lambda (x y) (funcall f y x))))
94
95 (defun set-exclusive-or (list1 list2 &key test test-not key)
96 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
97 Returns, as a list, those elements of LIST1 that are not elements of LIST2 and
98 those elements of LIST2 that are not elements of LIST1."
99 (nconc (set-difference list1 list2 :test test :test-not test-not :key key)
100 (set-difference list2 list1 :test (swap-args test) :test-not (swap-args test-not) :key key)))
101
102 (defun nset-exclusive-or (list1 list2 &key test test-not key)
103 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
104 Destructive SET-EXCLUSIVE-OR. Both LIST1 and LIST2 may be destroyed."
105 (nconc (set-difference list1 list2 :test test :test-not test-not :key key)
106 (nset-difference list2 list1 :test (swap-args test) :test-not (swap-args test-not) :key key)))
107
108 (defun subsetp (list1 list2 &key test test-not key)
109 "Args: (list1 list2 &key (key #'identity) (test #'eql) test-not)
110 Returns T if every element of LIST1 is also an element of LIST2. Returns NIL
111 otherwise."
112 (do ((l list1 (cdr l)))
113 ((null l) t)
114 (unless (member1 (car l) list2 test test-not key)
115 (return nil))))
116
117 (defun rassoc-if (test alist &key key)
118 "Returns the first pair in ALIST whose cdr satisfies TEST. Returns NIL if no
119 such pair exists."
120 (rassoc test alist :test #'funcall :key key))
121 (defun rassoc-if-not (test alist &key key)
122 "Returns the first pair in ALIST whose cdr does not satisfy TEST. Returns NIL
123 if no such pair exists."
124 (rassoc test alist :test-not #'funcall :key key))
125
126 (defun assoc-if (test alist &key key)
127 "Returns the first pair in ALIST whose car satisfies TEST. Returns NIL if no
128 such pair exists."
129 (assoc test alist :test #'funcall :key key))
130 (defun assoc-if-not (test alist &key key)
131 "Returns the first pair in ALIST whose car does not satisfy TEST. Returns NIL
132 if no such pair exists."
133 (assoc test alist :test-not #'funcall :key key))
134
135 (defun member-if (test list &key key)
136 "Searches LIST for an element that satisfies TEST. If found, returns the
137 sublist of LIST that begins with the element. If not found, returns NIL."
138 (member test list :test #'funcall :key key))
139 (defun member-if-not (test list &key key)
140 "Searches LIST for an element that does not satisfy TEST. If found, returns
141 the sublist of LIST that begins with the element. If not found, returns NIL."
142 (member test list :test-not #'funcall :key key))
143
144 (defun subst-if (new test tree &key key)
145 "Substitutes NEW for subtrees of TREE that satisfy TEST and returns the result.
146 The original TREE is not destroyed."
147 (subst new test tree :test #'funcall :key key))
148 (defun subst-if-not (new test tree &key key)
149 "Substitutes NEW for subtrees of TREE that do not satisfy TEST and returns the
150 result. The original TREE is not destroyed."
151 (subst new test tree :test-not #'funcall :key key))
152
153 (defun nsubst-if (new test tree &key key)
154 "Destructive SUBST-IF. TREE may be modified."
155 (nsubst new test tree :test #'funcall :key key))
156 (defun nsubst-if-not (new test tree &key key)
157 "Destructive SUBST-IF-NOT. TREE may be modified."
158 (nsubst new test tree :test-not #'funcall :key key))