[7238] | 1 | ;;; alist.el --- utility functions for association list |
---|
| 2 | |
---|
| 3 | ;; Copyright (C) 1993,1994,1995,1996,1998,2000 Free Software Foundation, Inc. |
---|
| 4 | |
---|
| 5 | ;; Author: MORIOKA Tomohiko <tomo@m17n.org> |
---|
| 6 | ;; Keywords: alist |
---|
| 7 | |
---|
| 8 | ;; This file is part of GNU Emacs. |
---|
| 9 | |
---|
| 10 | ;; This program is free software; you can redistribute it and/or |
---|
| 11 | ;; modify it under the terms of the GNU General Public License as |
---|
| 12 | ;; published by the Free Software Foundation; either version 2, or (at |
---|
| 13 | ;; your option) any later version. |
---|
| 14 | |
---|
| 15 | ;; This program is distributed in the hope that it will be useful, but |
---|
| 16 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | ;; General Public License for more details. |
---|
| 19 | |
---|
| 20 | ;; You should have received a copy of the GNU General Public License |
---|
| 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
---|
| 22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 23 | ;; Boston, MA 02111-1307, USA. |
---|
| 24 | |
---|
| 25 | ;;; Code: |
---|
| 26 | |
---|
| 27 | ;;;###autoload |
---|
| 28 | (defun put-alist (key value alist) |
---|
| 29 | "Set cdr of an element (KEY . ...) in ALIST to VALUE and return ALIST. |
---|
| 30 | If there is no such element, create a new pair (KEY . VALUE) and |
---|
| 31 | return a new alist whose car is the new pair and cdr is ALIST." |
---|
| 32 | (let ((elm (assoc key alist))) |
---|
| 33 | (if elm |
---|
| 34 | (progn |
---|
| 35 | (setcdr elm value) |
---|
| 36 | alist) |
---|
| 37 | (cons (cons key value) alist)))) |
---|
| 38 | |
---|
| 39 | ;;;###autoload |
---|
| 40 | (defun del-alist (key alist) |
---|
| 41 | "Delete an element whose car equals KEY from ALIST. |
---|
| 42 | Return the modified ALIST." |
---|
| 43 | (let ((pair (assoc key alist))) |
---|
| 44 | (if pair |
---|
| 45 | (delq pair alist) |
---|
| 46 | alist))) |
---|
| 47 | |
---|
| 48 | ;;;###autoload |
---|
| 49 | (defun set-alist (symbol key value) |
---|
| 50 | "Set cdr of an element (KEY . ...) in the alist bound to SYMBOL to VALUE." |
---|
| 51 | (or (boundp symbol) |
---|
| 52 | (set symbol nil)) |
---|
| 53 | (set symbol (put-alist key value (symbol-value symbol)))) |
---|
| 54 | |
---|
| 55 | ;;;###autoload |
---|
| 56 | (defun remove-alist (symbol key) |
---|
| 57 | "Delete an element whose car equals KEY from the alist bound to SYMBOL." |
---|
| 58 | (and (boundp symbol) |
---|
| 59 | (set symbol (del-alist key (symbol-value symbol))))) |
---|
| 60 | |
---|
| 61 | ;;;###autoload |
---|
| 62 | (defun modify-alist (modifier default) |
---|
| 63 | "Store elements in the alist MODIFIER in the alist DEFAULT. |
---|
| 64 | Return the modified alist." |
---|
| 65 | (mapcar (function |
---|
| 66 | (lambda (as) |
---|
| 67 | (setq default (put-alist (car as)(cdr as) default)))) |
---|
| 68 | modifier) |
---|
| 69 | default) |
---|
| 70 | |
---|
| 71 | ;;;###autoload |
---|
| 72 | (defun set-modified-alist (symbol modifier) |
---|
| 73 | "Store elements in the alist MODIFIER in an alist bound to SYMBOL. |
---|
| 74 | If SYMBOL is not bound, set it to nil at first." |
---|
| 75 | (if (not (boundp symbol)) |
---|
| 76 | (set symbol nil)) |
---|
| 77 | (set symbol (modify-alist modifier (eval symbol)))) |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | ;;; @ association-vector-list |
---|
| 81 | ;;; |
---|
| 82 | |
---|
| 83 | ;;;###autoload |
---|
| 84 | (defun vassoc (key avlist) |
---|
| 85 | "Search AVLIST for an element whose first element equals KEY. |
---|
| 86 | AVLIST is a list of vectors. |
---|
| 87 | See also `assoc'." |
---|
| 88 | (while (and avlist |
---|
| 89 | (not (equal key (aref (car avlist) 0)))) |
---|
| 90 | (setq avlist (cdr avlist))) |
---|
| 91 | (and avlist |
---|
| 92 | (car avlist))) |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | ;;; @ end |
---|
| 96 | ;;; |
---|
| 97 | |
---|
| 98 | (require 'product) |
---|
| 99 | (product-provide (provide 'alist) (require 'apel-ver)) |
---|
| 100 | |
---|
| 101 | ;;; alist.el ends here |
---|