source: release/1.5/source/contrib/krueger/InterfaceProjects/Utilities/list-utils.lisp

Last change on this file was 13646, checked in by R. Matthew Emerson, 15 years ago

Merge r13631, r13636 from trunk. (Paul Krueger's updated InterfaceProjects
contrib; fix for ticket:652)

File size: 3.8 KB
RevLine 
[13631]1;; list-utils.lisp
2
3#|
4The MIT license.
5
6Copyright (c) 2010 Paul L. Krueger
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of this software
9and associated documentation files (the "Software"), to deal in the Software without restriction,
10including without limitation the rights to use, copy, modify, merge, publish, distribute,
11sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all copies or substantial
15portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23|#
24
25(defpackage :interface-utilities
26 (:nicknames :iu)
27 (:export add-to-list-at
28 delete-from-list
29 find-cdr
30 sort-list-in-place))
31
32(in-package :iu)
33
34(defun add-to-list-at (lst indx value)
35 ;; splices value into lst at the designated indx
36 ;; if indx = 0 we make sure that the lst pointer remains
37 ;; valid.
38 (cond ((null lst)
39 (setf lst (list value)))
40 ((>= indx (list-length lst))
41 (nconc lst (list value)))
42 (t
43 (let* ((nth-ptr (nthcdr indx lst))
44 (new-cons (cons (car nth-ptr) (cdr nth-ptr))))
45 ;; splice in the new value at the position of the specified indx
46 (setf (car nth-ptr) value)
47 (setf (cdr nth-ptr) new-cons))))
48 lst)
49
50(defun delete-from-list (lst thing)
51 (cond ((eql 1 (list-length lst))
52 nil)
53 ((eq thing (first lst))
54 (setf (car lst) (cadr lst))
55 (setf (cdr lst) (cddr lst))
56 (if (and (null (car lst)) (null (cdr lst)))
57 nil
58 lst))
59 (t
60 (delete thing lst))))
61
62(defun find-cdr (lst cdr-thing)
63 ;; return the cons cell with a cdr eq to cdr-thing
64 (do* ((cons-cell lst (cdr cons-cell)))
65 ((or (null cons-cell) (eq (cdr cons-cell) cdr-thing)) cons-cell)))
66
67(defun sort-list-in-place (lst sort-pred &optional (sort-key nil))
68 ;; Sorting a list normally results in a reordering of the cons
69 ;; cells. We swap the contents of cons cells around after sorting
70 ;; so that previous references to the list are still valid.
71 ;; That way we can sort "in place" without having to copy the whole list.
72 (let ((old-first-cons lst)
73 (new-first-cons nil))
74 (if sort-key
75 (setf new-first-cons (sort lst sort-pred :key sort-key))
76 (setf new-first-cons (sort lst sort-pred)))
77 (let* ((prev-cons (find-cdr new-first-cons old-first-cons))
78 (new-first-car (car new-first-cons))
79 (new-first-cdr (cdr new-first-cons)))
80 (when prev-cons
81 ;; exchange the two cons cells: the one that used to
82 ;; be the first (called the old-first-cons) with the
83 ;; first in the sorted list (called the new-first-cons)
84 ;; first exchange cars
85 (setf (car new-first-cons) (car old-first-cons))
86 (setf (car old-first-cons) new-first-car)
87 ;; always set cdr of new-first-cons to the cdr of the old-first-cons
88 (setf (cdr new-first-cons) (cdr old-first-cons))
89 ;; have to be more careful about other cdr pointers
90 (if (eq prev-cons new-first-cons)
91 (setf (cdr old-first-cons) new-first-cons)
92 (progn
93 (setf (cdr prev-cons) new-first-cons)
94 (setf (cdr old-first-cons) new-first-cdr)))))
95 lst))
96
97(provide :list-utils)
Note: See TracBrowser for help on using the repository browser.