| 1 | ;; list-utils.lisp
|
|---|
| 2 |
|
|---|
| 3 | #|
|
|---|
| 4 | The MIT license.
|
|---|
| 5 |
|
|---|
| 6 | Copyright (c) 2010 Paul L. Krueger
|
|---|
| 7 |
|
|---|
| 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|---|
| 9 | and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|---|
| 10 | including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|---|
| 11 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|---|
| 12 | furnished to do so, subject to the following conditions:
|
|---|
| 13 |
|
|---|
| 14 | The above copyright notice and this permission notice shall be included in all copies or substantial
|
|---|
| 15 | portions of the Software.
|
|---|
| 16 |
|
|---|
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|---|
| 18 | LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|---|
| 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|---|
| 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|---|
| 21 | SOFTWARE 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)
|
|---|