This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
s48-rdf-map.scm
122 lines (100 loc) · 3.72 KB
/
s48-rdf-map.scm
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
;;; -*- Mode: Scheme; scheme48-package: rdf-maps-internal -*-
;;;; Schemantic Web
;;;; Maps Keyed by RDF Resources
;;; This code is written by Taylor R. Campbell and placed in the Public
;;; Domain. All warranties are disclaimed.
(define (map/lookup map key default)
(cond ((table-ref map key) => decanonicalize-false)
(else default)))
(define (map/search map key if-found if-not-found)
(cond ((table-ref map key)
=> (lambda (datum)
(if-found (decanonicalize-false datum))))
(else (if-not-found))))
(define (map/insert! map key datum)
(table-set! map key (canonicalize-false datum)))
(define (map/delete! map key)
(table-set! map key #f))
(define (map/update! map key if-found if-not-found)
(cond ((table-ref map key)
=> (lambda (datum)
(if-found (decanonicalize-false datum)
(lambda (datum*) ;replace
(table-set! map key (canonicalize-false datum*)))
(lambda () ;delete
(table-set! map key #f)))))
(else
(if-not-found (lambda (datum) ;insert
(table-set! map key (canonicalize-false datum)))))))
(define (map/modify! map key default modifier)
(table-set! map
key
(modifier (cond ((table-ref map key) => decanonicalize-false)
(else default)))))
(define (map/intern! map key generator)
(cond ((table-ref map key) => decanonicalize-false)
(else
(let ((datum (generator)))
(table-set! map key (canonicalize-false datum))
datum))))
(define (map/size map)
;; (collect-count (for key datum (in-map map)))
(let ((size 0))
(table-walk (lambda (key datum)
key datum ;ignore
(set! size (+ size 1)))
map)
size))
;;;; Iteration / Listing / Specialized Maps
(define (map/walk map procedure)
;; (loop ((for key datum (in-map map)))
;; (procedure key datum))
(table-walk procedure map))
(define-syntax in-map
(syntax-rules ()
((IN-MAP (key-variable datum-variable) (map-expression) next . rest)
(next (((ALIST) ;Outer bindings
(MAP->ALIST map-expression)))
((ALIST ALIST (CDR ALIST))) ;Loop variables
() ;Entry bindings
((NOT (PAIR? ALIST))) ;Termination conditions
(((key-variable datum-variable) ;Body bindings
(LET ((ENTRY (CAR ALIST)))
(VALUES (CAR ENTRY)
(CDR ENTRY)))))
() ;Final bindings
. rest))))
(define (map/key-list map)
(let ((keys '()))
(map/walk map (lambda (key datum) datum (set! keys (cons key keys))))
keys))
(define (map/datum-list map)
(let ((data '()))
(map/walk map (lambda (key datum) key (set! data (cons datum data))))
data))
(define (map->alist map)
(let ((alist '()))
(map/walk map
(lambda (key datum)
(set! alist (cons (cons key datum) alist))))
alist))
(define make-rdf-subject-map
(make-table-maker rdf-subject=? rdf-subject-hash))
(define make-rdf-predicate-map
(make-table-maker rdf-predicate=? rdf-predicate-hash))
(define make-rdf-object-map
(make-table-maker rdf-object=? rdf-object-hash))
;;;; False Canonicalization
(define canonicalize-false)
(define decanonicalize-false)
(let ((false-token (cons 'FALSE '())))
(set! canonicalize-false
(lambda (datum)
(if (not datum)
false-token
datum)))
(set! decanonicalize-false
(lambda (datum)
(if (eq? datum false-token)
#f
datum))))