source: project/wiki/eggref/4/record-variants @ 13867

Last change on this file since 13867 was 13867, checked in by Jim Ursetto, 11 years ago

Changes applied for zbigniew (71.201.84.72) through svnwiki:

record-variants: update example

File size: 6.4 KB
Line 
1[[toc:]]
2
3== record-variants
4
5'''record-variants''' defines optimized variants on existing records
6defined with {{define-record}}, and on SRFI 9 records defined with
7{{define-record-type}}.  You may also create new record types if
8desired.
9
10=== Interface
11
12==== define-record-variant
13
14<macro>(define-record-variant name-spec variant-spec slot1 slot2 ...)</macro>
15
16where:
17
18 name-spec := (variant-name original-name) | variant-name
19 variant-spec := (variant-type ...)
20 variant-type := unsafe | unchecked | inline
21
22Defines alternate accessor procedures to the existing record
23{{original-name}} according to {{variant-spec}}.  The accessors
24will be defined using {{variant-name}}, as if
25{{(define-record variant-name slot1 slot2 ...)}} had been invoked,
26but they will operate on records of type {{original-name}}.
27
28Variant type may be one of:
29* {{inline}}, so procedure definitions use {{define-inline}};
30* {{unchecked}}, so accessors do not check the record type;
31* {{unsafe}}, so accessors use {{##sys#slot}} and {{##sys#setslot}} instead of the safe {{block-ref!}} and {{block-set!}}
32
33and any combination of {{variant-type}} is allowed in {{variant-spec}}.
34
35A constructor, {{make-VARIANT-NAME}}, is defined to create a record
36of the original type.  If you are defining a variant on an existing
37record, this is here essentially for completeness, as {{unsafe}}
38and {{unchecked}} don't have any effect on the constructor --
39though {{inline}} will inline it.
40
41Additionally, one new procedure over {{define-record}} is created:
42
43{{(check-VARIANT-NAME x)}}: Checks that {{x}} is of the corresponding
44record type and returns {{x}} if so; otherwise throws an error.  When
45compiled in unsafe mode no check is performed, regardless of
46{{variant-type}}.
47
48{{unsafe}} and {{unchecked}} accessors are dangerous and should
49only be used internally in a module.  Only use these when you
50are absolutely sure the object is of the correct type; it is ''highly''
51recommended to use {{(check-VARIANT-NAME x)}} or call an original
52accessor before using these, after which the correct type
53is guaranteed.  (Assuming no side effects anywhere else!)
54
55Note that {{(define-record-variant foo () x y)}} is equivalent to
56{{(define-record foo x y)}} except that a {{check-foo}} procedure
57will be generated.
58
59==== define-record-type-variant
60
61<macro>(define-record-type-variant name-spec variant-spec pred-spec constructor field-spec)</macro>
62
63where:
64
65 name-spec := (variant-name original-name) | variant-name
66 variant-spec := (variant-type ...)
67 variant-type := unsafe | unchecked | inline
68 pred-spec := (predicate checker) | (predicate) | predicate
69 constructor, field-spec: as in SRFI 9
70
71Defines alternate accessor procedures to the existing SRFI 9
72record-type {{original-name}} according to {{variant-spec}}.
73
74{{name-spec}} acts as it does in {{define-record-variant}},
75including constructor generation behavior.
76
77{{pred-spec}} may be a predicate identifier or a list containing
78a predicate identifier and optionally a "checker" identifier.  The
79checker identifier is used as the name of the generated
80{{check-VARIANT-NAME}} procedure, which again behaves as in
81{{define-record-variant}}.  If the checker identifier is
82omitted, no check procedure is generated.
83
84See {{define-record-variant}} and SRFI 9 for further details.
85
86=== Examples
87
88Create a variant on an existing record {{node}} and a new
89record type {{%lru-cache}}.  Use e.g. {{(%lru-cache lru-cache)}} as
90{{name-spec}} to create a variant on the existing record
91{{lru-cache}}.
92
93 (define-record node prev next key value)
94 (define-record-variant (%node node)
95   (unsafe unchecked inline)
96   prev next key value)
97 
98 (define-record-type-variant %lru-cache
99   (unsafe unchecked inline)
100   (%make-lru-cache ht head tail)
101   (%lru-cache? %check-lru-cache)     
102   (ht %lru-cache-ht)
103   (head %lru-cache-head %lru-cache-head-set!)     
104   (tail %lru-cache-tail %lru-cache-tail-set!))
105 
106 (define-inline (lookup c k)
107   (hash-table-ref/default (%lru-cache-ht (%check-lru-cache c) k #f))
108 
109 (define (lru-cache-ref c k)
110   (and-let* ((n (lookup c k)))   ; c now guaranteed to be valid
111     (check-%node n)              ; n now guaranteed to be valid
112     (if (not (%node-prev n))     ; MRU
113         (%node-value n)
114         (let ((nx (%node-next n))
115               (pr (%node-prev n)))
116           (when pr
117             (check-%node pr)     ; pr now valid
118             (%node-next-set! pr nx)
119             (%node-prev-set! n #f)
120             (when (eq? n (%lru-cache-tail c))
121               (%lru-cache-tail-set! c pr)))
122           (when nx
123             (check-%node nx)     ; nx now valid
124             (%node-prev-set! nx pr))
125           (let ((head (%lru-cache-head c)))
126             (check-%node head)   ; head now valid
127             (%node-prev-set! head n)
128             (%node-next-set! n head)
129             (%lru-cache-head-set! c n)
130             (%node-value n)) ))))
131
132=== Author
133
134Jim Ursetto
135
136=== Version history
137
138* 0.5 (2009-03-22): Inital release
139
140=== Acknowledgements
141
142Inspiration was taken from Kon Lovett's {{misc-extn-record}}.
143
144=== License
145
146 Copyright (c) 2009 Jim Ursetto.  All rights reserved.
147 
148 Redistribution and use in source and binary forms, with or without
149 modification, are permitted provided that the following conditions are met:
150 
151  Redistributions of source code must retain the above copyright notice,
152   this list of conditions and the following disclaimer.
153  Redistributions in binary form must reproduce the above copyright notice,
154   this list of conditions and the following disclaimer in the documentation
155   and/or other materials provided with the distribution.
156  Neither the name of the author nor the names of its contributors
157   may be used to endorse or promote products derived from this software
158   without specific prior written permission.
159 
160 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
161 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
162 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
163 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
164 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
165 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
166 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
167 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
168 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
169 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
170 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171
Note: See TracBrowser for help on using the repository browser.