Add 2010 to copyright years.
[bpt/emacs.git] / lisp / emacs-lisp / assoc.el
CommitLineData
cdccfc0d
ER
1;;; assoc.el --- insert/delete/sort functions on association lists
2
d59c3137 3;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005,
114f9c96 4;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
b578f267 5
cdccfc0d
ER
6;; Author: Barry A. Warsaw <bwarsaw@cen.com>
7;; Keywords: extensions
8
b578f267
EN
9;; This file is part of GNU Emacs.
10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b578f267
EN
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
cdccfc0d
ER
23
24;;; Commentary:
25
26;; Association list utilities providing insertion, deletion, sorting
27;; fetching off key-value pairs in association lists.
28
29;;; Code:
30
6476a5fa 31(defun asort (alist-symbol key)
cdccfc0d 32 "Move a specified key-value pair to the head of an alist.
6cda144f 33The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
cdccfc0d
ER
34head is one matching KEY. Returns the sorted list and doesn't affect
35the order of any other key-value pair. Side effect sets alist to new
36sorted list."
37 (set alist-symbol
38 (sort (copy-alist (eval alist-symbol))
39 (function (lambda (a b) (equal (car a) key))))))
40
41
42(defun aelement (key value)
6cda144f 43 "Make a list of a cons cell containing car of KEY and cdr of VALUE.
cdccfc0d
ER
44The returned list is suitable as an element of an alist."
45 (list (cons key value)))
46
47
48(defun aheadsym (alist)
49 "Return the key symbol at the head of ALIST."
50 (car (car alist)))
51
52
53(defun anot-head-p (alist key)
54 "Find out if a specified key-value pair is not at the head of an alist.
55The alist to check is specified by ALIST and the key-value pair is the
56one matching the supplied KEY. Returns nil if ALIST is nil, or if
57key-value pair is at the head of the alist. Returns t if key-value
58pair is not at the head of alist. ALIST is not altered."
59 (not (equal (aheadsym alist) key)))
60
61
62(defun aput (alist-symbol key &optional value)
63 "Inserts a key-value pair into an alist.
6cda144f
JB
64The alist is referenced by ALIST-SYMBOL. The key-value pair is made
65from KEY and optionally, VALUE. Returns the altered alist or nil if
cdccfc0d
ER
66ALIST is nil.
67
68If the key-value pair referenced by KEY can be found in the alist, and
69VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
70If VALUE is not supplied, or is nil, the key-value pair will not be
6cda144f 71modified, but will be moved to the head of the alist. If the key-value
cdccfc0d
ER
72pair cannot be found in the alist, it will be inserted into the head
73of the alist (with value nil if VALUE is nil or not supplied)."
74 (let ((elem (aelement key value))
75 alist)
76 (asort alist-symbol key)
77 (setq alist (eval alist-symbol))
78 (cond ((null alist) (set alist-symbol elem))
79 ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
80 (value (setcar alist (car elem)))
81 (t alist))))
82
83
84(defun adelete (alist-symbol key)
85 "Delete a key-value pair from the alist.
86Alist is referenced by ALIST-SYMBOL and the key-value pair to remove
87is pair matching KEY. Returns the altered alist."
88 (asort alist-symbol key)
89 (let ((alist (eval alist-symbol)))
90 (cond ((null alist) nil)
91 ((anot-head-p alist key) alist)
92 (t (set alist-symbol (cdr alist))))))
93
94
95(defun aget (alist key &optional keynil-p)
6cda144f 96 "Return the value in ALIST that is associated with KEY.
cdccfc0d
ER
97Optional KEYNIL-P describes what to do if the value associated with
98KEY is nil. If KEYNIL-P is not supplied or is nil, and the value is
99nil, then KEY is returned. If KEYNIL-P is non-nil, then nil would be
100returned.
101
102If no key-value pair matching KEY could be found in ALIST, or ALIST is
6cda144f 103nil then nil is returned. ALIST is not altered."
cdccfc0d
ER
104 (let ((copy (copy-alist alist)))
105 (cond ((null alist) nil)
106 ((progn (asort 'copy key)
107 (anot-head-p copy key)) nil)
108 ((cdr (car copy)))
109 (keynil-p nil)
110 ((car (car copy)))
111 (t nil))))
112
113
114(defun amake (alist-symbol keylist &optional valuelist)
115 "Make an association list.
116The association list is attached to the alist referenced by
6cda144f
JB
117ALIST-SYMBOL. Each element in the KEYLIST becomes a key and is
118associated with the value in VALUELIST with the same index. If
cdccfc0d
ER
119VALUELIST is not supplied or is nil, then each key in KEYLIST is
120associated with nil.
121
122KEYLIST and VALUELIST should have the same number of elements, but
123this isn't enforced. If VALUELIST is smaller than KEYLIST, remaining
124keys are associated with nil. If VALUELIST is larger than KEYLIST,
125extra values are ignored. Returns the created alist."
126 (let ((keycar (car keylist))
127 (keycdr (cdr keylist))
128 (valcar (car valuelist))
129 (valcdr (cdr valuelist)))
130 (cond ((null keycdr)
131 (aput alist-symbol keycar valcar))
132 (t
133 (amake alist-symbol keycdr valcdr)
134 (aput alist-symbol keycar valcar))))
135 (eval alist-symbol))
136
137(provide 'assoc)
138
cbee283d 139;; arch-tag: 3e58bd89-d912-4b74-a0dc-6ed9735922bc
cdccfc0d 140;;; assoc.el ends here