Debian Codegraph @

src/libmemo.scm

raw
1 ;;;
2 ;;; libmemo.scm - interface for memoization table
3 ;;;
4 ;;; Copyright (c) 2021-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 (select-module gauche.internal)
35 (inline-stub
36 (.include "gauche/priv/configP.h"
37 "gauche/priv/memoP.h")
38
39 (declare-stub-type <memo-table> "ScmMemoTable*")
40 )
41
42 ;;
43 ;; The API is provisional
44 ;;
45
46 (define-cproc make-memo-table (capacity::<ulong>
47 num-keys::<int>
48 :key (weak #f)
49 (fixed #f))
50 (let* ([flags::u_long 0])
51 (unless (SCM_FALSEP weak) (logior= flags SCM_MEMO_TABLE_WEAK))
52 (unless (SCM_FALSEP fixed) (logior= flags SCM_MEMO_TABLE_FIXED))
53 (return (Scm_MakeMemoTable capacity num-keys flags))))
54
55 ;; Instead of the standard (*-get obj key :optional default) -> <top>
56 ;; signature, this returns two values: The value or #<undef>, and
57 ;; a boolean that indicates a hit.
58 ;; Since mishit is in ordinary operation in memoization table, it is a
59 ;; waste to provide and check default value. The '2' suffis is to
60 ;; distinguish from the standard protocol.
61
62 ;; keys are given in a vector; must match the table entry's shape.
63 (define-cproc memo-table-getv2 (tab::<memo-table> keys::<vector>)
64 ::(<top> <boolean>)
65 (let* ([v (Scm_MemoTableGetv tab (SCM_VECTOR_ELEMENTS keys)
66 (SCM_VECTOR_SIZE keys))])
67 (if (SCM_UNBOUNDP v)
68 (return SCM_UNDEFINED FALSE)
69 (return v TRUE))))
70
71 ;; keys are given as a list
72 (define-cproc memo-table-get2 (tab::<memo-table> keys::<list>)
73 ::(<top> <boolean>)
74 (let* ([v (Scm_MemoTableGet tab keys)])
75 (if (SCM_UNBOUNDP v)
76 (return SCM_UNDEFINED FALSE)
77 (return v TRUE))))
78
79 ;; keys are given in a vector; must match the table entry's shape.
80 (define-cproc memo-table-putv! (tab::<memo-table> keys::<vector> value)
81 (return (Scm_MemoTablePutv tab (SCM_VECTOR_ELEMENTS keys)
82 (SCM_VECTOR_SIZE keys) value)))
83
84 ;; keys are given as a list
85 (define-cproc memo-table-put! (tab::<memo-table> keys::<list> value)
86 (return (Scm_MemoTablePut tab keys value)))
87
88 ;; For debugging
89 (define-cproc memo-table-dump (tab::<memo-table>
90 :optional (port::<port> (current-output-port)))
91 ::<void>
92 (Scm__MemoTableDump tab port))