Debian Codegraph @

src/libdict.scm

raw
1 ;;;
2 ;;; libgdict.scm - generic dictionary base API
3 ;;;
4 ;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (declare) ;; a dummy form to suppress generation of "sci" file
35
36 ;; NOTE: This must be initialized after macro and object systems are booted.
37 ;;
38 ;; The module gauche.libdict is not meant to be used directly; the user
39 ;; program needs to use gauche.dictionary.
40 ;;
41 ;; We define basic generic functions for dictionary API, and attach
42 ;; methods specialized to hashtable and treemap here. Built-in writer
43 ;; refers to them.
44 ;;
45 ;;
46 ;;
47
48 (define-module gauche.libdict
49 (export define-dict-interface
50 dict-get dict-put! |setter of dict-get|
51 dict-immutable? dict-transparent? dict-exists?
52 dict-delete!
53 dict-seek dict-find dict-any
54 dict-fold dict-fold-right
55 dict-for-each dict-map
56 dict-keys dict-values dict-comparator
57 dict->alist dict-pop! dict-push! dict-update! dict-clear!
58 ))
59 (select-module gauche.libdict)
60
61 ;; A convenient macro to define dictionary methods.
62
63 (define-macro (define-dict-interface class . clauses)
64 (define (gen-def kind specific)
65 (let ([dict (gensym)]
66 [key (gensym)]
67 [val (gensym)]
68 [default (gensym)]
69 [proc (gensym)]
70 [seed (gensym)]
71 [succ (gensym)]
72 [fail (gensym)])
73 (case kind
74 [(:get)
75 `(define-method dict-get ((,dict ,class) ,key . ,default)
76 (if (null? ,default)
77 (,specific ,dict ,key)
78 (,specific ,dict ,key (car ,default))))]
79 [(:put!)
80 `(define-method dict-put! ((,dict ,class) ,key ,val)
81 (,specific ,dict ,key ,val))]
82 [(:exists?)
83 `(define-method dict-exists? ((,dict ,class) ,key)
84 (,specific ,dict ,key))]
85 [(:immutable?)
86 `(define-method dict-immutable? ((,dict ,class)) (,specific ,dict))]
87 [(:transparent?)
88 `(define-method dict-transparent? ((,dict ,class)) (,specific ,dict))]
89 [(:delete!)
90 `(define-method dict-delete! ((,dict ,class) ,key)
91 (,specific ,dict ,key))]
92 [(:clear!)
93 `(define-method dict-clear! ((,dict ,class))
94 (,specific ,dict))]
95 [(:fold)
96 `(define-method dict-fold ((,dict ,class) ,proc ,seed)
97 (,specific ,dict ,proc ,seed))]
98 [(:fold-right)
99 `(define-method dict-fold-right ((,dict ,class) ,proc ,seed)
100 (,specific ,dict ,proc ,seed))]
101 [(:map)
102 `(define-method dict-map ((,dict ,class) ,proc)
103 (,specific ,dict ,proc))]
104 [(:for-each)
105 `(define-method dict-for-each ((,dict ,class) ,proc)
106 (,specific ,dict ,proc))]
107 [(:keys)
108 `(define-method dict-keys ((,dict ,class))
109 (,specific ,dict))]
110 [(:values)
111 `(define-method dict-values ((,dict ,class))
112 (,specific ,dict))]
113 [(:push!)
114 `(define-method dict-push! ((,dict ,class) ,key ,val)
115 (,specific ,dict ,key ,val))]
116 [(:pop!)
117 `(define-method dict-pop! ((,dict ,class) ,key . ,default)
118 (apply ,specific ,dict ,key ,default))]
119 [(:update!)
120 `(define-method dict-update! ((,dict ,class) ,key . ,default)
121 (apply ,specific ,dict ,key ,default))]
122 [(:->alist)
123 `(define-method dict->alist ((,dict ,class))
124 (,specific ,dict))]
125 [(:comparator)
126 `(define-method dict-comparator ((,dict ,class))
127 (,specific ,dict))]
128 [(:seek)
129 `(define-method dict-seek ((,dict ,class) ,proc ,fail ,succ)
130 (,specific ,dict ,proc ,fail ,succ))]
131 [else (error "invalid kind in define-dict-interface:" kind)])))
132 `(begin
133 ,@(map (^p (gen-def (car p) (cadr p))) (slices clauses 2))))
134
135 ;;-----------------------------------------------
136 ;; Methods for hash-table, tree-map
137 ;;
138
139 (define-dict-interface <hash-table>
140 :get hash-table-get
141 :put! hash-table-put!
142 :delete! hash-table-delete!
143 :clear! hash-table-clear!
144 :exists? hash-table-exists?
145 :seek hash-table-seek
146 :fold hash-table-fold
147 :for-each hash-table-for-each
148 :map hash-table-map
149 :keys hash-table-keys
150 :values hash-table-values
151 :pop! hash-table-pop!
152 :push! hash-table-push!
153 :update! hash-table-update!
154 :->alist hash-table->alist
155 :comparator hash-table-comparator
156 :transparent? (^_ #t))
157
158 (define-dict-interface <tree-map>
159 :get tree-map-get
160 :put! tree-map-put!
161 :delete! tree-map-delete!
162 :clear! tree-map-clear!
163 :exists? tree-map-exists?
164 :seek tree-map-seek
165 :fold tree-map-fold
166 :fold-right tree-map-fold-right
167 :for-each tree-map-for-each
168 :map tree-map-map
169 :keys tree-map-keys
170 :values tree-map-values
171 :pop! tree-map-pop!
172 :push! tree-map-push!
173 :update! tree-map-update!
174 :->alist tree-map->alist
175 :comparator tree-map-comparator
176 :transparent? (^_ #t))