annotate lisp/utils/assoc.el @ 172:a38aed19690b

Added tag r20-3b12 for changeset 929b76928fce
author cvs
date Mon, 13 Aug 2007 09:47:55 +0200
parents ac2d302a0011
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; assoc.el --- insert/delete/sort functions on association lists
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
4
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Barry A. Warsaw <bwarsaw@cen.com>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
8 ;; This file is part of XEmacs.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
9
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
13 ;; any later version.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
18 ;; General Public License for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
20 ;; You should have received a copy of the GNU General Public License
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
23 ;; 02111-1307, USA.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
24
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
25 ;;; Synched up with: FSF 19.34.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; Association list utilities providing insertion, deletion, sorting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; fetching off key-value pairs in association lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (defun asort (alist-symbol key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 "Move a specified key-value pair to the head of an alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 The alist is referenced by ALIST-SYMBOL. Key-value pair to move to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 head is one matching KEY. Returns the sorted list and doesn't affect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 the order of any other key-value pair. Side effect sets alist to new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 sorted list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (set alist-symbol
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (sort (copy-alist (eval alist-symbol))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (function (lambda (a b) (equal (car a) key))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (defun aelement (key value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 "Makes a list of a cons cell containing car of KEY and cdr of VALUE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 The returned list is suitable as an element of an alist."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (list (cons key value)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (defun aheadsym (alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 "Return the key symbol at the head of ALIST."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (car (car alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (defun anot-head-p (alist key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 "Find out if a specified key-value pair is not at the head of an alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 The alist to check is specified by ALIST and the key-value pair is the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 one matching the supplied KEY. Returns nil if ALIST is nil, or if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 key-value pair is at the head of the alist. Returns t if key-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 pair is not at the head of alist. ALIST is not altered."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (not (equal (aheadsym alist) key)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (defun aput (alist-symbol key &optional value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 "Inserts a key-value pair into an alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 The alist is referenced by ALIST-SYMBOL. The key-value pair is made
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 from KEY and optionally, VALUE. Returns the altered alist or nil if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ALIST is nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 If the key-value pair referenced by KEY can be found in the alist, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 If VALUE is not supplied, or is nil, the key-value pair will not be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 modified, but will be moved to the head of the alist. If the key-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 pair cannot be found in the alist, it will be inserted into the head
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 of the alist (with value nil if VALUE is nil or not supplied)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (let ((elem (aelement key value))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (asort alist-symbol key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (setq alist (eval alist-symbol))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (cond ((null alist) (set alist-symbol elem))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ((anot-head-p alist key) (set alist-symbol (nconc elem alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (value (setcar alist (car elem)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (t alist))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (defun adelete (alist-symbol key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 "Delete a key-value pair from the alist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 Alist is referenced by ALIST-SYMBOL and the key-value pair to remove
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 is pair matching KEY. Returns the altered alist."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (asort alist-symbol key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (let ((alist (eval alist-symbol)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (cond ((null alist) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 ((anot-head-p alist key) alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (t (set alist-symbol (cdr alist))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (defun aget (alist key &optional keynil-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 "Returns the value in ALIST that is associated with KEY.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 Optional KEYNIL-P describes what to do if the value associated with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 KEY is nil. If KEYNIL-P is not supplied or is nil, and the value is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 nil, then KEY is returned. If KEYNIL-P is non-nil, then nil would be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 returned.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 If no key-value pair matching KEY could be found in ALIST, or ALIST is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 nil then nil is returned. ALIST is not altered."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (let ((copy (copy-alist alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (cond ((null alist) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 ((progn (asort 'copy key)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (anot-head-p copy key)) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 ((cdr (car copy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (keynil-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ((car (car copy)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (t nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (defun amake (alist-symbol keylist &optional valuelist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 "Make an association list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 The association list is attached to the alist referenced by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ALIST-SYMBOL. Each element in the KEYLIST becomes a key and is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 associated with the value in VALUELIST with the same index. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 VALUELIST is not supplied or is nil, then each key in KEYLIST is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 associated with nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 KEYLIST and VALUELIST should have the same number of elements, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 this isn't enforced. If VALUELIST is smaller than KEYLIST, remaining
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 keys are associated with nil. If VALUELIST is larger than KEYLIST,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 extra values are ignored. Returns the created alist."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (let ((keycar (car keylist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (keycdr (cdr keylist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (valcar (car valuelist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (valcdr (cdr valuelist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (cond ((null keycdr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (aput alist-symbol keycar valcar))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (amake alist-symbol keycdr valcdr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (aput alist-symbol keycar valcar))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (eval alist-symbol))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (provide 'assoc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ;;; assoc.el ends here