-
Notifications
You must be signed in to change notification settings - Fork 11
/
tables.lisp
210 lines (173 loc) · 6.88 KB
/
tables.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
;;; A simple table utility
;;; ----------------------------------------------------------------------
;;; - File: tables.lisp
;;; - Author: Chris Riesbeck
#|
Copyright (c) 2006 Christopher K. Riesbeck
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
|#
;;; ----------------------------------------------------------------------
;;; Defining a table function
;;; ----------------------------------------------------------------------
;;; (DEFTABLE name) => name
;;;
;;; DEFTABLE defines name to be a table function such that
;;;
;;; - (name key) retrieves a value for key, if any
;;; - (SETF (name key) value) stores a value for key
;;; - (name) returns the internal table associated with name;
;;; this is useful when manipulating tables (see below).
;;;
;;; The table is empty when name is defined (or redefined).
;;;
;;; Examples:
;;;
;;; > (deftable AGE-of)
;;; AGE-OF
;;; > (age-of 'john)
;;; NIL
;;; > (setf (age-of 'john) 22)
;;; 22
;;; > (age-of 'john)
;;; 22
;;;
;;; Note: DEFTABLE is a top-level form, like DEFUN. It is not for
;;; creating local table functions. The following is wrong:
;;;
;;; (defun foo (...)
;;; (deftable baz)
;;; ...)
;;;
;;; If you want a local table, use MAKE-HASH-TABLE and GETHASH.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Packages
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cl:defpackage #:tables
(:use #:common-lisp)
(:export #:clear-table #:deftable #:in-table-p #:map-table #:remove-key)
)
(in-package #:tables)
;;; ----------------------------------------------------------------------
;;; Implementation notes:
;;;
;;; - I avoided (DEFUN (SETF fn) ...) so as not to require CL 2
;;; - I used PROGN to make the DEFSETF top-level for MacIntosh
;;; Common Lisp.
(defmacro deftable (fn)
(let ((set-fn (gentemp)))
`(eval-when (:compile-toplevel :load-toplevel :execute)
(let* ((fn ',fn)
(table (get-table fn)))
(defun ,fn (&optional (key nil key-given-p))
(if key-given-p
(gethash key table)
table))
(defun ,set-fn (arg1 &optional (arg2 nil arg2-p))
(cond (arg2-p
(setf (gethash arg1 table) arg2))
(t (set-table fn arg1)))))
(defsetf ,fn ,set-fn)
',fn)))
(defvar *tables* (make-hash-table)
"Table of DEFTABLE functions.")
(defun get-table (name)
(or (gethash name *tables*)
(set-table name (make-hash-table))))
(defun set-table (name table)
(if (hash-table-p table)
(setf (gethash name *tables*) table)
(error "~S not a table" table)))
;;; ----------------------------------------------------------------------
;;; Manipulating tables
;;; ----------------------------------------------------------------------
;;; Certain functions need explicit access to the internal table. To
;;; get this table, call the table function with no arguments, e.g.,
;;; (AGE-OF). This returns the internal table for AGE-OF, which
;;; can then be passed to a table manipulation function.
;;;
;;; Example: The following clears the AGE-OF table.
;;;
;;; > (clear-table (age-of))
;;;
;;; The nature of the internal table is implementation-dependent.
;;; (IN-TABLE-P key table) => T or NIL
;;; Returns true if key has a value in the table.
;;; (REMOVE-KEY key table) => T or NIL
;;; Removes any entry for key in the table, and returns true
;;; if there was one.
;;; (CLEAR-TABLE table) => table
;;; Removes all entries from the table.
;;; (MAP-TABLE function table) => NIL
;;; Calls (function key value) for every key and value in the table.
;;; The order in which keys are found is implementation-dependent.
;;; ----------------------------------------------------------------------
;;; Implementation notes:
;;;
;;; - I avoided MULTIPLE-VALUE-BIND for Xlisp compatibility.
(let ((flag (list nil)))
(defun in-table-p (key table)
(not (eq flag (gethash key table flag)))))
(defun remove-key (key table) (remhash key table))
(defun clear-table (table) (clrhash table))
(defun map-table (fn table) (maphash fn table))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|(in-package #+:cltl2 :common-lisp-user #-:cltl2 :user)|#
#|(provide "tables")|#
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Change log
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#|
1/3/03 [CKR]
Problem: wouldn't load in Allegro Modern
Cause: uppercase export strings
Change: use #:symbol hack
9/13/95 [CKR]
Problem: the package was not getting common-lisp in some CL2's
Cause: CLTL2 is not a defined feature in all CL2's
Change: Use (OR (find-package ...) (find-package ...)) form.
11/3/94 [CKR]
Problem: :lisp/:common-lisp conflicts
Change: added #+:cltl2 forms
10/19/94 [CKR]
Problem: PROVIDE undefined function error.
Cause: (make-package :tables :use '(:common-lisp))
Change: Insert (in-package :common-lisp-user) before (provide ...)
10/19/94 [CKR]
Problem: EXPORT undefined function error.
Cause: (make-package :tables)
Change: (make-package :tables :use '(:common-lisp)).
7/27/94 [CKR]
Problem: If name is a function, (DEFTABLE name) would cause an error.
Cause: Calling (name) doesn't do the right thing.
Change: Store name->table associations in the table *TABLES*.
12/1/93 [CKR]
Problem: If several packages used TABLES, they each loaded separate
copies of TABLES.
Cause: No TABLES package (because all functions were exported) that
they could use.
Change: Set up TABLES package.
11/4/92 [CKR]
Problem: In some Lisps, e.g., MCL, the DEFSETF in DEFTABLE wasn't
happening at the right time in compiled code.
Cause: DEFSETF, a top-level form, was inside the LET.
Change: Put DEFSETF outside the LET, in a PROGN.
9/30/92 [CKR]
Problem: IN-TABLE-P returned multiple values instead of just T or NIL
Cause: IN-TABLE-P defined as a simple call to GETHASH
Change: Use (NOT (EQ flag (GETHASH ... flag)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|#